-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Adding line number printing when output is piped out #2983
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
Adding line number printing when output is piped out #2983
Conversation
tests/integration_tests.rs
Outdated
| .assert() | ||
| .success() | ||
| .stdout("dummy content\n"); | ||
| .stdout(" 1 dummy content\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious, is this really the expected behavior? The test invokes bat without any line number style component arguments. I think it should stay as it was before - according to the issue being solved, line numbers should only be displayed when piping when the command line argument was explicitly passed, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!
I added a check for handle type here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't pass the test now, it seems like there may be an issue with the filename and the is_terminal() function or something like that
fn is_terminal(&self) -> bool
───────────────────────────────────────────
Returnstrueif the descriptor/handle refers to a terminal/tty.On platforms where Rust does not know how to detect a terminal yet, this will return
false. This will also returnfalseif an unexpected error occurred, such as from
passing an invalid file descriptor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debugging it shows self.config.style_components.numbers() returns true, is there a different way to know if line numbers are requested that i'm missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My guess would be the following:
The SimplePrinter is used whenever stdout is not interactive, --color is not set to always, --decorations is not set to always and --force-colorization is not set either (see app.rs, config.rs and controller.rs). --style controls which style components (such as header or line numbers) are used. The default for --style is changes,grid,header-filename,numbers,snip. This default currently stays the same even if loop_through == true (which means that SimplePrinter will be used). Currently, SimplePrinter just does not care about --style.
So, when loop_through == true, the default for --style should be plain instead. Then your changes should work since they do when you specify --style=plain explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the tip @einfachIrgendwer0815!
I updated the code and pushed the changes, now all the tests pass correctly and I removed all the integration test changes which were not necessary.
I also added a test for this specific case :)
…:domenicomastrangelo/bat into 2935-compatibility-issue-with-cat-piping
…:domenicomastrangelo/bat into 2935-compatibility-issue-with-cat-piping
|
I think I've found a simpler approach. The following should do the trick: diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs
index 8f69870f..cf385c88 100644
--- a/src/bin/bat/app.rs
+++ b/src/bin/bat/app.rs
@@ -251,9 +251,10 @@ impl App {
loop_through: !(self.interactive_output
|| self.matches.get_one::<String>("color").map(|s| s.as_str()) == Some("always")
|| self
.matches
.get_one::<String>("decorations")
.map(|s| s.as_str())
== Some("always")
- || self.matches.get_flag("force-colorization")),
+ || self.matches.get_flag("force-colorization")
+ || self.matches.get_flag("number")),
tab_width: selfThat way, whenever |
|
I certainly prefer things to be simple, thanks for the insight. So this PR is not needed then? I'll close it but let me know if I misunderstood something. |
|
I just came across this via #3316 and repeated the same idea in #3438 🙃 The issue reported in #2935 which remains in #3316 is that It looks like a proposal was given above to continue this work (by editing or shipping as a fresh PR):
The logic for this now lives in the Lines 179 to 189 in a730eaa
I can put that in a fresh PR (#3438), it sounds like this is a viable approach That is the same idea I took away from reading through, emphasis added:
Originally posted by @lmmx in #3316
|
Updating
print_line function inSimplePrinter to use line numbers when printing a line if line numbers are desired (self.config.style_components.numbers() / StyleComponent::LineNumbers).This was a compatibility issue with cat until now as describe in issue #2935.
Updating of some integration tests was necessary as now we expect the output to contain line numbers.