Bat: last decoration line is not formatted properly with `--wrap never`

Created on 10 Sep 2018  Β·  5Comments  Β·  Source: sharkdp/bat

Example:

$ echo -n deadbeef > test.txt
$ bat --wrap=never test.txt 
───────┬─────────────────────────────────
       β”‚ File: test.txt
───────┼─────────────────────────────────
   1   β”‚ deadbeef───────┴─────────────────────────────────

Compare that with --wrap=character (same input file):

$ bat test.txt
───────┬─────────────────────────────────
       β”‚ File: test.txt
───────┼─────────────────────────────────
   1   β”‚ deadbeef
───────┴─────────────────────────────────
bug

Most helpful comment

It seems that this section is the culprit:
https://github.com/sharkdp/bat/blob/master/src/printer.rs#L230-L245

It simply prints the line as is, expecting that every line will be ended in a newline. But the last line can end without a newline, resulting in broken formatting.

It is possible to look at the last character and print the missing newline if there isn't one:

impl<'a> Printer for InteractivePrinter<'a> {
                     )).collect::<Vec<_>>()
                     .join("")
             )?;
+
+            if line.bytes().next_back() != Some(0x0a) {
+                write!(handle, "\n")?;
+            }
         } else {
             for &(style, region) in regions.iter() {
                 let mut ansi_iterator = AnsiCodeIterator::new(region);

If you are okay with such solution, I would be happy to create a pull request!

All 5 comments

I'm using 0.6.1 on Arch Linux:

$ bat --version
bat 0.6.1

It seems that this section is the culprit:
https://github.com/sharkdp/bat/blob/master/src/printer.rs#L230-L245

It simply prints the line as is, expecting that every line will be ended in a newline. But the last line can end without a newline, resulting in broken formatting.

It is possible to look at the last character and print the missing newline if there isn't one:

impl<'a> Printer for InteractivePrinter<'a> {
                     )).collect::<Vec<_>>()
                     .join("")
             )?;
+
+            if line.bytes().next_back() != Some(0x0a) {
+                write!(handle, "\n")?;
+            }
         } else {
             for &(style, region) in regions.iter() {
                 let mut ansi_iterator = AnsiCodeIterator::new(region);

If you are okay with such solution, I would be happy to create a pull request!

Thank you for reporting this!

Your fix looks good (I would prefer b'\n' instead of 0x0a) - a PR would be very much appreciated!

Here you go: #300

Released in v0.7.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

niedzielski picture niedzielski  Β·  3Comments

mjlbach picture mjlbach  Β·  3Comments

tbsvttr picture tbsvttr  Β·  3Comments

adamtabrams picture adamtabrams  Β·  3Comments

issmirnov picture issmirnov  Β·  3Comments