Skip to content

Commit

Permalink
Merge pull request #16354 from JuliaLang/jb/output
Browse files Browse the repository at this point in the history
RFC: use IOContext more consistently. part of #14052
  • Loading branch information
JeffBezanson committed May 24, 2016
2 parents b00fcb0 + 0c3147e commit c43b5e5
Show file tree
Hide file tree
Showing 29 changed files with 234 additions and 226 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=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
6 changes: 3 additions & 3 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,11 +937,11 @@ end
#14335
@deprecate super(T::DataType) supertype(T)

function with_output_limit(thk, lim=true) # thk is usually show()
depwarn("with_output_limit is deprecated. use `io = IOContext(io, :limit_output => lim)` as a replacement", :with_output_limit)
function with_output_limit(thk, lim::Bool=true) # thk is usually show()
depwarn("with_output_limit is deprecated. use `io = IOContext(io, :limit => lim)` as a replacement", :with_output_limit)
global _limit_output
last = _limit_output
_limit_output::Bool = lim
_limit_output = lim
try
thk()
finally
Expand Down
25 changes: 13 additions & 12 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)
limit::Bool = limit_output(io)
function show{K,V}(io::IO, t::Associative{K,V})
recur_io = IOContext(io, SHOWN_SET=t, multiline=false)
limit::Bool = get(io, :limit, false)
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,14 +149,14 @@ 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", ":")
limit::Bool = limit_output(io)
print(io, ". ", isa(iter,KeyIterator) ? "Keys" : "Values", ":")
limit::Bool = get(io, :limit, false)
if limit
sz = displaysize(io)
rows, cols = sz[1] - 3, sz[2]
Expand Down
7 changes: 3 additions & 4 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2888,10 +2888,9 @@ Show an expression and result, returning the result.
"""
showcompact(x)
Show a more compact representation of a value. This is used for printing array elements. If
a new type has a different compact representation,
it should test `Base.limit_output(io)` in its normal `show` method.
Show a more compact representation of a value. This is used for printing array elements.
If a new type has a different compact representation,
it should test `get(io, :compact, false)` in its normal `show` method.
"""
showcompact

Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,6 @@ export
ismarked,
isopen,
isreadonly,
limit_output,
listen,
listenany,
ltoh,
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)
Base.showarray(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)
10 changes: 6 additions & 4 deletions base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,33 @@ mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)
# format and is returned unmodified. This is useful so that raw data can be
# passed to display(m::MIME, x).

verbose_writemime(io, m, x) = writemime(IOContext(io,multiline=true,limit=false), m, x)

macro textmime(mime)
quote
mimeT = MIME{Symbol($mime)}
# avoid method ambiguities with the general definitions below:
# (Q: should we treat Vector{UInt8} as a String?)
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(writemime, m, x)
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_writemime, m, x)
Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x)

Base.Multimedia.istextmime(::mimeT) = true
if $(mime != "text/plain") # strings are shown escaped for text/plain
Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x
end
Base.Multimedia.reprmime(m::mimeT, x) = sprint(writemime, m, x)
Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_writemime, m, x)
Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x)
end
end

istextmime(::MIME) = false
function reprmime(m::MIME, x)
s = IOBuffer()
writemime(s, m, x)
verbose_writemime(s, m, x)
takebuf_array(s)
end
reprmime(m::MIME, x::Vector{UInt8}) = x
stringmime(m::MIME, x) = base64encode(writemime, m, x)
stringmime(m::MIME, x) = base64encode(verbose_writemime, m, x)
stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)

# it is convenient to accept strings instead of ::MIME
Expand Down
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
31 changes: 23 additions & 8 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 @@ -265,8 +277,11 @@ function print_range(io::IO, r::Range,
hdots::AbstractString = ",\u2026,") # horiz ellipsis
# This function borrows from print_matrix() in show.jl
# and should be called by writemime (replutil.jl) and by display()
limit = limit_output(io)
limit = get(io, :limit, false)
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://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 c43b5e5

Please sign in to comment.