Skip to content

Commit

Permalink
use IOContext more consistently. part of #14052
Browse files Browse the repository at this point in the history
We had several `writemime` definitions whose purpose was to control how
objects should be printed in the REPL. Instead this introduces a
`:multiline` IOContext flag.

Use `:compact` IOContext flag to request shorter printing of numbers.

Removes `showarray` and `showdict`, allowing IOContext flags to work
better recursively. Fixes #13710.

Also related: #16103 and #16267.
  • Loading branch information
JeffBezanson committed May 23, 2016
1 parent 9afb230 commit ce23174
Show file tree
Hide file tree
Showing 23 changed files with 189 additions and 184 deletions.
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ macro enum(T,syms...)
end
end
function Base.show(io::IO,x::$(esc(typename)))
if Base.limit_output(io)
if get(io, :compact, false)
print(io, x)
else
print(io, x, "::", $(esc(typename)), " = ", Int(x))
Expand Down
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ end
function display(d::REPLDisplay, ::MIME"text/plain", x)
io = outstream(d.repl)
Base.have_color && write(io, answer_color(d.repl))
writemime(io, MIME("text/plain"), x)
writemime(IOContext(io, multiline=true, limit_output=true), MIME("text/plain"), x)
println(io)
end
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
Expand Down
6 changes: 3 additions & 3 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)

function show(io::IO, z::Complex)
r, i = reim(z)
compact = limit_output(io)
showcompact_lim(io, r)
compact = get(io, :compact, false)
show(io, r)
if signbit(i) && !isnan(i)
i = -i
print(io, compact ? "-" : " - ")
else
print(io, compact ? "+" : " + ")
end
showcompact_lim(io, i)
show(io, i)
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
print(io, "*")
end
Expand Down
21 changes: 11 additions & 10 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ function summary(t::Associative)
string(typeof(t), " with ", n, (n==1 ? " entry" : " entries"))
end

show{K,V}(io::IO, t::Associative{K,V}) = showdict(io, t; compact = true)

function _truncate_at_width_or_chars(str, width, chars="", truncmark="")
truncwidth = strwidth(truncmark)
(width <= 0 || width < truncwidth) && return ""
Expand All @@ -58,10 +56,13 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
end
end

showdict(t::Associative; kw...) = showdict(STDOUT, t; kw...)
function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false)
recur_io = IOContext(io, :SHOWN_SET => t)
function show{K,V}(io::IO, t::Associative{K,V})
recur_io = IOContext(io, SHOWN_SET=t, multiline=false)
limit::Bool = limit_output(io)
compact = !get(io, :multiline, false)
if !haskey(io, :compact)
recur_io = IOContext(recur_io, compact=true)
end
if compact
# show in a Julia-syntax-like form: Dict(k=>v, ...)
if isempty(t)
Expand Down Expand Up @@ -148,13 +149,13 @@ end
summary{T<:Union{KeyIterator,ValueIterator}}(iter::T) =
string(T.name, " for a ", summary(iter.dict))

show(io::IO, iter::Union{KeyIterator,ValueIterator}) = show(io, collect(iter))

showkv(iter::Union{KeyIterator,ValueIterator}) = showkv(STDOUT, iter)
function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T)
function show(io::IO, iter::Union{KeyIterator,ValueIterator})
if !get(io, :multiline, false)
return show(io, collect(iter))
end
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
print(io, ". ", isa(iter,KeyIterator) ? "Keys" : "Values", ":")
limit::Bool = limit_output(io)
if limit
sz = displaysize(io)
Expand Down
20 changes: 15 additions & 5 deletions base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,25 @@ function _show(io::IO, x::AbstractFloat, mode, n::Int, typed, nanstr, infstr)
nothing
end

Base.show(io::IO, x::AbstractFloat) = Base.limit_output(io) ? showcompact(io, x) : _show(io, x, SHORTEST, 0, true)
function Base.show(io::IO, x::Union{Float64,Float32})
if get(io, :compact, false)
_show(io, x, PRECISION, 6, false)
else
_show(io, x, SHORTEST, 0, true)
end
end

function Base.show(io::IO, x::Float16)
if get(io, :compact, false)
_show(io, x, PRECISION, 5, false)
else
_show(io, x, SHORTEST, 0, true)
end
end

Base.print(io::IO, x::Float32) = _show(io, x, SHORTEST, 0, false)
Base.print(io::IO, x::Float16) = _show(io, x, SHORTEST, 0, false)

Base.showcompact(io::IO, x::Float64) = _show(io, x, PRECISION, 6, false)
Base.showcompact(io::IO, x::Float32) = _show(io, x, PRECISION, 6, false)
Base.showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)

# normal:
# 0 < pt < len ####.#### len+1
# pt <= 0 0.000######## len-pt+1
Expand Down
3 changes: 2 additions & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ end

# system information

function show(io::IO, info::Sys.CPUinfo, header::Bool=true, prefix::AbstractString=" ")
# used by sysinfo.jl
function _show_cpuinfo(io::IO, info::Sys.CPUinfo, header::Bool=true, prefix::AbstractString=" ")
tck = Sys.SC_CLK_TCK
if header
println(io, info.model, ": ")
Expand Down
4 changes: 2 additions & 2 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ log(::Irrational{:e}, x::Number) = log(x)

# align along = for nice Array printing
function alignment(io::IO, x::Irrational)
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact_lim, x, env=io))
m === nothing ? (length(sprint(0, showcompact_lim, x, env=io)), 0) :
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact, x, env=io))
m === nothing ? (length(sprint(0, showcompact, x, env=io)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end
14 changes: 9 additions & 5 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ svdfact(M::Bidiagonal; thin::Bool=true) = svdfact!(copy(M),thin=thin)
####################

function show(io::IO, M::Bidiagonal)
println(io, summary(M), ":")
print(io, " diag:")
print_matrix(io, (M.dv)')
print(io, M.isupper?"\n super:":"\n sub:")
print_matrix(io, (M.ev)')
if get(io, :multiline, false)
invoke(show, (IO, AbstractArray), io, M)
else
println(io, summary(M), ":")
print(io, " diag:")
print_matrix(io, (M.dv)')
print(io, M.isupper?"\n super:":"\n sub:")
print_matrix(io, (M.ev)')
end
end

size(M::Bidiagonal) = (length(M.dv), length(M.dv))
Expand Down
4 changes: 0 additions & 4 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,3 @@ function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
print(io, "</ul>")
end
end

# override usual show method for Vector{Method}: don't abbreviate long lists
writemime(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) =
showarray(IOContext(io, :limit_output => false), mt)
2 changes: 1 addition & 1 deletion base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T}) = Nullable{promote_type(S, T)}
promote_rule{S,T}(::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_type(S, T)}

function show{T}(io::IO, x::Nullable{T})
if limit_output(io)
if get(io, :compact, false)
if isnull(x)
print(io, "#NULL")
else
Expand Down
29 changes: 22 additions & 7 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,25 @@ linspace(start::Real, stop::Real, len::Real=50) =
linspace(promote(AbstractFloat(start), AbstractFloat(stop))..., len)

function show(io::IO, r::LinSpace)
print(io, "linspace(")
show(io, first(r))
print(io, ',')
show(io, last(r))
print(io, ',')
show(io, length(r))
print(io, ')')
if get(io, :multiline, false)
# writemime for linspace, e.g.
# linspace(1,3,7)
# 7-element LinSpace{Float64}:
# 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
print(io, summary(r))
if !isempty(r)
println(io, ":")
print_range(io, r)
end
else
print(io, "linspace(")
show(io, first(r))
print(io, ',')
show(io, last(r))
print(io, ',')
show(io, length(r))
print(io, ')')
end
end

"""
Expand All @@ -267,6 +279,9 @@ function print_range(io::IO, r::Range,
# and should be called by writemime (replutil.jl) and by display()
limit = limit_output(io)
sz = displaysize(io)
if !haskey(io, :compact)
io = IOContext(io, compact=true)
end
screenheight, screenwidth = sz[1] - 4, sz[2]
screenwidth -= length(pre) + length(post)
postsp = ""
Expand Down
62 changes: 1 addition & 61 deletions base/replutil.jl
Original file line number Diff line number Diff line change
@@ -1,67 +1,7 @@
# This file is a part of Julia. License is MIT: http:https://julialang.org/license

# fallback text/plain representation of any type:
writemime(io::IO, ::MIME"text/plain", x) = showcompact(io, x)
writemime(io::IO, ::MIME"text/plain", x::Number) = show(io, x)
writemime(io::IO, ::MIME"text/plain", x::Associative) = showdict(io, x)

function writemime(io::IO, ::MIME"text/plain", f::Function)
ft = typeof(f)
mt = ft.name.mt
name = mt.name
isself = isdefined(ft.name.module, name) &&
ft == typeof(getfield(ft.name.module, name))
n = length(mt)
m = n==1 ? "method" : "methods"
ns = isself ? string(name) : string("(::", name, ")")
what = startswith(ns, '@') ? "macro" : "generic function"
print(io, ns, " (", what, " with $n $m)")
end

function writemime(io::IO, ::MIME"text/plain", f::Builtin)
print(io, typeof(f).name.mt.name, " (built-in function)")
end

# writemime for linspace, e.g.
# linspace(1,3,7)
# 7-element LinSpace{Float64}:
# 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
function writemime(io::IO, ::MIME"text/plain", r::LinSpace)
print(io, summary(r))
if !isempty(r)
println(io, ":")
print_range(IOContext(io, :limit_output => true), r)
end
end

# writemime for ranges
function writemime(io::IO, ::MIME"text/plain", r::Range)
show(io, r)
end

function writemime(io::IO, ::MIME"text/plain", v::AbstractVector)
print(io, summary(v))
if !isempty(v)
println(io, ":")
print_matrix(IOContext(io, :limit_output => true), v)
end
end

writemime(io::IO, ::MIME"text/plain", v::AbstractArray) =
showarray(IOContext(io, :limit_output => true), v, header=true, repr=false)

function writemime(io::IO, ::MIME"text/plain", v::DataType)
show(io, v)
# TODO: maybe show constructor info?
end

function writemime(io::IO, ::MIME"text/plain", t::Task)
show(io, t)
if t.state == :failed
println(io)
showerror(io, CapturedException(t.result, t.backtrace))
end
end
writemime(io::IO, ::MIME"text/plain", x) = show(io, x)


# showing exception objects as descriptive error messages
Expand Down
Loading

0 comments on commit ce23174

Please sign in to comment.