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/src/ProgressMeter.jl b/src/ProgressMeter.jl index 15a9e4a..8bb5353 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -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)) + 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. - 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) 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