Skip to content

Commit 2086cd2

Browse files
authored
Merge pull request #2835 from FilipRazek/fix-long-file-names
fix: Wrap file name in header
2 parents db66e44 + 1b88267 commit 2086cd2

4 files changed

Lines changed: 108 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## Bugfixes
66

7+
- Fix long file name wrapping in header, see #2835 (@FilipRazek)
78
- Fix `NO_COLOR` support, see #2767 (@acuteenvy)
89

910
## Other

src/printer.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ impl<'a> InteractivePrinter<'a> {
287287
}
288288
}
289289

290+
fn get_header_component_indent_length(&self) -> usize {
291+
if self.config.style_components.grid() && self.panel_width > 0 {
292+
self.panel_width + 2
293+
} else {
294+
self.panel_width
295+
}
296+
}
297+
290298
fn print_header_component_indent(&mut self, handle: &mut OutputHandle) -> Result<()> {
291299
if self.config.style_components.grid() {
292300
write!(
@@ -302,6 +310,30 @@ impl<'a> InteractivePrinter<'a> {
302310
}
303311
}
304312

313+
fn print_header_component_with_indent(
314+
&mut self,
315+
handle: &mut OutputHandle,
316+
content: &str,
317+
) -> Result<()> {
318+
self.print_header_component_indent(handle)?;
319+
writeln!(handle, "{}", content)
320+
}
321+
322+
fn print_header_multiline_component(
323+
&mut self,
324+
handle: &mut OutputHandle,
325+
content: &str,
326+
) -> Result<()> {
327+
let mut content = content;
328+
let content_width = self.config.term_width - self.get_header_component_indent_length();
329+
while content.len() > content_width {
330+
let (content_line, remaining) = content.split_at(content_width);
331+
self.print_header_component_with_indent(handle, content_line)?;
332+
content = remaining;
333+
}
334+
self.print_header_component_with_indent(handle, content)
335+
}
336+
305337
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
306338
if self.config.tab_width > 0 {
307339
return expand_tabs(text, self.config.tab_width, cursor);
@@ -377,31 +409,32 @@ impl<'a> Printer for InteractivePrinter<'a> {
377409
}
378410
}
379411

380-
header_components.iter().try_for_each(|component| {
381-
self.print_header_component_indent(handle)?;
382-
383-
match component {
384-
StyleComponent::HeaderFilename => writeln!(
385-
handle,
386-
"{}{}{}",
387-
description
388-
.kind()
389-
.map(|kind| format!("{}: ", kind))
390-
.unwrap_or_else(|| "".into()),
391-
self.colors.header_value.paint(description.title()),
392-
mode
393-
),
394-
412+
header_components
413+
.iter()
414+
.try_for_each(|component| match component {
415+
StyleComponent::HeaderFilename => {
416+
let header_filename = format!(
417+
"{}{}{}",
418+
description
419+
.kind()
420+
.map(|kind| format!("{}: ", kind))
421+
.unwrap_or_else(|| "".into()),
422+
self.colors.header_value.paint(description.title()),
423+
mode
424+
);
425+
self.print_header_multiline_component(handle, &header_filename)
426+
}
395427
StyleComponent::HeaderFilesize => {
396428
let bsize = metadata
397429
.size
398430
.map(|s| format!("{}", ByteSize(s)))
399431
.unwrap_or_else(|| "-".into());
400-
writeln!(handle, "Size: {}", self.colors.header_value.paint(bsize))
432+
let header_filesize =
433+
format!("Size: {}", self.colors.header_value.paint(bsize));
434+
self.print_header_multiline_component(handle, &header_filesize)
401435
}
402436
_ => Ok(()),
403-
}
404-
})?;
437+
})?;
405438

406439
if self.config.style_components.grid() {
407440
if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The header is not broken

tests/integration_tests.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,61 @@ fn header_full_binary() {
13811381
.stderr("");
13821382
}
13831383

1384+
#[test]
1385+
#[cfg(not(feature = "git"))]
1386+
fn header_narrow_terminal() {
1387+
bat()
1388+
.arg("--terminal-width=30")
1389+
.arg("--decorations=always")
1390+
.arg("this-file-path-is-really-long-and-would-have-broken-the-layout-of-the-header.txt")
1391+
.assert()
1392+
.success()
1393+
.stdout(
1394+
"\
1395+
─────┬────────────────────────
1396+
│ File: this-file-path-is
1397+
│ -really-long-and-would-
1398+
│ have-broken-the-layout-
1399+
│ of-the-header.txt
1400+
─────┼────────────────────────
1401+
1 │ The header is not broke
1402+
│ n
1403+
─────┴────────────────────────
1404+
",
1405+
)
1406+
.stderr("");
1407+
}
1408+
1409+
#[test]
1410+
fn header_very_narrow_terminal() {
1411+
bat()
1412+
.arg("--terminal-width=10")
1413+
.arg("--decorations=always")
1414+
.arg("this-file-path-is-really-long-and-would-have-broken-the-layout-of-the-header.txt")
1415+
.assert()
1416+
.success()
1417+
.stdout(
1418+
"\
1419+
──────────
1420+
File: this
1421+
-file-path
1422+
-is-really
1423+
-long-and-
1424+
would-have
1425+
-broken-th
1426+
e-layout-o
1427+
f-the-head
1428+
er.txt
1429+
──────────
1430+
The header
1431+
is not br
1432+
oken
1433+
──────────
1434+
",
1435+
)
1436+
.stderr("");
1437+
}
1438+
13841439
#[test]
13851440
#[cfg(feature = "git")] // Expected output assumes git is enabled
13861441
fn header_default() {

0 commit comments

Comments
 (0)