Skip to content

Retry Fix printing of multiline values, and allow colors in showed values #345

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ julia = "1.6"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

[targets]
test = ["Test", "Random", "Distributed", "InteractiveUtils"]
test = ["Test", "Random", "Distributed", "InteractiveUtils", "StyledStrings", "UnicodePlots"]
14 changes: 12 additions & 2 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,21 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate
p.numprintedvalues = 0

for (name, value) in showvalues
msg = "\n " * lpad(string(name) * ": ", maxwidth+2+1) * string(value)
string_value = "\e[0m" * if value isa Union{String, SubString{String}}
value
else
repr("text/plain", value; context=IOContext(PipeBuffer(), :color => true))

Choose a reason for hiding this comment

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

Is this repr the cause of the explicit quotes showing up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Trying to show it like in the previous commit causes it to show up as AnnotatedString(" julia", ...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

end
if countlines(IOBuffer(string_value)) > 1
# Multiline objects should go on their own line so their
# alignment doesn't get messed up
string_value = "\n" * string_value
end
msg = "\n " * lpad(string(name) * ": ", maxwidth+2+1) * string_value
max_len = (displaysize(p.output)::Tuple{Int,Int})[2]
# I don't understand why the minus 1 is necessary here, but empircally
# it is needed.
Comment on lines 595 to 596

Choose a reason for hiding this comment

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

Does this "empirically it is needed" change at all with this modified calculation? Is there a test for this that verifies it is/is not needed?

Copy link
Contributor Author

@MasonProtter MasonProtter Apr 4, 2025

Choose a reason for hiding this comment

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

That's an old comment from before my PR. I did try some tests to change that value, and it causes the number of printed lines to shrink or expand each iteration if you change it.

Choose a reason for hiding this comment

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

Yeah I assumed it was old, I was just wondering if it was no longer relevant since it’s now a different method for counting lines

msg_lines = ceil(Int, (length(msg)-1) / max_len)
msg_lines = countlines(IOBuffer(msg))-1
if truncate && msg_lines >= 2
# For multibyte characters, need to index with nextind.
printover(p.output, msg[1:nextind(msg, 1, max_len-1)] * "…", color)
Expand Down
19 changes: 18 additions & 1 deletion test/test_showvalues.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using StyledStrings, UnicodePlots

for ijulia_behavior in [:warn, :clear, :append]

ProgressMeter.ijulia_behavior(ijulia_behavior)
Expand Down Expand Up @@ -111,4 +113,19 @@ for i in 1:50
sleep(0.1)
end

end # if
println("Testing showvalues with UnicodePlot")
prog = Progress(50; dt=1, desc="progress: ")
for i in 1:50
update!(prog, i; showvalues = [("A plot", lineplot(rand(10)))])
sleep(0.1)
end

println("Testing showvalues with StyledStrings")
prog = Progress(50; dt=1, desc="progress: ")
for i in 1:50
str = AnnotatedString(" julia", [(2:6, :face, rand((:magenta, :red, :blue, :green)))])
update!(prog, i; showvalues = [("A plot", str)])
sleep(0.1)
end

end # for
Loading