-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add simplest TTY detection for Colorize #4075
Add simplest TTY detection for Colorize #4075
Conversation
It renames `@on` to `@enabled` because `Colorize::Object` has `#on` method also, but `@on` is not related and its name does not make a sense.
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.
@makenowjust would you mind adding a doc sentence regarding that by default colorize is enabled if both STDOUT and STDERR are tty or if Colorize.enabled=
/ #toggle
is explicitly called?
Apologies for the delay on this. 🙏
If you won't be able let us know and I will pick it from here.
src/colorize.cr
Outdated
@@ -158,11 +158,13 @@ struct Colorize::Object(T) | |||
COLORS = %w(black red green yellow blue magenta cyan light_gray dark_gray light_red light_green light_yellow light_blue light_magenta light_cyan white) | |||
MODES = %w(bold bright dim underline blink reverse hidden) | |||
|
|||
class_property? enabled : Bool = STDOUT.tty? && STDERR.tty? |
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 think this class_property should belong to module Colorize
. I found it misleading to be inside the generic class.
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.
Sorry. It's my mistake. I fixed it and added doc comment to Colorize.enabled?
.
Just a question: what if I want to send output to a file and later show it colorized in another way but still recognizing the color codes? For example like what travis does (stores colored output and probably formats it in the web with JS). I don't understand why the Colorize should be enabled or disabled according to one possible output (STDOUT and STDERR). I understand that this is the most probable output, but this will lead to a lot of confusion and unexpected behaviour. I'd say to revert this merge, but I'd like to know others' opinions. |
For example: # foo.cr
require "colorize"
# I want to write "hello" in red to a file
File.write("file.txt", "hello".colorize.red)
puts "Done!" Now:
Totally unexpected behaviour. Colorize shouldn't be tied to STDOUT/STDERR. |
This PR change the default setting from enabled to enabled if TTY. Colorize was already global and not sensible to the targeted IO. I agree it is an issue it should be addressed (for example in playground you can send the log with ansi colors despite the tty). But I see that as a separate issue. |
For example this is with Ruby's colorize: require "colorize"
puts "hello".colorize(:red) It outputs "hello" in red to the console. But if we redirect the output to a file with @makenowjust What do you think about this? |
I agree with @asterite 's last comment, A use case would be to have a big colored output, and pipe it to $ ./some_big_colored_output | less -R The |
@asterite said:
But I use However I think I shouldn't have changed default behavior (backward compatibility is important and current behavior is difficult to understand). @bew's |
Simplest version of #3925.
It renames
@on
to@enabled
becauseColorize::Object
has#on
method also, but@on
is not related and its name does not make a sense.