ReplMaker.jl icon indicating copy to clipboard operation
ReplMaker.jl copied to clipboard

Allow custom `show`

Open MasonProtter opened this issue 5 years ago • 4 comments

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.

MasonProtter avatar Oct 24 '20 19:10 MasonProtter

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

cscherrer avatar Oct 26 '21 14:10 cscherrer

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

MasonProtter avatar Oct 26 '21 16:10 MasonProtter

xref https://github.com/JuliaLang/julia/issues/42813

MasonProtter avatar Oct 26 '21 20:10 MasonProtter

Thanks! I appreciate your taking the time to dig into this :)

cscherrer avatar Oct 26 '21 20:10 cscherrer