Added terminal(), terminals() and splash() functions

This commit is contained in:
Giorgio Calderone 2020-03-25 18:48:23 +01:00
parent c5abde30ff
commit faa701af61

View File

@ -5,7 +5,7 @@ using StatsBase, ColorSchemes, ColorTypes, StructC14N, ReusePatterns
import Base.reset
import Base.write
export @gp, @gsp, save, linestyles, palette, contourlines, hist
export @gp, @gsp, save, linestyles, palette, contourlines, hist, terminal, terminals
# ╭───────────────────────────────────────────────────────────────────╮
# │ TYPE DEFINITIONS │
@ -61,6 +61,8 @@ const options = Options()
# │ LOW LEVEL FUNCTIONS │
# ╰───────────────────────────────────────────────────────────────────╯
version() = v"1.0-dev"
# ---------------------------------------------------------------------
"""
# gpversion
@ -70,6 +72,7 @@ const options = Options()
blocks).
"""
function gpversion()
options.dry && (return v"0.0.0")
icmd = `$(options.cmd) --version`
proc = open(`$icmd`, read=true)
@ -282,7 +285,6 @@ end
# ╰───────────────────────────────────────────────────────────────────╯
# ---------------------------------------------------------------------
function DrySession(sid::Symbol)
global options
(sid in keys(sessions)) && error("Gnuplot session $sid is already active")
out = DrySession(sid, Vector{DataSet}(), [SinglePlot()], 1)
sessions[sid] = out
@ -292,7 +294,6 @@ end
# ---------------------------------------------------------------------
function GPSession(sid::Symbol)
function readTask(sid, stream, channel)
global options
saveOutput = false
while isopen(stream)
@ -326,7 +327,6 @@ function GPSession(sid::Symbol)
return nothing
end
global options
gpversion()
session = DrySession(sid)
@ -370,7 +370,6 @@ end
# ---------------------------------------------------------------------
function getsession(sid::Symbol=options.default)
global options
if !(sid in keys(sessions))
if options.dry
DrySession(sid)
@ -400,7 +399,6 @@ end
"""
write(gp::DrySession, str::AbstractString) = nothing
function write(gp::GPSession, str::AbstractString)
global options
if options.verbose
printstyled(color=:light_yellow, "GNUPLOT ($(gp.sid)) $str\n")
end
@ -436,7 +434,6 @@ end
# ---------------------------------------------------------------------
writeread(gp::DrySession, str::AbstractString) = [""]
function writeread(gp::GPSession, str::AbstractString)
global options
verbose = options.verbose
options.verbose = false
@ -517,7 +514,6 @@ end
# ---------------------------------------------------------------------
function quit(gp::DrySession)
global options
delete!(sessions, gp.sid)
return 0
end
@ -939,10 +935,7 @@ end
Quit the session and the associated gnuplot process (if any).
"""
function quit(sid::Symbol)
global options
if !(sid in keys(sessions))
error("Gnuplot session $sid do not exists")
end
(sid in keys(sessions)) || (return 0)
return quit(sessions[sid])
end
@ -952,7 +945,6 @@ end
Quit all the sessions and the associated gnuplot processes.
"""
function quitall()
global options
for sid in keys(sessions)
quit(sid)
end
@ -994,11 +986,8 @@ function exec(gp::GPSession, command::String)
return join(answer, "\n")
end
function exec(s::String)
global options
exec(getsession(), s)
end
exec(sid::Symbol, s::String) = exec(getsession(sid), s)
exec(s::String) = exec(getsession(), s)
# --------------------------------------------------------------------
@ -1008,7 +997,6 @@ exec(sid::Symbol, s::String) = exec(getsession(sid), s)
Set verbose flag to `true` or `false` (default: `false`).
"""
function setverbose(b::Bool)
global options
options.verbose = b
end
@ -1084,7 +1072,6 @@ end
# return nothing
# end
# function repl()
# global options
# return repl(options.default)
# end
@ -1301,4 +1288,43 @@ function histo2segments(in_x, counts)
return (x, y)
end
# --------------------------------------------------------------------
terminals() = split(exec("print GPVAL_TERMINALS"), " ")
terminal() = exec("print GPVAL_TERM") * " " * exec("print GPVAL_TERMOPTIONS")
# --------------------------------------------------------------------
function splash(outputfile="")
quit(:splash)
gp = getsession(:splash)
if outputfile == ""
# Try to set a reasonably modern terminal. Setting the size
# is necessary for the text to be properly sized. The
# `noenhanced` option is required to display the "@" character
# (alternatively use "\\\\@", but it doesn't work on all
# terminals).
terms = terminals()
if "wxt" in terms
exec(gp, "set term wxt noenhanced size 600,300")
elseif "qt" in terms
exec(gp, "set term qt noenhanced size 600,300")
elseif "aqua" in terms
exec(gp, "set term aqua noenhanced size 600,300")
else
@warn "None of the `wxt`, `qt` and `aqua` terminals are available. Output may look strange.."
end
else
exec(gp, "set term unknown")
end
@gp :- :splash "set margin 0" "set border 0" "unset tics"
@gp :- :splash xr=[-0.3,1.7] yr=[-0.3,1.1]
@gp :- :splash "set origin 0,0" "set size 1,1"
@gp :- :splash "set label 1 at graph 1,1 right font 'Verdana,20' tc rgb '#4d64ae' ' Ver: " * string(version()) * "' "
@gp :- :splash "set arrow 1 from graph 0.05, 0.15 to graph 0.95, 0.15 size 0.2,20,60 noborder lw 9 lc rgb '#4d64ae'"
@gp :- :splash "set arrow 2 from graph 0.15, 0.05 to graph 0.15, 0.95 size 0.2,20,60 noborder lw 9 lc rgb '#4d64ae'"
@gp :- :splash ["0.35 0.65 @ 13253682'", "0.85 0.65 g 3774278", "1.3 0.65 p 9591203"] "w labels notit font 'Mono,160' tc rgb var"
(outputfile == "") || save(:splash, term="pngcairo transparent noenhanced size 600,300", output=outputfile)
nothing
end
end #module