2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-16 21:25:56 +00:00

Fix padding, add --wrap argument, disable wrap for non-tty.

Now bat(1) can be used like cat(1) again!
This commit is contained in:
eth-p 2018-05-12 13:23:33 -07:00
parent f95a23f948
commit cd26d403a3
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC

View File

@ -117,67 +117,59 @@ impl<'a> Printer<'a> {
let border = if gutter_width > 0 && self.config.output_components.grid() { let border = if gutter_width > 0 && self.config.output_components.grid() {
self.gen_border() self.gen_border()
} else { } else {
PrintSegment { for &(style, text) in regions.iter() {
size: 0, let mut chars = text.chars().filter(|c| *c != '\n');
text: "".to_owned(), let mut remaining = text.chars().filter(|c| *c != '\n').count();
}
};
cursor_max -= border.size; while remaining > 0 {
write!(self.handle, "{} ", border.text)?; let available = cursor_max - cursor;
// Line contents. // It fits.
for &(style, text) in regions.iter() { if remaining <= available {
let mut chars = text.chars().filter(|c| *c != '\n'); let text = chars.by_ref().take(remaining).collect::<String>();
let mut remaining = text.chars().filter(|c| *c != '\n').count(); cursor += remaining;
while remaining > 0 { write!(
let available = cursor_max - cursor; self.handle,
"{}",
as_terminal_escaped(
style,
&*text,
self.config.true_color,
self.config.colored_output
)
)?;
break;
}
// It fits. // It wraps.
if remaining <= available { if self.config.output_wrap == OutputWrap::Character {
let text = chars.by_ref().take(remaining).collect::<String>(); let text = chars.by_ref().take(available).collect::<String>();
cursor += remaining; cursor = 0;
remaining -= available;
write!( write!(
self.handle, self.handle,
"{}", "{}\n{}{}",
as_terminal_escaped( as_terminal_escaped(
style, style,
&*text, &*text,
self.config.true_color, self.config.true_color,
self.config.colored_output self.config.colored_output
) ),
)?; " ".repeat(gutter_width),
break; border.text.to_owned()
} )?;
// It wraps. continue;
if self.config.output_wrap == OutputWrap::Character { }
let text = chars.by_ref().take(available).collect::<String>();
cursor = 0;
remaining -= available;
write!(
self.handle,
"{}\n{}{} ",
as_terminal_escaped(
style,
&*text,
self.config.true_color,
self.config.colored_output
),
" ".repeat(gutter_width),
border.text.to_owned()
)?;
continue;
} }
} }
write!(self.handle, "\n")?;
} }
// Finished. // Finished.
write!(self.handle, "\n")?;
Ok(()) Ok(())
} }
@ -230,8 +222,8 @@ impl<'a> Printer<'a> {
/// Generates the vertical grid border. /// Generates the vertical grid border.
fn gen_border(&self) -> PrintSegment { fn gen_border(&self) -> PrintSegment {
return PrintSegment { return PrintSegment {
text: self.colors.grid.paint("").to_string(), text: self.colors.grid.paint(" ").to_string(),
size: 1, size: 2,
}; };
} }