Implemented show() interface for Juno, Jupyter, etc.

This commit is contained in:
Giorgio Calderone 2020-04-17 14:57:16 +02:00
parent 7759d0fd9e
commit d94e491522

View File

@ -5,7 +5,7 @@ using REPL, ReplMaker
import Base.reset import Base.reset
import Base.write import Base.write
import Base.display import Base.show
export session_names, dataset_names, palette_names, linetypes, palette, export session_names, dataset_names, palette_names, linetypes, palette,
terminal, terminals, test_terminal, terminal, terminals, test_terminal,
@ -18,6 +18,17 @@ export session_names, dataset_names, palette_names, linetypes, palette,
# │ User data representation │ # │ User data representation │
# ╰───────────────────────────────────────────────────────────────────╯ # ╰───────────────────────────────────────────────────────────────────╯
# --------------------------------------------------------------------- # ---------------------------------------------------------------------
"""
SessionID
A structure identifying a specific session. Used in the `show` interface.
"""
struct SessionID
sid::Symbol
dump::Bool
end
""" """
Dataset Dataset
@ -121,7 +132,7 @@ mutable struct PlotElement
end end
function display(v::PlotElement) function show(v::PlotElement)
if isa(v.data, DatasetText) if isa(v.data, DatasetText)
data = "DatasetText" data = "DatasetText"
elseif isa(v.data, DatasetBin) elseif isa(v.data, DatasetBin)
@ -134,9 +145,9 @@ function display(v::PlotElement)
name=v.name, data, plot=plot) name=v.name, data, plot=plot)
end end
function display(v::Vector{PlotElement}) function show(v::Vector{PlotElement})
for p in v for p in v
display(p) show(p)
println() println()
end end
end end
@ -515,6 +526,13 @@ function GPSession(sid::Symbol)
gpexec(out, l) gpexec(out, l)
end end
# If running in IJulia or Juno set the unknown terminal (trick
# copied from Gaston.jl)
if (isdefined(Main, :IJulia) && Main.IJulia.inited) ||
(isdefined(Main, :Juno) && Main.Juno.isactive())
gpexec(out, "set term unknown")
end
# Set window title (if not already set) # Set window title (if not already set)
term = writeread(out, "print GPVAL_TERM")[1] term = writeread(out, "print GPVAL_TERM")[1]
if term in ("aqua", "x11", "qt", "wxt") if term in ("aqua", "x11", "qt", "wxt")
@ -1211,7 +1229,7 @@ function driver(_args...; is3d=false)
if length(_args) == 0 if length(_args) == 0
gp = getsession() gp = getsession()
execall(gp) execall(gp)
return nothing return SessionID(gp.sid, doDump)
end end
(sid, doReset, doDump, elems) = parseArguments(_args...) (sid, doReset, doDump, elems) = parseArguments(_args...)
@ -1225,7 +1243,7 @@ function driver(_args...; is3d=false)
end end
end end
elems = elems[sortperm(getfield.(elems, :mid))] elems = elems[sortperm(getfield.(elems, :mid))]
# display(elems) # debug # show(elems) # debug
# Set dataset names and send them to gnuplot process # Set dataset names and send them to gnuplot process
for elem in elems for elem in elems
@ -1267,9 +1285,8 @@ function driver(_args...; is3d=false)
end end
end end
# (doDump) && (execall(gp)) (doDump) && (execall(gp))
return SessionID(gp.sid, doDump)
return gp
end end
@ -1466,6 +1483,7 @@ save(sid::Symbol, file::AbstractString; kw...) = savescript(getsession(sid), fil
# ╰───────────────────────────────────────────────────────────────────╯ # ╰───────────────────────────────────────────────────────────────────╯
# -------------------------------------------------------------------- # --------------------------------------------------------------------
#=
# Define a display that will be used when Gnuplot.jl is used # Define a display that will be used when Gnuplot.jl is used
# in the Julia REPL (see PGFPlotsX.jl). # in the Julia REPL (see PGFPlotsX.jl).
struct GnuplotDisplay <: AbstractDisplay end struct GnuplotDisplay <: AbstractDisplay end
@ -1482,19 +1500,26 @@ function Base.display(d::GnuplotDisplay, gp::Session)
execall(gp) execall(gp)
return return
end end
function Base.show(io::IO, ::MIME"image/svg+xml", gp::Session) =#
Base.show(gp::SessionID) = nothing
Base.show(io::IO, gp::SessionID) = nothing
function Base.show(io::IO, ::MIME"image/svg+xml", gp::SessionID)
if gp.dump
tmpfile = tempname()*".svg" tmpfile = tempname()*".svg"
execall(gp; output=tmpfile, term="svg") save(gp.sid; term="svg", output=tmpfile)
write(io, read(tmpfile)) write(io, read(tmpfile))
rm(tmpfile; force=true) rm(tmpfile; force=true)
return
end end
function Base.show(io::IO, ::MIME"image/png", gp::Session) nothing
end
function Base.show(io::IO, ::MIME"image/png", gp::SessionID)
if gp.dump
tmpfile = tempname()*".png" tmpfile = tempname()*".png"
execall(gp; output=tmpfile, term="png") save(gp.sid; output=tmpfile, term="pngcairo")
write(io, read(tmpfile)) write(io, read(tmpfile))
rm(tmpfile; force=true) rm(tmpfile; force=true)
return end
nothing
end end
# ╭───────────────────────────────────────────────────────────────────╮ # ╭───────────────────────────────────────────────────────────────────╮