mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-18 20:11:03 +00:00
fix: Wrap file name in header
This commit is contained in:
parent
db66e4459b
commit
45ee2dc4c7
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
## Bugfixes
|
## Bugfixes
|
||||||
|
|
||||||
|
- Fix long file name wrapping in header, see #2817 (@FilipRazek)
|
||||||
- Fix `NO_COLOR` support, see #2767 (@acuteenvy)
|
- Fix `NO_COLOR` support, see #2767 (@acuteenvy)
|
||||||
|
|
||||||
## Other
|
## Other
|
||||||
|
@ -302,6 +302,23 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn print_header_component_with_indent(&mut self, handle: &mut OutputHandle, content: &str) -> Result<()> {
|
||||||
|
self.print_header_component_indent(handle)?;
|
||||||
|
writeln!(handle, "{}", content)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_header_multiline_component(&mut self, handle: &mut OutputHandle, content: &str) -> Result<()> {
|
||||||
|
let mut content = content;
|
||||||
|
let panel_width = if self.panel_width > 0 { self.panel_width + 2 } else { 0 };
|
||||||
|
let content_width = self.config.term_width - panel_width;
|
||||||
|
while content.len() > content_width {
|
||||||
|
let (content_line, remaining) = content.split_at(content_width);
|
||||||
|
self.print_header_component_with_indent(handle, content_line)?;
|
||||||
|
content = remaining;
|
||||||
|
}
|
||||||
|
self.print_header_component_with_indent(handle, content)
|
||||||
|
}
|
||||||
|
|
||||||
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
||||||
if self.config.tab_width > 0 {
|
if self.config.tab_width > 0 {
|
||||||
return expand_tabs(text, self.config.tab_width, cursor);
|
return expand_tabs(text, self.config.tab_width, cursor);
|
||||||
@ -378,26 +395,26 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
header_components.iter().try_for_each(|component| {
|
header_components.iter().try_for_each(|component| {
|
||||||
self.print_header_component_indent(handle)?;
|
|
||||||
|
|
||||||
match component {
|
match component {
|
||||||
StyleComponent::HeaderFilename => writeln!(
|
StyleComponent::HeaderFilename => {
|
||||||
handle,
|
let header_filename = format!(
|
||||||
"{}{}{}",
|
"{}{}{}",
|
||||||
description
|
description
|
||||||
.kind()
|
.kind()
|
||||||
.map(|kind| format!("{}: ", kind))
|
.map(|kind| format!("{}: ", kind))
|
||||||
.unwrap_or_else(|| "".into()),
|
.unwrap_or_else(|| "".into()),
|
||||||
self.colors.header_value.paint(description.title()),
|
self.colors.header_value.paint(description.title()),
|
||||||
mode
|
mode
|
||||||
),
|
);
|
||||||
|
self.print_header_multiline_component(handle, &header_filename)
|
||||||
|
}
|
||||||
StyleComponent::HeaderFilesize => {
|
StyleComponent::HeaderFilesize => {
|
||||||
let bsize = metadata
|
let bsize = metadata
|
||||||
.size
|
.size
|
||||||
.map(|s| format!("{}", ByteSize(s)))
|
.map(|s| format!("{}", ByteSize(s)))
|
||||||
.unwrap_or_else(|| "-".into());
|
.unwrap_or_else(|| "-".into());
|
||||||
writeln!(handle, "Size: {}", self.colors.header_value.paint(bsize))
|
let header_filesize = format!("Size: {}", self.colors.header_value.paint(bsize));
|
||||||
|
self.print_header_multiline_component(handle, &header_filesize)
|
||||||
}
|
}
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
The header is not broken
|
@ -1381,6 +1381,61 @@ fn header_full_binary() {
|
|||||||
.stderr("");
|
.stderr("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn header_narrow_terminal() {
|
||||||
|
bat()
|
||||||
|
.arg("--terminal-width=30")
|
||||||
|
.arg("--decorations=always")
|
||||||
|
.arg("this-file-path-is-really-long-and-would-have-broken-the-layout-of-the-header.txt")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(
|
||||||
|
"\
|
||||||
|
───────┬──────────────────────
|
||||||
|
│ File: this-file-path-
|
||||||
|
│ is-really-long-and-wo
|
||||||
|
│ uld-have-broken-the-l
|
||||||
|
│ ayout-of-the-header.t
|
||||||
|
│ xt
|
||||||
|
───────┼──────────────────────
|
||||||
|
1 │ The header is not bro
|
||||||
|
│ ken
|
||||||
|
───────┴──────────────────────
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.stderr("");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn header_very_narrow_terminal() {
|
||||||
|
bat()
|
||||||
|
.arg("--terminal-width=10")
|
||||||
|
.arg("--decorations=always")
|
||||||
|
.arg("this-file-path-is-really-long-and-would-have-broken-the-layout-of-the-header.txt")
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(
|
||||||
|
"\
|
||||||
|
──────────
|
||||||
|
File: this
|
||||||
|
-file-path
|
||||||
|
-is-really
|
||||||
|
-long-and-
|
||||||
|
would-have
|
||||||
|
-broken-th
|
||||||
|
e-layout-o
|
||||||
|
f-the-head
|
||||||
|
er.txt
|
||||||
|
──────────
|
||||||
|
The header
|
||||||
|
is not br
|
||||||
|
oken
|
||||||
|
──────────
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.stderr("");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "git")] // Expected output assumes git is enabled
|
#[cfg(feature = "git")] // Expected output assumes git is enabled
|
||||||
fn header_default() {
|
fn header_default() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user