Skip to content

Commit c43b5e5

Browse files
committed
Merge pull request #16354 from JuliaLang/jb/output
RFC: use IOContext more consistently. part of #14052
2 parents b00fcb0 + 0c3147e commit c43b5e5

29 files changed

+234
-226
lines changed

base/Enums.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ macro enum(T,syms...)
9393
end
9494
end
9595
function Base.show(io::IO,x::$(esc(typename)))
96-
if Base.limit_output(io)
96+
if get(io, :compact, false)
9797
print(io, x)
9898
else
9999
print(io, x, "::", $(esc(typename)), " = ", Int(x))

base/REPL.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ end
111111
function display(d::REPLDisplay, ::MIME"text/plain", x)
112112
io = outstream(d.repl)
113113
Base.have_color && write(io, answer_color(d.repl))
114-
writemime(io, MIME("text/plain"), x)
114+
writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x)
115115
println(io)
116116
end
117117
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)

base/complex.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ flipsign(x::Complex, y::Real) = ifelse(signbit(y), -x, x)
6767

6868
function show(io::IO, z::Complex)
6969
r, i = reim(z)
70-
compact = limit_output(io)
71-
showcompact_lim(io, r)
70+
compact = get(io, :compact, false)
71+
show(io, r)
7272
if signbit(i) && !isnan(i)
7373
i = -i
7474
print(io, compact ? "-" : " - ")
7575
else
7676
print(io, compact ? "+" : " + ")
7777
end
78-
showcompact_lim(io, i)
78+
show(io, i)
7979
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
8080
print(io, "*")
8181
end

base/deprecated.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,11 @@ end
937937
#14335
938938
@deprecate super(T::DataType) supertype(T)
939939

940-
function with_output_limit(thk, lim=true) # thk is usually show()
941-
depwarn("with_output_limit is deprecated. use `io = IOContext(io, :limit_output => lim)` as a replacement", :with_output_limit)
940+
function with_output_limit(thk, lim::Bool=true) # thk is usually show()
941+
depwarn("with_output_limit is deprecated. use `io = IOContext(io, :limit => lim)` as a replacement", :with_output_limit)
942942
global _limit_output
943943
last = _limit_output
944-
_limit_output::Bool = lim
944+
_limit_output = lim
945945
try
946946
thk()
947947
finally

base/dict.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ function summary(t::Associative)
3333
string(typeof(t), " with ", n, (n==1 ? " entry" : " entries"))
3434
end
3535

36-
show{K,V}(io::IO, t::Associative{K,V}) = showdict(io, t; compact = true)
37-
3836
function _truncate_at_width_or_chars(str, width, chars="", truncmark="")
3937
truncwidth = strwidth(truncmark)
4038
(width <= 0 || width < truncwidth) && return ""
@@ -58,10 +56,13 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
5856
end
5957
end
6058

61-
showdict(t::Associative; kw...) = showdict(STDOUT, t; kw...)
62-
function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false)
63-
recur_io = IOContext(io, :SHOWN_SET => t)
64-
limit::Bool = limit_output(io)
59+
function show{K,V}(io::IO, t::Associative{K,V})
60+
recur_io = IOContext(io, SHOWN_SET=t, multiline=false)
61+
limit::Bool = get(io, :limit, false)
62+
compact = !get(io, :multiline, false)
63+
if !haskey(io, :compact)
64+
recur_io = IOContext(recur_io, compact=true)
65+
end
6566
if compact
6667
# show in a Julia-syntax-like form: Dict(k=>v, ...)
6768
if isempty(t)
@@ -148,14 +149,14 @@ end
148149
summary{T<:Union{KeyIterator,ValueIterator}}(iter::T) =
149150
string(T.name, " for a ", summary(iter.dict))
150151

151-
show(io::IO, iter::Union{KeyIterator,ValueIterator}) = show(io, collect(iter))
152-
153-
showkv(iter::Union{KeyIterator,ValueIterator}) = showkv(STDOUT, iter)
154-
function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T)
152+
function show(io::IO, iter::Union{KeyIterator,ValueIterator})
153+
if !get(io, :multiline, false)
154+
return show(io, collect(iter))
155+
end
155156
print(io, summary(iter))
156157
isempty(iter) && return
157-
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
158-
limit::Bool = limit_output(io)
158+
print(io, ". ", isa(iter,KeyIterator) ? "Keys" : "Values", ":")
159+
limit::Bool = get(io, :limit, false)
159160
if limit
160161
sz = displaysize(io)
161162
rows, cols = sz[1] - 3, sz[2]

base/docs/helpdb/Base.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,10 +2888,9 @@ Show an expression and result, returning the result.
28882888
"""
28892889
showcompact(x)
28902890
2891-
2892-
Show a more compact representation of a value. This is used for printing array elements. If
2893-
a new type has a different compact representation,
2894-
it should test `Base.limit_output(io)` in its normal `show` method.
2891+
Show a more compact representation of a value. This is used for printing array elements.
2892+
If a new type has a different compact representation,
2893+
it should test `get(io, :compact, false)` in its normal `show` method.
28952894
"""
28962895
showcompact
28972896

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,6 @@ export
11441144
ismarked,
11451145
isopen,
11461146
isreadonly,
1147-
limit_output,
11481147
listen,
11491148
listenany,
11501149
ltoh,

base/grisu.jl

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ function _show(io::IO, x::AbstractFloat, mode, n::Int, typed, nanstr, infstr)
116116
nothing
117117
end
118118

119-
Base.show(io::IO, x::AbstractFloat) = Base.limit_output(io) ? showcompact(io, x) : _show(io, x, SHORTEST, 0, true)
119+
function Base.show(io::IO, x::Union{Float64,Float32})
120+
if get(io, :compact, false)
121+
_show(io, x, PRECISION, 6, false)
122+
else
123+
_show(io, x, SHORTEST, 0, true)
124+
end
125+
end
126+
127+
function Base.show(io::IO, x::Float16)
128+
if get(io, :compact, false)
129+
_show(io, x, PRECISION, 5, false)
130+
else
131+
_show(io, x, SHORTEST, 0, true)
132+
end
133+
end
120134

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

124-
Base.showcompact(io::IO, x::Float64) = _show(io, x, PRECISION, 6, false)
125-
Base.showcompact(io::IO, x::Float32) = _show(io, x, PRECISION, 6, false)
126-
Base.showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)
127-
128138
# normal:
129139
# 0 < pt < len ####.#### len+1
130140
# pt <= 0 0.000######## len-pt+1

base/interactiveutil.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ end
161161

162162
# system information
163163

164-
function show(io::IO, info::Sys.CPUinfo, header::Bool=true, prefix::AbstractString=" ")
164+
# used by sysinfo.jl
165+
function _show_cpuinfo(io::IO, info::Sys.CPUinfo, header::Bool=true, prefix::AbstractString=" ")
165166
tck = Sys.SC_CLK_TCK
166167
if header
167168
println(io, info.model, ": ")

base/irrationals.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ log(::Irrational{:e}, x::Number) = log(x)
131131

132132
# align along = for nice Array printing
133133
function alignment(io::IO, x::Irrational)
134-
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact_lim, x, env=io))
135-
m === nothing ? (length(sprint(0, showcompact_lim, x, env=io)), 0) :
134+
m = match(r"^(.*?)(=.*)$", sprint(0, showcompact, x, env=io))
135+
m === nothing ? (length(sprint(0, showcompact, x, env=io)), 0) :
136136
(length(m.captures[1]), length(m.captures[2]))
137137
end

base/linalg/bidiag.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ svdfact(M::Bidiagonal; thin::Bool=true) = svdfact!(copy(M),thin=thin)
120120
####################
121121

122122
function show(io::IO, M::Bidiagonal)
123-
println(io, summary(M), ":")
124-
print(io, " diag:")
125-
print_matrix(io, (M.dv)')
126-
print(io, M.isupper?"\n super:":"\n sub:")
127-
print_matrix(io, (M.ev)')
123+
if get(io, :multiline, false)
124+
Base.showarray(io, M)
125+
else
126+
println(io, summary(M), ":")
127+
print(io, " diag:")
128+
print_matrix(io, (M.dv)')
129+
print(io, M.isupper?"\n super:":"\n sub:")
130+
print_matrix(io, (M.ev)')
131+
end
128132
end
129133

130134
size(M::Bidiagonal) = (length(M.dv), length(M.dv))

base/methodshow.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,3 @@ function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
281281
print(io, "</ul>")
282282
end
283283
end
284-
285-
# override usual show method for Vector{Method}: don't abbreviate long lists
286-
writemime(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) =
287-
showarray(IOContext(io, :limit_output => false), mt)

base/multimedia.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,33 @@ mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x)
5757
# format and is returned unmodified. This is useful so that raw data can be
5858
# passed to display(m::MIME, x).
5959

60+
verbose_writemime(io, m, x) = writemime(IOContext(io,multiline=true,limit=false), m, x)
61+
6062
macro textmime(mime)
6163
quote
6264
mimeT = MIME{Symbol($mime)}
6365
# avoid method ambiguities with the general definitions below:
6466
# (Q: should we treat Vector{UInt8} as a String?)
65-
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(writemime, m, x)
67+
Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_writemime, m, x)
6668
Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x)
6769

6870
Base.Multimedia.istextmime(::mimeT) = true
6971
if $(mime != "text/plain") # strings are shown escaped for text/plain
7072
Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x
7173
end
72-
Base.Multimedia.reprmime(m::mimeT, x) = sprint(writemime, m, x)
74+
Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_writemime, m, x)
7375
Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x)
7476
end
7577
end
7678

7779
istextmime(::MIME) = false
7880
function reprmime(m::MIME, x)
7981
s = IOBuffer()
80-
writemime(s, m, x)
82+
verbose_writemime(s, m, x)
8183
takebuf_array(s)
8284
end
8385
reprmime(m::MIME, x::Vector{UInt8}) = x
84-
stringmime(m::MIME, x) = base64encode(writemime, m, x)
86+
stringmime(m::MIME, x) = base64encode(verbose_writemime, m, x)
8587
stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x)
8688

8789
# it is convenient to accept strings instead of ::MIME

base/nullable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ promote_rule{S,T}(::Type{Nullable{S}}, ::Type{T}) = Nullable{promote_type(S, T)}
2727
promote_rule{S,T}(::Type{Nullable{S}}, ::Type{Nullable{T}}) = Nullable{promote_type(S, T)}
2828

2929
function show{T}(io::IO, x::Nullable{T})
30-
if limit_output(io)
30+
if get(io, :compact, false)
3131
if isnull(x)
3232
print(io, "#NULL")
3333
else

base/range.jl

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,25 @@ linspace(start::Real, stop::Real, len::Real=50) =
236236
linspace(promote(AbstractFloat(start), AbstractFloat(stop))..., len)
237237

238238
function show(io::IO, r::LinSpace)
239-
print(io, "linspace(")
240-
show(io, first(r))
241-
print(io, ',')
242-
show(io, last(r))
243-
print(io, ',')
244-
show(io, length(r))
245-
print(io, ')')
239+
if get(io, :multiline, false)
240+
# writemime for linspace, e.g.
241+
# linspace(1,3,7)
242+
# 7-element LinSpace{Float64}:
243+
# 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
244+
print(io, summary(r))
245+
if !isempty(r)
246+
println(io, ":")
247+
print_range(io, r)
248+
end
249+
else
250+
print(io, "linspace(")
251+
show(io, first(r))
252+
print(io, ',')
253+
show(io, last(r))
254+
print(io, ',')
255+
show(io, length(r))
256+
print(io, ')')
257+
end
246258
end
247259

248260
"""
@@ -265,8 +277,11 @@ function print_range(io::IO, r::Range,
265277
hdots::AbstractString = ",\u2026,") # horiz ellipsis
266278
# This function borrows from print_matrix() in show.jl
267279
# and should be called by writemime (replutil.jl) and by display()
268-
limit = limit_output(io)
280+
limit = get(io, :limit, false)
269281
sz = displaysize(io)
282+
if !haskey(io, :compact)
283+
io = IOContext(io, compact=true)
284+
end
270285
screenheight, screenwidth = sz[1] - 4, sz[2]
271286
screenwidth -= length(pre) + length(post)
272287
postsp = ""

base/replutil.jl

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,7 @@
11
# This file is a part of Julia. License is MIT: http://julialang.org/license
22

33
# fallback text/plain representation of any type:
4-
writemime(io::IO, ::MIME"text/plain", x) = showcompact(io, x)
5-
writemime(io::IO, ::MIME"text/plain", x::Number) = show(io, x)
6-
writemime(io::IO, ::MIME"text/plain", x::Associative) = showdict(io, x)
7-
8-
function writemime(io::IO, ::MIME"text/plain", f::Function)
9-
ft = typeof(f)
10-
mt = ft.name.mt
11-
name = mt.name
12-
isself = isdefined(ft.name.module, name) &&
13-
ft == typeof(getfield(ft.name.module, name))
14-
n = length(mt)
15-
m = n==1 ? "method" : "methods"
16-
ns = isself ? string(name) : string("(::", name, ")")
17-
what = startswith(ns, '@') ? "macro" : "generic function"
18-
print(io, ns, " (", what, " with $n $m)")
19-
end
20-
21-
function writemime(io::IO, ::MIME"text/plain", f::Builtin)
22-
print(io, typeof(f).name.mt.name, " (built-in function)")
23-
end
24-
25-
# writemime for linspace, e.g.
26-
# linspace(1,3,7)
27-
# 7-element LinSpace{Float64}:
28-
# 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
29-
function writemime(io::IO, ::MIME"text/plain", r::LinSpace)
30-
print(io, summary(r))
31-
if !isempty(r)
32-
println(io, ":")
33-
print_range(IOContext(io, :limit_output => true), r)
34-
end
35-
end
36-
37-
# writemime for ranges
38-
function writemime(io::IO, ::MIME"text/plain", r::Range)
39-
show(io, r)
40-
end
41-
42-
function writemime(io::IO, ::MIME"text/plain", v::AbstractVector)
43-
print(io, summary(v))
44-
if !isempty(v)
45-
println(io, ":")
46-
print_matrix(IOContext(io, :limit_output => true), v)
47-
end
48-
end
49-
50-
writemime(io::IO, ::MIME"text/plain", v::AbstractArray) =
51-
showarray(IOContext(io, :limit_output => true), v, header=true, repr=false)
52-
53-
function writemime(io::IO, ::MIME"text/plain", v::DataType)
54-
show(io, v)
55-
# TODO: maybe show constructor info?
56-
end
57-
58-
function writemime(io::IO, ::MIME"text/plain", t::Task)
59-
show(io, t)
60-
if t.state == :failed
61-
println(io)
62-
showerror(io, CapturedException(t.result, t.backtrace))
63-
end
64-
end
4+
writemime(io::IO, ::MIME"text/plain", x) = show(io, x)
655

666

677
# showing exception objects as descriptive error messages

0 commit comments

Comments
 (0)