-
Notifications
You must be signed in to change notification settings - Fork 14
Use color if stdout is a tty #91
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
Conversation
|
Great idea. I’ve been thinking about doing this. Can you add a changelog entry? Also should we add a test for it? Maybe in the exe integration tests send at at four/stderr to a file and then read it back in and assert no control chars? |
|
Should we do this on all location where we pass terminal? Some places it's true and other it's false. |
|
The main interfaces that need it are DeadEnd.call and the script in exe/dead_end (I think). most places in the existing tests are explicitly disabling terminal mode because otherwise we have to strip control characters for assertions. |
It's quite commen for commands use check if the output is a terminal or for and use that to determine if it should print colors.
When you print these codes to an none interactive shell it looks like. This could be build tools integrated in ides or Ci.
```
4 describe Iso20022::Camt053 do
�[0m 223 describe "::Parser", :aggregate_failures do
�[0m❯ 376 �[1;3m fdescribe "DNB cryptic description" do
�[0m❯ 400 �[1;3m end
�[0m❯ 401 �[1;3m end
�[0m 535 end
�[0m 536 end
�[0m
```
|
Made some new changes, including changelog. Some other changed you'd like to see? |
|
Thanks, I'll merge and tweak it a bit. I really appreciate your help! Thanks for the contribution ❤️ |
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. Personal preference.
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. (Personal preference)
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. (Personal preference)
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. (Personal preference)
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. (Personal preference)
In #91 the behavior of dead_end is now optimized to output values based on if the io object is a TTY (interactive terminal) or not. This behavior is awesome. The way we were checking default values though introduces a subtle bug, in Ruby `nil` and `false` are usually assumed to have the same/similar behavior (being falsey). This updates the logic to use a `Object.new` value to indicate a default value rather than rely on specific contents. It will reduce the chance the programmer accidentally passes in a value that triggers the default behavior. I also REALLY wanted to find a way to assert the inverse of the test added in "passing --terminal will force color codes" but it turns out it's really hard (if not impossible). I asked on twitter as well https://twitter.com/schneems/status/1452814816456945672. Instead I added some unit tests to at least assert that calling DisplayInvalidBlocks with default args defaulted to `io.isatty`. I also got rid of the describe block for now as there's only one thing in there. It makes diffs a bit more muddled if the contents of the tests get moved around. (Personal preference)
|
I opened up #92 with some minor changes to your logic. I would love it if you could review that PR and let me know if you've got any feedback or questions. |
It's quite commen for commands use check if the output is a terminal or for and use that to determine if it should print colors.
When you print these codes to an none interactive shell it looks like. This could be build tools integrated in ides or Ci.
This fixes my case. It would proabaly be better if I spent a little more time and check each place its written. Like at https://github.com/zombocom/dead_end/blob/2e3807649d7f999cc3e42ebba0792f8701a79373/lib/dead_end/display_invalid_blocks.rb#L50 we could check .@io
What's you think? I wanted to open a minimal PR so check if you have any plans for this