Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom show #14

Open
MasonProtter opened this issue Oct 24, 2020 · 4 comments
Open

Allow custom show #14

MasonProtter opened this issue Oct 24, 2020 · 4 comments

Comments

@MasonProtter
Copy link
Owner

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.

@cscherrer
Copy link

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

@MasonProtter
Copy link
Owner Author

MasonProtter commented Oct 26, 2021

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
Copy link
Owner Author

xref JuliaLang/julia#42813

@cscherrer
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants