-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
fix: query stdout terminal size to see if the output gose to a tty. #438
Conversation
`terminal_size::terminal_size()` is used to determine if stdout is connected to a real tty/pty. In previous version of `terminal-size`, this method only queries the terminal size for `stdout`. However, it is changed in the following commit eminence/terminal-size@08f0e73 Now the method will use `stdout, stdin, stderr`, trying its best to determine the terminal size. This cannot be used to determine if `eza` output is piped or redirected. We should use `terminal_size_using_fd` on `stdout.as_raw_fd` instead. Resolves eza-community#434 Fixes eza-community#433
This looks good, Thanks for the contribution! I see that this causes some issues on Windows though, take a look at the test results and it shouldn't be too hard to set up a solution. If you don't have access to a windows machine, I can try to grab one from work tomorrow and figure it out. |
Sorry, I don't have a machine with MS Windows installed. With that said, I can look into the document of |
It looks like it even suggests a solution in the error output. No worries, I can get a windows box tomorrow and I'm sure it's just specifying a windows specific method. Shouldn't be too tricky. |
The previous commit 427f975 uses `terminal_size::terminal_size_using_fd` and `io::stdout().as_raw_fd()` which are only available on unix platform, creating compatibility issue for windows. In this commit, I leverage rust conditional compilation and `terminal_size::terminal_size_using_handle` to implement the same functionality for windows platform.
We use functions in this crate to get stdout handler for terminal size query.
CI/CD tasks passed, but I am uncertain about whether this works on real windows machine. |
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.
The code looks good to me, and testing seems to show it as working.
I'd prefer if we first got a windows user to test it out as well. Also, from #433 I got the impression that this wasn't documented that well, maybe it would be a good idea to update completions, man page, and readme to reflect this behavior.
Also, I'd like to pair this with a test case. I'm assuming something like testing eza --color=automatic --online > eza-out; cat ./eza-out
(as mentioned in #433) and ensuring it doesn't change following this PR would be an option, but I wonder if you had more ideas.
Hi @cafkafk , thanks for pointing out these needy updates. I didn't realize the need for including new testcases and improving documentation. |
No worries, take as much time as you need! If you have any questions, feel free to ask here, or pop into the matrix chat. |
@hehelego I'm just checking this out on Windows. As far as I can tell, redirecting to a file with and without |
Hi @daviessm , thanks for testing it on Windows. I am not sure what is the behavior on windows before this patch. But as long as this patch works as expected, I think we are fine. I spotted the unexpected behavior on Linux platform (see the related issue threads for details). I tried to correct the bug by query |
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.
Yeah, looks like the original code always worked for Windows and this doesn't break it so LGTM for Windows 👍
BTW, I fish shell has a special builtin function |
Great. I'll keep working on manpages improvement and test case introduction. |
Yep, once the |
I'll need some help. Looks like See assert-rs/snapbox#168 for related discussion. The OP switched to |
Interesting, I've never heard of it. I'm going to turn this into a discussion post, and look into snapbox. Feel free to come by the matrix channel if you want, we can see about how to proceed here particularly |
Wait, I think I've mis-read that issue. They actually build another testing framework based on |
I think we can move forward to merge this PR first. Finding a proper way to test eza's behavior when redirection and pipelining are involved should be a separate task. |
Why not use the trait IsTerminal? |
Thanks for pointing it our, exactly the thing we need. |
Previsouly we tried to use `terminal_size_using_fd` on unix, and `terminal_size_using_handle` on windows to detect if stdout is connected to a terminal. This can be done by using `std::io::IsTerminal` on `std::io::stdout`, eliminating the need of conditional compiling.
Update: I've come up with a plan to test this using |
I'll give the new code a whiz on Windows later today. |
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.
Windows still looks good
Yep. I find another API misuse: Line 56 in afe832b
The comment shows that we want only to query |
Another API misuse. `terminal_size::terminal_size()` now looks into `stdout`, `stderr` and `stdin` to determine the terminal size with best effort. This is desirable for TUI applications, but not for CLI applications. `eza` only cares about the size of the terminal which is connected to standard output. We change this to `terminal_size_with_fd(stdout.as_raw_fd)` on unix, and `terminal_size_with_handle(STD_OUTPUT_HANDLE)` on windows.
I cannot test this with You may try this code snippet: use std::io::Write;
let out = std::process::Command::new("bash")
.args(["-c", "/bin/ls --color=auto"])
.output()
.unwrap()
.stdout;
std::io::stdout().write_all(&out).unwrap();
Does anyone have any idea on how should we do this? |
Alright, this thread is becoming too long to read. I shall give a quick summary of the current progress:
|
`eza` manpage and `README` have been refering to this option as `--colo[u]r`. We should follow the tradition and keep the writing style consistent. Co-authored-by: MartinFillon <114775771+MartinFillon@users.noreply.github.com> Signed-off-by: hehelego <hahalegao@gmail.com>
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 can't push to your branch for some reason, but I rebased and added a breaking notifiers (!
and BREAKING CHANGE:
) to the first commit.
commit 9a3b1cbd66a07ae0208b99418db0bb2598303582
Author: hehelego <hahalegao@gmail.com>
Date: Wed Sep 27 18:05:39 2023 -0500
fix!: query stdout terminal size to see if the output goes to a tty.
`terminal_size::terminal_size()` is used to determine if stdout is
connected to a real tty/pty. In previous version of `terminal-size`,
this method only queries the terminal size for `stdout`.
However, it is changed in the following commit
https://github.com/eminence/terminal-size/commit/08f0e73926c11adc3105dbf4eb84dd8c9e6c873a
Now the method will use `stdout, stdin, stderr`, trying its best to
determine the terminal size. This cannot be used to determine if `eza`
output is piped or redirected.
We should use `terminal_size_using_fd` on `stdout.as_raw_fd` instead.
Resolves #434 Fixes #433
BREAKING CHANGE: This is a change in behaviour from before, and so we
have marked it as breaking. In practice, it probably won't break
anything except if you're doing something really weird (more power to you!).
You need to do that, and this should be good to ship today
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 just gonna fix this after merge, still needs a follow up with tests
terminal_size::terminal_size()
is used to determine if stdout is connected to a real tty/pty. In previous version ofterminal-size
, this method only queries the terminal size forstdout
. However, it is changed in the following commiteminence/terminal-size@08f0e73 Now the method will use
stdout, stdin, stderr
, trying its best to determine the terminal size. This cannot be used to determine ifeza
output is piped or redirected.We should use
terminal_size_using_fd
onstdout.as_raw_fd
instead.This patch makes
eza --color=auto
behaves likels --color=auto
.Resolves #434 Fixes #433
Also Resolves #441 as well.