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

Newline can be added even if --style=plain #1438

Closed
Enselic opened this issue Dec 16, 2020 · 2 comments · Fixed by #1440
Closed

Newline can be added even if --style=plain #1438

Enselic opened this issue Dec 16, 2020 · 2 comments · Fixed by #1440
Labels
bug Something isn't working

Comments

@Enselic
Copy link
Collaborator

Enselic commented Dec 16, 2020

In #975 @sharkdp said this:

The fact that bat -p prints a trailing newline (even if there is no newline at the end of the file) should be considered a bug, I believe (but we should check older tickets, if there was a reason for this behavior).

And I completely agree. The reason the artificial newline was added in the first place was to fix #299, but there should be no risk of regressions since --style=plain excludes any kind of decorations that bug was about.

Note that a pager such as less will add an artificial newline too, so one either has to disable paging or use a "pager" that does not add a newline to get the benefit of fixing this bug.

Step-by-step:

  1. echo -n nonewline > nonewline.txt
  2. bat --style=plain --paging=never nonewline.txt
  3. bat --style=plain --pager=cat nonewline.txt

Expected result:
After 2. and 3. the line shall not contain a newline. I personally use zsh to check this since by default zsh will print e.g. % as the last char if there is a missing newline.

Actual result:
A newline is printed with bat 0.17.1.

(I will soon open a PR with a proposed fix)

@Enselic Enselic added the bug Something isn't working label Dec 16, 2020
Enselic added a commit to Enselic/bat that referenced this issue Dec 16, 2020
This fixes sharkdp#1438.

Note however, that using a pager such as less will add a newline itself.
So to actually not print a newline for such files, you need to either
disable paging:

  bat --style=plain --paging=never no-newline-at-end-of-file.txt

or use a "pager" that does not add a newline:

  bat --style=plain --pager=cat no-newline-at-end-of-file.txt
@sharkdp
Copy link
Owner

sharkdp commented Dec 16, 2020

Thank you for taking the time to write this down in detail!

Enselic added a commit to Enselic/bat that referenced this issue Dec 19, 2020
Since it has a functional role, we can not just replace it, we must keep
it around. This also allows us to simplify the code slightly.

We must fix this before we fix sharkdp#1438 since otherwise the \n will be
missing with --style=plain, since we will stop adding it if it is
missing.
Enselic added a commit to Enselic/bat that referenced this issue Dec 19, 2020
This fixes sharkdp#1438.

Note however, that using a pager such as less will add a newline itself.
So to actually not print a newline for such files, you need to either
disable paging:

  bat --style=plain --paging=never no-newline-at-end-of-file.txt

or use a "pager" that does not add a newline:

  bat --style=plain --pager=cat no-newline-at-end-of-file.txt

Note that we also update syntax tests file since a bunch of them had
missing newlines on the last lines.
@sharkdp
Copy link
Owner

sharkdp commented Feb 28, 2021

Bugfix released in bat v0.18.

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

Successfully merging a pull request may close this issue.

2 participants