support sixel

This commit is contained in:
t-bltg 2021-12-19 22:29:54 +01:00
parent 9ff65b4003
commit 65e3dcf3ad
2 changed files with 55 additions and 48 deletions

View File

@ -114,7 +114,7 @@ function addUnicodeSeries!(
elseif st == :spy elseif st == :spy
return UnicodePlots.spy(series[:z].surf; kw...) return UnicodePlots.spy(series[:z].surf; kw...)
elseif st == :image elseif st == :image
return UnicodePlots.imshow(series[:z].surf; kw...) return UnicodePlots.image(series[:z].surf; kw...)
end end
series_kw = (;) series_kw = (;)
@ -195,53 +195,61 @@ Base.show(io::IO, plt::Plot{UnicodePlotsBackend}) = _show(io, MIME("text/plain")
function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
unicodeplots_rebuild(plt) unicodeplots_rebuild(plt)
nr, nc = size(plt.layout) nr, nc = size(plt.layout)
lines_colored = Array{Union{Nothing,Vector{String}}}(undef, nr, nc) if nr == 1 && nc == 1 # fast path
lines_uncolored = copy(lines_colored) for p in plt.o
l_max = zeros(Int, nr) show(io, p)
w_max = zeros(Int, nc)
buf = IOBuffer()
cbuf = IOContext(buf, :color => true)
re_col = r"\x1B\[[0-9;]*[a-zA-Z]"
sps = 0
for r in 1:nr
lmax = 0
for c in 1:nc
l = plt.layout[r, c]
if l isa GridLayout && size(l) != (1, 1)
@error "UnicodePlots: complex 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, re_col => "")
lines_colored[r, c] = lc = split(colored, "\n")
lines_uncolored[r, c] = lu = split(uncolored, "\n")
lmax = max(length(lc), lmax)
w_max[c] = max(maximum(length.(lu)), w_max[c])
end
end
end
l_max[r] = lmax
end
empty = String[' '^w for w in w_max]
for r in 1:nr
for n in 1:l_max[r]
for c in 1:nc
pre = c == 1 ? '\0' : ' '
lc = lines_colored[r, c]
if lc === nothing || length(lc) < n
print(io, pre, empty[c])
else
lu = lines_uncolored[r, c]
print(io, pre, lc[n], ' '^(w_max[c] - length(lu[n])))
end
end
println(io) println(io)
end end
r < nr && println(io) else
# FIXME: sixel unsupported
lines_colored = Array{Union{Nothing,Vector{String}}}(undef, nr, nc)
lines_uncolored = copy(lines_colored)
l_max = zeros(Int, nr)
w_max = zeros(Int, nc)
buf = IOBuffer()
cbuf = IOContext(buf, :color => true)
re_col = r"\x1B\[[0-9;]*[a-zA-Z]"
sps = 0
for r in 1:nr
lmax = 0
for c in 1:nc
l = plt.layout[r, c]
if l isa GridLayout && size(l) != (1, 1)
@error "UnicodePlots: complex 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, re_col => "")
lines_colored[r, c] = lc = split(colored, "\n")
lines_uncolored[r, c] = lu = split(uncolored, "\n")
lmax = max(length(lc), lmax)
w_max[c] = max(maximum(length.(lu)), w_max[c])
end
end
end
l_max[r] = lmax
end
empty = String[' '^w for w in w_max]
for r in 1:nr
for n in 1:l_max[r]
for c in 1:nc
pre = c == 1 ? '\0' : ' '
lc = lines_colored[r, c]
if lc === nothing || length(lc) < n
print(io, pre, empty[c])
else
lu = lines_uncolored[r, c]
print(io, pre, lc[n], ' '^(w_max[c] - length(lu[n])))
end
end
println(io)
end
r < nr && println(io)
end
end end
nothing nothing
end end

View File

@ -1286,7 +1286,6 @@ _backend_skips = Dict(
:inspectdr => [4, 6, 10, 22, 24, 28, 30, 38, 43, 45, 47, 48, 49, 50, 51, 55, 56], :inspectdr => [4, 6, 10, 22, 24, 28, 30, 38, 43, 45, 47, 48, 49, 50, 51, 55, 56],
:unicodeplots => [ :unicodeplots => [
5, # limits issue 5, # limits issue
6, # embedded images unsupported
16, # nested layout unsupported 16, # nested layout unsupported
21, # custom markers unsupported 21, # custom markers unsupported
22, # contours unsupported 22, # contours unsupported
@ -1302,7 +1301,7 @@ _backend_skips = Dict(
47, # mesh3D unsupported 47, # mesh3D unsupported
49, # polar heatmap 49, # polar heatmap
50, # 3D surface unsupported 50, # 3D surface unsupported
51, # embedded images unsupported 51, # drawing on top of image unsupported
52, # 3D quiver unsupported 52, # 3D quiver unsupported
55, # 3D unsupported 55, # 3D unsupported
56, # barplots 56, # barplots