Skip to content

Commit

Permalink
repl: make a trailing colon display with 1000 row limit
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Dec 8, 2022
1 parent c8662b5 commit bf9d8e3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 28 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Standard library changes

#### REPL

- Conversely to `;` which suppresses display, ending repl input with a `:` now calls display with
a large (1000) row limit to make a convenient way to show larger objects with nicely formatted
show methods that are taller than the terminal window ([#47520])

#### SuiteSparse

Expand Down
40 changes: 40 additions & 0 deletions stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ julia> ans
"12"
```

Custom show methods for objects that are used when objects are returned in the repl typically will truncate
output to the height of the terminal window. A trailing colon on the line can be used as a flag to
display to a much higher row limit beyond the terminal window height.

```julia-repl
julia> d = Dict(i => i for i in 1:20)
Dict{Int64, Int64} with 20 entries:
5 => 5
16 => 16
7 => 7
20 => 20
⋮ => ⋮
julia> d:
Dict{Int64, Int64} with 20 entries:
5 => 5
16 => 16
7 => 7
20 => 20
12 => 12
8 => 8
17 => 17
1 => 1
19 => 19
4 => 4
6 => 6
13 => 13
2 => 2
10 => 10
11 => 11
9 => 9
15 => 15
18 => 18
14 => 14
3 => 3
julia>
```

In Julia mode, the REPL supports something called *prompt pasting*. This activates when pasting
text that starts with `julia> ` into the REPL. In that case, only expressions starting with
`julia> ` are parsed, others are removed. This makes it possible to paste a chunk of code
Expand Down
23 changes: 20 additions & 3 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,25 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
end
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)

function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool, limitrows::Bool)
repl.waserror = response[2]
with_repl_linfo(repl) do io
io = IOContext(io, :module => active_module(repl)::Module)
print_response(io, response, show_value, have_color, specialdisplay(repl))
if limitrows
print_response(io, response, show_value, have_color, specialdisplay(repl))
else
before = get(Base.active_repl.options.iocontext, :displaysize, nothing)
try
Base.active_repl.options.iocontext[:displaysize] = (1000, displaysize(io)[2])
print_response(io, response, show_value, have_color, specialdisplay(repl))
finally
if isnothing(before)
delete!(Base.active_repl.options.iocontext, :displaysize)
else
Base.active_repl.options.iocontext[:displaysize] = before
end
end
end
end
return nothing
end
Expand Down Expand Up @@ -890,7 +904,7 @@ function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon:
response = Pair{Any, Bool}(current_exceptions(), true)
end
hide_output = suppress_on_semicolon && ends_with_semicolon(line)
print_response(repl, response, !hide_output, hascolor(repl))
print_response(repl, response, !hide_output, hascolor(repl), !ends_with_colon(line))
end
prepare_next(repl)
reset_state(s)
Expand Down Expand Up @@ -1361,6 +1375,9 @@ end
ends_with_semicolon(code::AbstractString) = ends_with_semicolon(String(code))
ends_with_semicolon(code::Union{String,SubString{String}}) =
contains(_rm_strings_and_comments(code), r";\s*$")
ends_with_colon(code::AbstractString) = ends_with_colon(String(code))
ends_with_colon(code::Union{String,SubString{String}}) =
contains(_rm_strings_and_comments(code), r":\s*$")

function run_frontend(repl::StreamREPL, backend::REPLBackendRef)
have_color = hascolor(repl)
Expand Down
50 changes: 25 additions & 25 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,32 +952,32 @@ fake_repl() do stdin_write, stdout_read, repl
nothing
end

let ends_with_semicolon = REPL.ends_with_semicolon
@test !ends_with_semicolon("")
@test ends_with_semicolon(";")
@test !ends_with_semicolon("ä")
@test !ends_with_semicolon("ä # äsdf ;")
@test ends_with_semicolon("""a * "#ä" ;""")
@test ends_with_semicolon("a; #=#=# =# =#\n")
@test ends_with_semicolon("1;")
@test ends_with_semicolon("1;\n")
@test ends_with_semicolon("1;\r")
@test ends_with_semicolon("1;\r\n \t\f")
@test ends_with_semicolon("1;#äsdf\n")
@test ends_with_semicolon("""1;\n#äsdf\n""")
@test !ends_with_semicolon("\"\\\";\"#\"")
@test ends_with_semicolon("\"\\\\\";#\"")
@test !ends_with_semicolon("begin\na;\nb;\nend")
@test !ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")
@test ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
@test ends_with_semicolon("f()= 1; # é ; 2")
@test ends_with_semicolon("f()= 1; # é")
@test !ends_with_semicolon("f()= 1; \"é\"")
@test !ends_with_semicolon("""("f()= 1; # é")""")
@test !ends_with_semicolon(""" "f()= 1; # é" """)
@test ends_with_semicolon("f()= 1;")
for (c, f) in ((';', REPL.ends_with_semicolon), (':', REPL.ends_with_colon))
@test !f("")
@test f("$c")
@test !f("ä")
@test !f("ä # äsdf $c")
@test f("""a * "#ä" $c""")
@test f("a$c #=#=# =# =#\n")
@test f("1$c")
@test f("1$c\n")
@test f("1$c\r")
@test f("1$c\r\n \t\f")
@test f("1$c#äsdf\n")
@test f("""1$c\n#äsdf\n""")
@test !f("\"\\\"$c\"#\"")
@test f("\"\\\\\"$c#\"")
@test !f("begin\na$c\nb$c\nend")
@test !f("begin\na$c #=#=#\n=#b=#\nend")
@test f("\na$c #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
@test f("f()= 1$c # é $c 2")
@test f("f()= 1$c # é")
@test !f("f()= 1$c \"é\"")
@test !f("""("f()= 1$c # é")""")
@test !f(""" "f()= 1$c # é" """)
@test f("f()= 1$c")
# the next result does not matter because this is not legal syntax
@test_nowarn ends_with_semicolon("1; #=# 2")
@test_nowarn f("1$c #=# 2")
end

# PR #20794, TTYTerminal with other kinds of streams
Expand Down

0 comments on commit bf9d8e3

Please sign in to comment.