Skip to content

Conversation

@KristofferC
Copy link
Member

This is useful if you for example want to print something before each line of colored text.

For example:

function boxify(io, str)
	lines = split(str, '\n')
	for (i, line) in enumerate(lines)
		i == 1 || print(io, '\n')
		if length(lines) == 1
			print(io, "[ ")
		elseif i == 1
			print(io, "┌ ")
		elseif i == length(lines)
			print(io, "└ ")
		else
			print(io, "│ ")
		end
		print(io, line)
	end
end

str = """
This is a string
that has many lines
we will print it
in color"""

io = IOBuffer(); print_with_color(:red, IOContext(io, :color => true), str)
str_colored = String(take!(io));
boxify(STDOUT, str_colored)

Before this PR this will show

screen shot 2017-12-27 at 23 53 31

now it shows:

screen shot 2017-12-27 at 23 52 55

@ararslan ararslan added the display and printing Aesthetics and correctness of printed representations of objects. label Dec 27, 2017
base/util.jl Outdated
iob = IOBuffer()
for line in lines
first || print(iob, '\n')
first = false
Copy link
Member Author

@KristofferC KristofferC Dec 28, 2017

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

@vtjnash vtjnash left a 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])
Copy link
Member

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

Copy link
Member Author

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
    ...

@KristofferC KristofferC force-pushed the kc/reset_newline_colors branch from 4093381 to b7237d4 Compare December 28, 2017 15:54
@KristofferC
Copy link
Member Author

Good to go?

base/util.jl Outdated
else
lines = split(str, '\n')
first = true
iob = IOBuffer()
Copy link
Member

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?

Copy link
Member Author

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])
Copy link
Member

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?

Copy link
Member Author

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])
Copy link
Member

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.

Copy link
Member Author

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.

@KristofferC KristofferC force-pushed the kc/reset_newline_colors branch from b7237d4 to 47fa29a Compare January 4, 2018 21:09
@KristofferC
Copy link
Member Author

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])
Copy link
Member

@stevengj stevengj Jan 4, 2018

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.

Copy link
Member Author

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] : "")
Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@StefanKarpinski StefanKarpinski merged commit 0a98aa9 into master Jan 6, 2018
@StefanKarpinski StefanKarpinski deleted the kc/reset_newline_colors branch January 6, 2018 23:38
@c42f
Copy link
Member

c42f commented Jan 7, 2018

Oh ho, very nice 👍

StefanKarpinski added a commit that referenced this pull request Jan 7, 2018
Fix for log colorization test due to merge overlap with #25289
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

display and printing Aesthetics and correctness of printed representations of objects.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants