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

Don't add artificial newline to last line if --style=plain #1440

Conversation

Enselic
Copy link
Collaborator

@Enselic Enselic commented Dec 16, 2020

This fixes #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

@Enselic
Copy link
Collaborator Author

Enselic commented Dec 16, 2020

I'd say the fix definitely should include some integration tests too, but before I put too much time into that, I just wanted to check if this fix looks reasonable to you?

@sharkdp
Copy link
Owner

sharkdp commented Dec 16, 2020

Thank you for your contribution!

I'd say the fix definitely should include some integration tests too, but before I put too much time into that, I just wanted to check if this fix looks reasonable to you?

It does! :-)

The "Test with new syntaxes and themes" currently fails in the syntax regression tests, as there are some files without newlines at the end. This should be fixed anyway (trailing newlines should be added).

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.
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.
@Enselic Enselic force-pushed the fix-1438-newline-can-be-added-even-if-style-plain branch from 652a0b0 to 68d525c Compare December 19, 2020 17:24
@Enselic
Copy link
Collaborator Author

Enselic commented Dec 19, 2020

Just a quick status update. I've pushed a set of commits that (should) pass all existing tests. Feel free to code-review if you wish.

However, to add new tests for this usecase, I need to come up with a way to test tty output.

From what I can tell, only non-tty output is tested in tests/integration_tests.rs. And after a quick glance at the assert_cmd crate, tty output/simulation does not seem to be not supported.

So it seems to me as if the right course of action is to add support for tty-output-testing to assert_cmd, bump our dependency, and then add tests to this fix. So it will take a while to get in place.

If you have idea for other approaches, I am all ears.

@sharkdp
Copy link
Owner

sharkdp commented Dec 21, 2020

If you have idea for other approaches, I am all ears.

I don't think we need to mock a complete terminal to test this. We can force bat to run in "interactive mode" (as if it was connected to a terminal) by using either --color=always or --decorations=always.

I added a test that fails on master but succeeds on your branch.

The changes here look great - thank you very much!

@sharkdp sharkdp merged commit cc7b89f into sharkdp:master Dec 21, 2020
@Enselic
Copy link
Collaborator Author

Enselic commented Dec 21, 2020

Thanks a lot for the help with adding a test for this fix!

I found no dedicated regression test for the fix for #299, so I added such a test in PR #1445. The new test fails if the fix for #299 is reverted from master, but of course pass on unmodified master.

It is true that we can force bat options to behave as if a tty was used, but I still think tests for tty defaults has some merit. There is currently no test that fails if we mess up the code paths that depends on e.g. atty::is(Stream::Stdout) being true, though such regressions arguable would affect a lot of users, and the involved code is rather complex. Making such tests possible to write in a maintainable manner is a big topic of its own, so let's defer that discussion to some other occasion though :)

@sharkdp
Copy link
Owner

sharkdp commented Dec 21, 2020

I found no dedicated regression test for the fix for #299, so I added such a test in PR #1445. The new test fails if the fix for #299 is reverted from master, but of course pass on unmodified master.

👍

It is true that we can force bat options to behave as if a tty was used, but I still think tests for tty defaults has some merit. There is currently no test that fails if we mess up the code paths that depends on e.g. atty::is(Stream::Stdout) being true, though such regressions arguable would affect a lot of users, and the involved code is rather complex.

Interesting thoughts. I agree in general. It would be great to see an explicit example where such a test would be superior over forcing colorized/decorated output.. even if constructed.

Making such tests possible to write in a maintainable manner is a big topic of its own, so let's defer that discussion to some other occasion though :)

Yes, I think that should be discussed in a new ticket 👍

@Enselic Enselic deleted the fix-1438-newline-can-be-added-even-if-style-plain branch January 6, 2021 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Newline can be added even if --style=plain
2 participants