-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
make print_with_color emit starting and terminating ANSI sequences on each line #25289
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
base/util.jl
Outdated
| iob = IOBuffer() | ||
| for line in lines | ||
| first || print(iob, '\n') | ||
| first = false |
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.
Perhaps check if line is empty here and continue to not write sequences on empty lines.
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.
Done
9c67cc9 to
4093381
Compare
vtjnash
left a comment
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 does sound like a good idea. It would also help any time formatting gets messed up in the middle of a line, by ensuring that the formatting gets reset for the next line.
base/util.jl
Outdated
| iscolor && print(iob, get(text_colors, color, color_normal)) | ||
| print(iob, line) | ||
| iscolor && color != :nothing && print(iob, get(disable_text_style, color, text_colors[:default])) | ||
| iscolor && (bold || color == :bold) && print(iob, disable_text_style[:bold]) |
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.
it seems silly to copy line to iob one at a time if !iscolor
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.
Updated with
if !iscolor
print(io, str)
else
...
4093381 to
b7237d4
Compare
|
Good to go? |
base/util.jl
Outdated
| else | ||
| lines = split(str, '\n') | ||
| first = true | ||
| iob = IOBuffer() |
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.
Can we re-use buf here since it was just emptied?
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.
Yes, done.
base/util.jl
Outdated
| first || print(iob, '\n') | ||
| first = false | ||
| isempty(line) && continue | ||
| iscolor && bold && print(iob, text_colors[:bold]) |
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.
iscolor is always true in this loop, 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.
Indeed, removed the checks.
base/util.jl
Outdated
| iscolor && print(iob, get(text_colors, color, color_normal)) | ||
| print(iob, line) | ||
| iscolor && color != :nothing && print(iob, get(disable_text_style, color, text_colors[:default])) | ||
| iscolor && (bold || color == :bold) && print(iob, disable_text_style[:bold]) |
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.
Won't the color == :bold case be handled by the previous line? It seems like you are printing disable_text_style[:bold] twice for color == :bold.
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 is true. Fixed. Also made it not double print when passing both :color = bold and bold = true.
b7237d4 to
47fa29a
Compare
|
Updated from @stevengj's review |
base/util.jl
Outdated
| first || print(buf, '\n') | ||
| first = false | ||
| isempty(line) && continue | ||
| bold && color != :bold && print(buf, text_colors[:bold]) |
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.
Seems better to just add a
bold && color == :bold && (color = :nothing)
line above and then you can just use bold && print(...) in the loop.
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.
Alright, did this and shoved the dictionary lookups outside the loop.
base/util.jl
Outdated
| enable_ansi = get(text_colors, color, text_colors[:default]) * | ||
| (bold ? text_colors[:bold] : "") | ||
| disable_ansi = get(disable_text_style, color, text_colors[:default]) * | ||
| (bold ? disable_text_style[:bold] : "") |
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.
Does it matter if you don't end in the opposite order? i.e. if you do red*bold does it need to end with disablebold*defaultcolor?
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.
It doesn't matter but changed it anyway. It seems nicer.
base/util.jl
Outdated
| (bold ? disable_text_style[:bold] : "") | ||
| lines = split(str, '\n') | ||
| first = true | ||
| for line in lines |
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.
for line in split(str, '\n') would make it clearer that the lines variable is not used elsewhere.
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.
Done
|
Oh ho, very nice 👍 |
Fix for log colorization test due to merge overlap with #25289
This is useful if you for example want to print something before each line of colored text.
For example:
Before this PR this will show
now it shows: