From 2472db66ebb4b4d0033727d981c59b09a16fc2d9 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Apr 2025 17:09:51 +0200 Subject: [PATCH 1/7] Fix printing of multiline values --- src/ProgressMeter.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index 15a9e4a..dadc215 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -584,7 +584,7 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate 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. - 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) From 29d27a4d0141815f19e80ca5779e6ad29edffe70 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Apr 2025 17:17:10 +0200 Subject: [PATCH 2/7] better solution --- src/ProgressMeter.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index dadc215..0c0a5bd 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -580,7 +580,13 @@ 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 = string(value, color=true) + 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. From 4b5745cfd69a70204cb03be1de122af1f0f370b4 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Apr 2025 17:47:09 +0200 Subject: [PATCH 3/7] fix printing of single line value non-UnicodePlot values --- src/ProgressMeter.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index 0c0a5bd..af5a2a6 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -580,7 +580,10 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate p.numprintedvalues = 0 for (name, value) in showvalues - string_value = string(value, color=true) + string_value = let io = PipeBuffer() + show(IOContext(io, :color => true), value) + "\e[0m" * read(io, String) + end if countlines(IOBuffer(string_value)) > 1 # Multiline objects should go on their own line so their # alignment doesn't get messed up From 16a2279026f4bc1044afb3f198d9a790e27f0a30 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Apr 2025 19:00:38 +0200 Subject: [PATCH 4/7] check if the value is a string, and if so, display as is. --- src/ProgressMeter.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index af5a2a6..124955b 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -580,9 +580,13 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate p.numprintedvalues = 0 for (name, value) in showvalues - string_value = let io = PipeBuffer() - show(IOContext(io, :color => true), value) - "\e[0m" * read(io, String) + string_value = if value isa AbstractString + value + else + let io = PipeBuffer() + show(IOContext(io, :color => true), value) + "\e[0m" * read(io, String) + end end if countlines(IOBuffer(string_value)) > 1 # Multiline objects should go on their own line so their From 84fa6ea6e277dd4cb3c321d284af4ccf0ec0d4e2 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 2 Apr 2025 22:20:07 +0200 Subject: [PATCH 5/7] don't apply blue formatting to String inputs --- src/ProgressMeter.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index 124955b..ffc9e6a 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -580,12 +580,12 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate p.numprintedvalues = 0 for (name, value) in showvalues - string_value = if value isa AbstractString + string_value = "\e[0m" * if value isa AbstractString value else let io = PipeBuffer() show(IOContext(io, :color => true), value) - "\e[0m" * read(io, String) + read(io, String) end end if countlines(IOBuffer(string_value)) > 1 From e8166129ae26a1e05139af8133a3de7965499ff9 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 5 Apr 2025 00:15:54 +0200 Subject: [PATCH 6/7] Be more defensive about String types, use `repr` for all else --- src/ProgressMeter.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index ffc9e6a..8bb5353 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -580,13 +580,10 @@ function printvalues!(p::AbstractProgress, showvalues; color = :normal, truncate p.numprintedvalues = 0 for (name, value) in showvalues - string_value = "\e[0m" * if value isa AbstractString + string_value = "\e[0m" * if value isa Union{String, SubString{String}} value else - let io = PipeBuffer() - show(IOContext(io, :color => true), value) - read(io, String) - end + repr("text/plain", value; context=IOContext(PipeBuffer(), :color => true)) end if countlines(IOBuffer(string_value)) > 1 # Multiline objects should go on their own line so their From f452210d3180002dd2b15b625719d7953e4c4fc6 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 5 Apr 2025 11:43:57 +0200 Subject: [PATCH 7/7] add UnicodePlot and StyledString tests --- Project.toml | 4 +++- test/test_showvalues.jl | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index f3fac32..12933e1 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/test/test_showvalues.jl b/test/test_showvalues.jl index a91416d..be6b1cf 100644 --- a/test/test_showvalues.jl +++ b/test/test_showvalues.jl @@ -1,3 +1,5 @@ +using StyledStrings, UnicodePlots + for ijulia_behavior in [:warn, :clear, :append] ProgressMeter.ijulia_behavior(ijulia_behavior) @@ -111,4 +113,19 @@ for i in 1:50 sleep(0.1) end -end # if \ No newline at end of file +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