Skip to content

Commit 3f73066

Browse files
committed
improve output limiting behavior, fixing #4179
Uses a dynamic variable to control output limiting, so that everything inside interactive display is limited, but everything inside string(), repr(), and showall() is not limited.
1 parent 5dd3132 commit 3f73066

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

base/repl.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# fallback text/plain representation of any type:
2-
writemime(io, ::MIME"text/plain", x) = show(io, x)
2+
writemime(io, ::MIME"text/plain", x) = showlimited(io, x)
33

44
function writemime(io, ::MIME"text/plain", v::Function)
55
if isgeneric(v)

base/show.jl

+55-8
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ function xdump(fn::Function, io::IO, x::Array{Any}, n::Int, indent)
503503
end
504504
xdump(fn::Function, io::IO, x::Symbol, n::Int, indent) = println(io, typeof(x), " ", x)
505505
xdump(fn::Function, io::IO, x::Function, n::Int, indent) = println(io, x)
506-
xdump(fn::Function, io::IO, x::Array, n::Int, indent) = println(io, "Array($(eltype(x)),$(size(x)))", " ", x)
506+
xdump(fn::Function, io::IO, x::Array, n::Int, indent) =
507+
(print(io, "Array($(eltype(x)),$(size(x))) ");
508+
show(io, x); println(io))
507509

508510
# Types
509511
xdump(fn::Function, io::IO, x::UnionType, n::Int, indent) = println(io, x)
@@ -579,15 +581,17 @@ xdump(fn::Function, io::IO, x, n::Int) = xdump(xdump, io, x, n, "")
579581
xdump(fn::Function, io::IO, args...) = error("invalid arguments to xdump")
580582
xdump(fn::Function, args...) = xdump(fn, STDOUT::IO, args...)
581583
xdump(io::IO, args...) = xdump(xdump, io, args...)
582-
xdump(args...) = xdump(xdump, STDOUT::IO, args...)
584+
xdump(args...) = with_output_limit(()->xdump(xdump, STDOUT::IO, args...), true)
583585

584586
# Here are methods specifically for dump:
585587
dump(io::IO, x, n::Int) = dump(io, x, n, "")
586588
dump(io::IO, x) = dump(io, x, 5, "") # default is 5 levels
587-
dump(io::IO, x::String, n::Int, indent) = println(io, typeof(x), " \"", x, "\"")
589+
dump(io::IO, x::String, n::Int, indent) =
590+
(print(io, typeof(x), " ");
591+
show(io, x); println(io))
588592
dump(io::IO, x, n::Int, indent) = xdump(dump, io, x, n, indent)
589593
dump(io::IO, args...) = error("invalid arguments to dump")
590-
dump(args...) = dump(STDOUT::IO, args...)
594+
dump(args...) = with_output_limit(()->dump(STDOUT::IO, args...), true)
591595

592596
function dump(io::IO, x::Dict, n::Int, indent)
593597
println(typeof(x), " len ", length(x))
@@ -854,10 +858,17 @@ function show{T}(io::IO, x::AbstractArray{T,0})
854858
print(io, sx)
855859
end
856860

861+
# global flag for limiting output
862+
# TODO: this should be replaced with a better mechanism. currently it is only
863+
# for internal use in showing arrays.
864+
_limit_output = false
865+
857866
# NOTE: this is a possible, so-far-unexported function, providing control of
858867
# array output. Not sure I want to do it this way.
859868
showarray(X::AbstractArray; kw...) = showarray(STDOUT, X; kw...)
860-
function showarray(io::IO, X::AbstractArray; header=true, limit=true, rows=tty_rows()-4, cols=tty_cols())
869+
function showarray(io::IO, X::AbstractArray;
870+
header::Bool=true, limit::Bool=_limit_output,
871+
rows = tty_rows()-4, cols = tty_cols())
861872
header && print(io, summary(X))
862873
if !isempty(X)
863874
header && println(io, ":")
@@ -879,12 +890,48 @@ show(io::IO, X::AbstractArray) = showarray(io, X)
879890

880891
print(io::IO, X::AbstractArray) = writedlm(io, X)
881892

893+
function with_output_limit(thk, lim=true)
894+
global _limit_output
895+
last = _limit_output
896+
_limit_output = lim
897+
try
898+
thk()
899+
finally
900+
_limit_output = last
901+
end
902+
end
903+
882904
showall(x) = showall(STDOUT, x)
883-
showall(io::IO, x) = show(io, x)
884-
showall(io::IO, x::AbstractArray) = showarray(io, x, limit=false)
905+
function showall(io::IO, x)
906+
if _limit_output==false
907+
show(io, x)
908+
else
909+
with_output_limit(false) do
910+
show(io, x)
911+
end
912+
end
913+
end
914+
915+
showlimited(x) = showlimited(STDOUT, x)
916+
function showlimited(io::IO, x)
917+
if _limit_output==true
918+
show(io, x)
919+
else
920+
with_output_limit(true) do
921+
show(io, x)
922+
end
923+
end
924+
end
885925

886926
function show_vector(io::IO, v, opn, cls)
887-
show_delim_array(io, v, opn, ",", cls, false)
927+
if _limit_output && length(v) > 20
928+
show_delim_array(io, v[1:10], opn, ",", "", false)
929+
print(io, " \u2026 ")
930+
show_delim_array(io, v[end-9:end], "", ",", cls, false)
931+
#print_matrix(io, X, 1, tty_cols(), opn, ", ", cls, " \u2026 ", "\u22ee", " \u22f1 ", 5, 5)
932+
else
933+
show_delim_array(io, v, opn, ",", cls, false)
934+
end
888935
end
889936

890937
show(io::IO, v::AbstractVector{Any}) = show_vector(io, v, "{", "}")

base/string.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,6 @@ bytes2hex(arr::Array{Uint8,1}) = join([hex(i, 2) for i in arr])
14051405

14061406
function repr(x)
14071407
s = IOBuffer()
1408-
show(s, x)
1408+
showall(s, x)
14091409
takebuf_string(s)
14101410
end
1411-

0 commit comments

Comments
 (0)