From 2dc812aa1513975947287b0eb665c7b157b7f4a6 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Wed, 1 Sep 2021 11:26:29 +0200 Subject: [PATCH] UnicodePlots: support layout (#3787) --- src/backends/unicodeplots.jl | 50 +++++++++++++++++++++++++++++++++--- src/examples.jl | 9 +++---- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index f65921ed..f62b2a5a 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -166,13 +166,55 @@ function png(plt::Plot{UnicodePlotsBackend}, fn::AbstractString) end # ------------------------------- +Base.show(plt::Plot{UnicodePlotsBackend}) = _show(stdout, MIME("text/plain"), plt) function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) unicodeplots_rebuild(plt) - n = length(plt.o) - for (i, p) in enumerate(plt.o) - show(io, p) - i < n && println(io) + nr, nc = size(plt.layout) + lines_colored = Array{Union{Nothing,Vector{String}}}(undef, nr, nc) + lines_uncolored = copy(lines_colored) + buf = IOBuffer() + cbuf = IOContext(buf, :color => true) + sps = wmax = 0 + l_max = zeros(Int, nr) + for r in 1:nr + lmax = 0 + for c in 1:nc + l = plt.layout[r, c] + if l isa GridLayout + @error "UnicodePlots: nested layout is currently unsupported !" + else + if get(l.attr, :blank, false) + lines_colored[r, c] = lines_uncolored[r, c] = nothing + else + sp = plt.o[sps += 1] + show(cbuf, sp) + colored = String(take!(buf)) + uncolored = replace(colored, r"\x1B\[[0-9;]*[a-zA-Z]" => "") + lines_colored[r, c] = lc = split(colored, "\n") + lines_uncolored[r, c] = lu = split(uncolored, "\n") + lmax = max(length(lc), lmax) + wmax = max(maximum(length.(lu)), wmax) + end + end + end + l_max[r] = lmax + end + for r in 1:nr + for n in 1:l_max[r] + for c in 1:nc + pre = c == 1 ? '' : ' ' + if (lc = lines_colored[r, c]) !== nothing + length(lc) < n && continue + lu = lines_uncolored[r, c] + print(io, pre, lc[n], ' '^(wmax - length(lu[n]))) + else + print(io, pre, ' '^wmax) + end + end + println(io) + end + r < nr && println(io) end nothing end diff --git a/src/examples.jl b/src/examples.jl index 5ebbf5e5..fbd434ac 100644 --- a/src/examples.jl +++ b/src/examples.jl @@ -1253,16 +1253,15 @@ _backend_skips = Dict( 6, # embedded images unsupported 10, # histogram2d 13, # markers unsupported - 16, # subplots unsupported - 17, # grid layout unsupported + 16, # nested layout unsupported 20, # annotations unsupported 21, # custom markers unsupported 22, # contours unsupported 24, # 3D unsupported - 26, # subplots unsupported - 29, # layout unsupported + 26, # nested layout unsupported + 29, # nested layout unsupported 33, # grid lines unsupported - 34, # framestyles unsupported + 34, # framestyle unsupported 37, # ribbons / filled unsupported 38, # histogram2D 43, # heatmap with DateTime