ReplMaker.jl
ReplMaker.jl copied to clipboard
Allow custom `show`
It'd be good if the user could supply a custom function other than show, or thier own IO / MIME type to be used when show is called in a custom REPL mode.
I had some discussion in the linked PR, but asking in an issue probably makes more sense...
Should this work as a startup.jl? I've tried a few variations with no luck.
atreplinit() do repl
function compact_show(io::IO, m::MIME, x)
io = IOContext(io, :compact => true)
show(io, m, x)
end
try
@eval using ReplMaker
@async initrepl(Meta.parse,
repl=enablecustomdisplay(Base.active_repl, compact_show, stdout),
prompt_text="jul> ",
prompt_color=:magenta,
start_key=')',
mode_name="compact_mode"
)
catch
end
end
So there are a couple of errors being swallowed by the try / catch there. Here is how I would fix them:
atreplinit() do repl
function compact_show(io::IO, m::MIME, x)
io = IOContext(io, :compact => true)
show(io, m, x)
end
try
@eval using ReplMaker
@eval initrepl(Meta.parse,
prompt_text="jul> ",
prompt_color=:magenta,
start_key=')',
show_function=$compact_show,
mode_name="compact_mode"
)
catch e;
@warn e.msg
end
end
However, it seems that the enablecustomdisplay function wants to access the field repl.interface and repl.backendref, and these fields are undefined even when atreplinit runs. I'm not sure if this is intentional or not on the part of the julia devs, or if it's an upstream bug. I'll have to look into it, sorry.
In the meantime, you could put this in your startup.jl:
function enable_compact_mode()
function compact_show(io::IO, m::MIME, x)
io = IOContext(io, :compact => true)
show(io, m, x)
end
try
@eval begin
using ReplMaker
initrepl(Meta.parse,
prompt_text="jul> ",
prompt_color=:magenta,
start_key=')',
show_function=$compact_show,
mode_name="compact_mode"
)
end
catch e;
@warn e.msg
end
nothing
end
and then once your repl starts you can just do
julia> enable_compact_mode()
REPL mode compact_mode initialized. Press ) to enter and backspace to exit.
jul> 1.00000000000000000000000001
1.0
xref https://github.com/JuliaLang/julia/issues/42813
Thanks! I appreciate your taking the time to dig into this :)