MasonProtter / ReplMaker.jl

Simple API for building repl modes in Julia
Other
134 stars 14 forks source link

Attempt to enable custom replshow methods #16

Closed brenhinkeller closed 3 years ago

brenhinkeller commented 3 years ago

A potential solution to #14. Could use some code review as to whether this is really the best way to do this.

MasonProtter commented 3 years ago

I love this.

julia> begin
       backwards_show(io, M, x) = (show(io, M, x); println(io))
       backwards_show(io, M, v::Union{Vector, Tuple}) = (show(io, M, reverse(v)); println(io))

       using ReplMaker
       initrepl(Meta.parse,
           repl=enablecustomdisplay(Base.active_repl, backwards_show, stdout),
           prompt_text="reverse_julia> ",
           start_key=')',
           mode_name="reverse mode"
       )
       end
REPL mode reverse mode initialized. Press ) to enter and backspace to exit.
"Prompt(\"reverse_julia> \",...)"

reverse_julia> [1,2,3] .+ [4, 5, 6]
3-element Array{Int64,1}:
 9
 7
 5
MasonProtter commented 3 years ago

My only real comment is that maybe we should hide the creation of a new repl from enablecustomdisplay inside initrepl and then just have something like a show_function kwarg where you'd pass your own custom show?

brenhinkeller commented 3 years ago

Haha, the backwards Julia is awesome.

Yeah, I think we could definitely hide it inside initrepl. Should we also add an option to specify the iostream for custom displays? One thought would be having defaults along the lines of

show_function = nothing, 
show_function_io = stdout

for the input arguments, and then within initrepl having a conditional

if ~isnothing(show_function)
    repl = enablecustomdisplay(repl, show_function, show_function_io)
end

such that a REPL that chooses not to use a custom show_function never has to follow any of the modified code paths for a repl with specialdisplay set to an instance of the new CustomREPLDisplay type, and can just use the normal Base paths.

MasonProtter commented 3 years ago

Yeah, I like both suggestions.

brenhinkeller commented 3 years ago

I think this should do it. Forgot that 1.0 doesn't have isnothing so switched back to != nothing for the conditional

MasonProtter commented 3 years ago

Okay, this is great! Thanks Bernhin, sorry I haven't been able to be more responsive on this!

cscherrer commented 2 years ago

This looks really great, but I can't get it working. Am I doing something wrong?


julia> begin
          shortshow(io, M, x) = show(io, M, x)
          shortshow(io, M, x::AbstractFloat) = show(io, M, round(x, sigdigits=6))

          using ReplMaker
          initrepl(Meta.parse,
              repl=enablecustomdisplay(Base.active_repl, shortshow, stdout),
              prompt_text="jul> ",
              start_key=')',
              mode_name="short mode"
          )
       end
ERROR: UndefVarError: enablecustomdisplay not defined
Stacktrace:
 [1] top-level scope
   @ REPL[4]:6
cscherrer commented 2 years ago

Oh had to qualify it, ReplMaker.enablecustomdisplay(....)

cscherrer commented 2 years ago

Oh wait, still not working:

jul> rand(10)
10-element Vector{Float64}:
 0.8723316825095376
 0.3503081526581854
 0.05720077887177
 0.4643888227565177
 0.6009073227202386
 0.6850277646515196
 0.987656385904079
 0.5233587859229942
 0.6737826047067421
 0.6375802455593176

:(

brenhinkeller commented 2 years ago

How about for just rand()? If that works then the issue might be more just that shortshow only has a method for x::AbstractFloat, not x::Vector{AbstractFloat}, i.e.

julia> shortshow(x) = show(x)
shortshow (generic function with 3 methods)

julia> shortshow(x::Float64) = show(round(x, sigdigits=6))
shortshow (generic function with 4 methods)

julia> shortshow(rand())
0.289587
julia> shortshow(rand(10))
[0.0774374070966819, 0.8721497065887969, 0.48136047600885445, 0.6980769667264656, 0.4523895128942049, 0.0034268051073700345, 0.8219629020389327, 0.0310145862972252, 0.26521714774690597, 0.41117509046516765]
cscherrer commented 2 years ago

Think I got it!

julia> begin
          function shortshow(io::IO, m::MIME, x)
               io = IOContext(io, :compact => true)
               show(io, m, x)
          end
          using ReplMaker
          initrepl(Meta.parse,
              repl=ReplMaker.enablecustomdisplay(Base.active_repl, shortshow, stdout),
              prompt_text="jul> ",
              start_key=')',
              mode_name="short mode"
          )
       end
REPL mode short mode initialized. Press ) to enter and backspace to exit.
"Prompt(\"jul> \",...)"

julia> rand(4)
4-element Vector{Float64}:
 0.12203834605911146
 0.3569019357350327
 0.9973117461360539
 0.26904453456168487

jul> rand(4)
4-element Vector{Float64}:
 0.367775
 0.124696
 0.884562
 0.956584