Skip to content

Commit

Permalink
REPL: allow tweaking the implicit IOContext of the REPL
Browse files Browse the repository at this point in the history
This adds an `iocontext::Dict{Symbol,Any}` field to `LineEditREPL`,
which can be initialized with `atreplinit`, and updated
interactively, e.g. `Base.active_repl.iocontext[:compact] = true`.

Fixes #20509.
  • Loading branch information
rfourquet committed Sep 25, 2019
1 parent 9a1dbc0 commit b386f5f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
27 changes: 26 additions & 1 deletion stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@ If it is enabled, you can try it out by pasting the code block above this paragr
the REPL. This feature does not work on the standard Windows command prompt due to its limitation
at detecting when a paste occurs.

Objects are printed at the REPL using the [`show`](@ref) function with a specific [`IOContext`](@ref).
In particular, the `:limit` attribute is set to `true`.
Other attributes can receive in certain `show` methods a default value if it's not already set,
like `:compact`.
It's possible to specify the attributes used by the REPL via the `Base.active_repl.iocontext`
dictionary (associating values to attributes).
For example:

```julia-repl
julia> rand(2, 2)
2×2 Array{Float64,2}:
0.8833 0.329197
0.719708 0.59114
julia> show(IOContext(stdout, :compact => false), "text/plain", rand(2, 2))
0.43540323669187075 0.15759787870609387
0.2540832269192739 0.4597637838786053
julia> Base.active_repl.iocontext[:compact] = false;
julia> rand(2, 2)
2×2 Array{Float64,2}:
0.2083967319174056 0.13330606013126012
0.6244375177790158 0.9777957560761545
```

### Help mode

When the cursor is at the beginning of the line, the prompt can be changed to a help mode by typing
Expand Down Expand Up @@ -175,7 +201,6 @@ to do so).
| `meta-Left Arrow` | indent the current line on the left |
| `meta-Right Arrow` | indent the current line on the right |


### Customizing keybindings

Julia's REPL keybindings may be fully customized to a user's preferences by passing a dictionary
Expand Down
10 changes: 8 additions & 2 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ end
function display(d::REPLDisplay, mime::MIME"text/plain", x)
io = outstream(d.repl)
get(io, :color, false) && write(io, answer_color(d.repl))
show(IOContext(io, :limit => true, :module => Main), mime, x)
if isdefined(d.repl, :iocontext)
# this can override the :limit property set initially
io = foldl(IOContext, d.repl.iocontext, init=IOContext(io, :limit => true, :module => Main))
end
show(io, mime, x)
println(io)
nothing
end
Expand Down Expand Up @@ -328,12 +332,14 @@ mutable struct LineEditREPL <: AbstractREPL
waserror::Bool
specialdisplay::Union{Nothing,AbstractDisplay}
options::Options
# default IOContext settings at the REPL
iocontext::Dict{Symbol,Any}
mistate::Union{MIState,Nothing}
interface::ModalInterface
backendref::REPLBackendRef
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) =
new(t,true,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
in_help,envcolors,false,nothing, Options(), nothing)
in_help,envcolors,false,nothing, Options(), Dict{Symbol,Any}(), nothing)
end
outstream(r::LineEditREPL) = r.t
specialdisplay(r::LineEditREPL) = r.specialdisplay
Expand Down

0 comments on commit b386f5f

Please sign in to comment.