UnicodePlots: png output

This commit is contained in:
t-bltg 2022-03-23 18:34:36 +01:00
parent 5c4fbc5e1a
commit b4e06bb6f0

View File

@ -254,36 +254,49 @@ end
# ------------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------------
# since this is such a hack, it's only callable using `png`...
# should error during normal `show`
function png(plt::Plot{UnicodePlotsBackend}, fn::AbstractString) function png(plt::Plot{UnicodePlotsBackend}, fn::AbstractString)
fn = addExtension(fn, "png") unicodeplots_rebuild(plt)
nr, nc = size(plt.layout)
@static if Sys.isapple() s1 = zeros(Int, nr, nc)
# make some whitespace and show the plot s2 = zeros(Int, nr, nc)
println("\n\n\n\n\n\n") canvas_type = nothing
gui(plt) imgs = []
sleep(0.5) sps = 0
# use osx screen capture when my terminal is maximized for r in 1:nr
# and cursor starts at the bottom (I know, right?) for c in 1:nc
run(`screencapture -R50,600,700,420 $fn`) if (l = plt.layout[r, c]) isa GridLayout && size(l) != (1, 1)
return error("Plots(UnicodePlots): complex nested layout is currently unsupported")
elseif Sys.islinux() else
run(`clear`) img = UnicodePlots.png_image(plt.o[sps += 1])
gui(plt) canvas_type = eltype(img)
win = if "WINDOWID" in keys(ENV) sz = size(img)
ENV["WINDOWID"] s1[r, c] = sz[1]
else s2[r, c] = sz[2]
readchomp(`xdotool getactivewindow`) push!(imgs, img)
end
end end
run(`import -window $win $fn`)
return
end end
if canvas_type !== nothing
error( rows = maximum(sum(s1; dims = 1))
"Can only savepng on MacOS or Linux with UnicodePlots " * cols = maximum(sum(s2; dims = 2))
"(though even then I wouldn't do it)", img = zeros(canvas_type, rows, cols)
) m1 = maximum(s1; dims = 2)
m2 = maximum(s2; dims = 1)
sps = 0
n1 = 1
for r in 1:nr
n2 = 1
for c in 1:nc
sp = imgs[sps += 1]
sz = size(sp)
img[n1:(n1 + (sz[1] - 1)), n2:(n2 + (sz[2] - 1))] = sp
n2 += m2[c]
end
n1 += m1[r]
end
UnicodePlots.FileIO.save(fn, img)
end
nothing
end end
# ------------------------------------------------------------------------------------------ # ------------------------------------------------------------------------------------------
@ -301,7 +314,6 @@ function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
i < n && println(io) i < n && println(io)
end end
else else
re_ansi = r"\e\[[0-9;]*[a-zA-Z]" # m: color, [a-zA-Z]: all escape sequences
have_color = Base.get_have_color() have_color = Base.get_have_color()
buf = IOContext(PipeBuffer(), :color => have_color) buf = IOContext(PipeBuffer(), :color => have_color)
lines_colored = Array{Union{Nothing,Vector{String}}}(undef, nr, nc) lines_colored = Array{Union{Nothing,Vector{String}}}(undef, nr, nc)
@ -312,9 +324,10 @@ function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
for r in 1:nr for r in 1:nr
lmax = 0 lmax = 0
for c in 1:nc for c in 1:nc
l = plt.layout[r, c] if (l = plt.layout[r, c]) isa GridLayout && size(l) != (1, 1)
if l isa GridLayout && size(l) != (1, 1) error(
@error "Plots(UnicodePlots): complex nested layout is currently unsupported" "Plots(UnicodePlots): complex nested layout is currently unsupported",
)
else else
if get(l.attr, :blank, false) if get(l.attr, :blank, false)
lines_colored[r, c] = lines_uncolored[r, c] = nothing lines_colored[r, c] = lines_uncolored[r, c] = nothing
@ -324,7 +337,7 @@ function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend})
colored = read(buf, String) colored = read(buf, String)
lines_colored[r, c] = lu = lc = split(colored, '\n') lines_colored[r, c] = lu = lc = split(colored, '\n')
if have_color if have_color
uncolored = replace(colored, re_ansi => "") uncolored = UnicodePlots.nocolor_string(colored)
lines_uncolored[r, c] = lu = split(uncolored, '\n') lines_uncolored[r, c] = lu = split(uncolored, '\n')
end end
lmax = max(length(lc), lmax) lmax = max(length(lc), lmax)