mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-20 11:58:45 +00:00
Fixed tab expansion not working in --wrap=never mode.
This commit is contained in:
parent
b4096e5627
commit
d90797f8e9
@ -1,7 +1,7 @@
|
||||
use console::AnsiCodeIterator;
|
||||
|
||||
/// Expand tabs like an ANSI-enabled expand(1).
|
||||
pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
|
||||
pub fn expand(line: &str, width: usize, cursor: &mut usize) -> String {
|
||||
let mut buffer = String::with_capacity(line.len() * 2);
|
||||
|
||||
for chunk in AnsiCodeIterator::new(line) {
|
||||
@ -11,20 +11,20 @@ pub fn expand(line: &str, width: usize, mut cursor: usize) -> String {
|
||||
while let Some(index) = text.find('\t') {
|
||||
// Add previous text.
|
||||
if index > 0 {
|
||||
cursor += index;
|
||||
*cursor += index;
|
||||
buffer.push_str(&text[0..index]);
|
||||
}
|
||||
|
||||
// Add tab.
|
||||
let spaces = width - (cursor % width);
|
||||
cursor += spaces;
|
||||
let spaces = width - (*cursor % width);
|
||||
*cursor += spaces;
|
||||
buffer.push_str(&*" ".repeat(spaces));
|
||||
|
||||
// Next.
|
||||
text = &text[index + 1..text.len()];
|
||||
}
|
||||
|
||||
cursor += text.len();
|
||||
*cursor += text.len();
|
||||
buffer.push_str(text);
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn preprocess(&self, text: &str, cursor: usize) -> String {
|
||||
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
||||
if self.config.tab_width > 0 {
|
||||
expand(text, self.config.tab_width, cursor)
|
||||
} else {
|
||||
@ -243,19 +243,15 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
let true_color = self.config.true_color;
|
||||
let colored_output = self.config.colored_output;
|
||||
|
||||
write!(
|
||||
handle,
|
||||
"{}",
|
||||
regions
|
||||
.iter()
|
||||
.map(|&(style, text)| as_terminal_escaped(
|
||||
style,
|
||||
&*self.preprocess(text, 0),
|
||||
true_color,
|
||||
colored_output,
|
||||
)).collect::<Vec<_>>()
|
||||
.join("")
|
||||
)?;
|
||||
for &(style, region) in regions.iter() {
|
||||
let text = &*self.preprocess(region, &mut cursor_total);
|
||||
write!(handle, "{}", as_terminal_escaped(
|
||||
style,
|
||||
&*text,
|
||||
true_color,
|
||||
colored_output,
|
||||
))?;
|
||||
}
|
||||
} else {
|
||||
for &(style, region) in regions.iter() {
|
||||
let mut ansi_iterator = AnsiCodeIterator::new(region);
|
||||
@ -280,8 +276,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
(text, false) => {
|
||||
let text = self.preprocess(
|
||||
text.trim_right_matches(|c| c == '\r' || c == '\n'),
|
||||
cursor_total,
|
||||
&mut cursor_total,
|
||||
);
|
||||
|
||||
let mut chars = text.chars();
|
||||
let mut remaining = text.chars().count();
|
||||
|
||||
@ -292,7 +289,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
if remaining <= available {
|
||||
let text = chars.by_ref().take(remaining).collect::<String>();
|
||||
cursor += remaining;
|
||||
cursor_total += remaining;
|
||||
|
||||
write!(
|
||||
handle,
|
||||
@ -330,7 +326,6 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
// It wraps.
|
||||
let text = chars.by_ref().take(available).collect::<String>();
|
||||
cursor = 0;
|
||||
cursor_total += available;
|
||||
remaining -= available;
|
||||
|
||||
write!(
|
||||
|
Loading…
x
Reference in New Issue
Block a user