Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last decoration line is not formatted properly with --wrap never #299

Closed
Rogach opened this issue Sep 10, 2018 · 5 comments
Closed

last decoration line is not formatted properly with --wrap never #299

Rogach opened this issue Sep 10, 2018 · 5 comments
Labels
bug Something isn't working

Comments

@Rogach
Copy link
Contributor

Rogach commented Sep 10, 2018

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
───────┴─────────────────────────────────
@Rogach
Copy link
Contributor Author

Rogach commented Sep 10, 2018

I'm using 0.6.1 on Arch Linux:

$ bat --version
bat 0.6.1

@Rogach
Copy link
Contributor Author

Rogach commented Sep 10, 2018

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!

@sharkdp sharkdp added the bug Something isn't working label Sep 10, 2018
@sharkdp
Copy link
Owner

sharkdp commented Sep 10, 2018

Thank you for reporting this!

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

@Rogach
Copy link
Contributor Author

Rogach commented Sep 10, 2018

Here you go: #300

@Rogach Rogach closed this as completed Sep 10, 2018
sharkdp pushed a commit that referenced this issue Sep 10, 2018
@sharkdp
Copy link
Owner

sharkdp commented Sep 12, 2018

Released in v0.7.0.

Enselic added a commit to Enselic/bat that referenced this issue Dec 21, 2020
This is a regression test for the fix for issue sharkdp#299. If that fix is
reverted, currently only one test ('header_padding') fails. But that
test is for a different use case, so add a dedicated regression test for
the particular use case issue sharkdp#299 is about.
sharkdp pushed a commit that referenced this issue Dec 21, 2020
This is a regression test for the fix for issue #299. If that fix is
reverted, currently only one test ('header_padding') fails. But that
test is for a different use case, so add a dedicated regression test for
the particular use case issue #299 is about.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants