mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-16 07:38:42 +00:00
Fix wrapping method to support unicode text
Related issues: - #787 - #811
This commit is contained in:
parent
22ded00824
commit
944866affd
@ -17,6 +17,8 @@ use content_inspector::ContentType;
|
|||||||
use encoding::all::{UTF_16BE, UTF_16LE};
|
use encoding::all::{UTF_16BE, UTF_16LE};
|
||||||
use encoding::{DecoderTrap, Encoding};
|
use encoding::{DecoderTrap, Encoding};
|
||||||
|
|
||||||
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
use crate::assets::HighlightingAssets;
|
use crate::assets::HighlightingAssets;
|
||||||
use crate::decorations::{
|
use crate::decorations::{
|
||||||
Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration,
|
Decoration, GridBorderDecoration, LineChangesDecoration, LineNumberDecoration,
|
||||||
@ -469,35 +471,22 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
&mut cursor_total,
|
&mut cursor_total,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut chars = text.chars();
|
let max_width = cursor_max - cursor;
|
||||||
let mut remaining = text.chars().count();
|
|
||||||
|
|
||||||
while remaining > 0 {
|
// line buffer (avoid calling write! for every character)
|
||||||
let available = cursor_max - cursor;
|
let mut line_buf = String::with_capacity(max_width * 4);
|
||||||
|
|
||||||
// It fits.
|
// Displayed width of line_buf
|
||||||
if remaining <= available {
|
let mut current_width = 0;
|
||||||
let text = chars.by_ref().take(remaining).collect::<String>();
|
|
||||||
cursor += remaining;
|
|
||||||
|
|
||||||
write!(
|
for c in text.chars() {
|
||||||
handle,
|
// calculate the displayed width for next character
|
||||||
"{}",
|
let cw = c.width().unwrap_or(0);
|
||||||
as_terminal_escaped(
|
current_width += cw;
|
||||||
style,
|
|
||||||
&*format!(
|
|
||||||
"{}{}{}",
|
|
||||||
self.ansi_prefix_sgr, ansi_prefix, text
|
|
||||||
),
|
|
||||||
self.config.true_color,
|
|
||||||
self.config.colored_output,
|
|
||||||
self.config.use_italic_text,
|
|
||||||
background_color
|
|
||||||
)
|
|
||||||
)?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// if next character cannot be printed on this line,
|
||||||
|
// flush the buffer.
|
||||||
|
if current_width > max_width {
|
||||||
// Generate wrap padding if not already generated.
|
// Generate wrap padding if not already generated.
|
||||||
if panel_wrap.is_none() {
|
if panel_wrap.is_none() {
|
||||||
panel_wrap = if self.panel_width > 0 {
|
panel_wrap = if self.panel_width > 0 {
|
||||||
@ -516,11 +505,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// It wraps.
|
|
||||||
let text = chars.by_ref().take(available).collect::<String>();
|
|
||||||
cursor = 0;
|
cursor = 0;
|
||||||
remaining -= available;
|
|
||||||
|
|
||||||
|
// It wraps.
|
||||||
write!(
|
write!(
|
||||||
handle,
|
handle,
|
||||||
"{}\n{}",
|
"{}\n{}",
|
||||||
@ -528,7 +515,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
style,
|
style,
|
||||||
&*format!(
|
&*format!(
|
||||||
"{}{}{}",
|
"{}{}{}",
|
||||||
self.ansi_prefix_sgr, ansi_prefix, text
|
self.ansi_prefix_sgr, ansi_prefix, line_buf
|
||||||
),
|
),
|
||||||
self.config.true_color,
|
self.config.true_color,
|
||||||
self.config.colored_output,
|
self.config.colored_output,
|
||||||
@ -537,8 +524,32 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
),
|
),
|
||||||
panel_wrap.clone().unwrap()
|
panel_wrap.clone().unwrap()
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
line_buf.clear();
|
||||||
|
current_width = cw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
line_buf.push(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
// flush the buffer
|
||||||
|
cursor += current_width;
|
||||||
|
write!(
|
||||||
|
handle,
|
||||||
|
"{}",
|
||||||
|
as_terminal_escaped(
|
||||||
|
style,
|
||||||
|
&*format!(
|
||||||
|
"{}{}{}",
|
||||||
|
self.ansi_prefix_sgr, ansi_prefix, line_buf
|
||||||
|
),
|
||||||
|
self.config.true_color,
|
||||||
|
self.config.colored_output,
|
||||||
|
self.config.use_italic_text,
|
||||||
|
background_color
|
||||||
|
)
|
||||||
|
)?;
|
||||||
|
|
||||||
// Clear the ANSI prefix buffer.
|
// Clear the ANSI prefix buffer.
|
||||||
ansi_prefix.clear();
|
ansi_prefix.clear();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user