Skip to content

Commit

Permalink
move around and rename some show related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jan 21, 2018
1 parent 5f1d77b commit 78730ba
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 229 deletions.
1 change: 1 addition & 0 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ function _display(io::IO, X::AbstractArray)
print_array(io, X)
end

show(io::IO, ::MIME"text/plain", X::AbstractArray) = _display(io, X)

## printing with `show`

Expand Down
158 changes: 0 additions & 158 deletions base/replutil.jl → base/errorshow.jl
Original file line number Diff line number Diff line change
@@ -1,163 +1,5 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

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

# multiline show functions for types defined before multimedia.jl:
function show(io::IO, ::MIME"text/plain", iter::Union{KeySet,ValueIterator})
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", isa(iter,KeySet) ? "Keys" : "Values", ":")
limit::Bool = get(io, :limit, false)
if limit
sz = displaysize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 4 && (cols = 4)
cols -= 2 # For prefix " "
rows -= 1 # For summary
else
rows = cols = typemax(Int)
end

for (i, v) in enumerate(iter)
print(io, "\n ")
i == rows < length(iter) && (print(io, ""); break)

if limit
str = sprint(show, v, context=io, sizehint=0)
str = _truncate_at_width_or_chars(str, cols, "\r\n")
print(io, str)
else
show(io, v)
end
end
end

function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
# show more descriptively, with one line per key/value pair
recur_io = IOContext(io, :SHOWN_SET => t)
limit::Bool = get(io, :limit, false)
if !haskey(io, :compact)
recur_io = IOContext(recur_io, :compact => true)
end

print(io, summary(t))
isempty(t) && return
print(io, ":")
show_circular(io, t) && return
if limit
sz = displaysize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 12 && (cols = 12) # Minimum widths of 2 for key, 4 for value
cols -= 6 # Subtract the widths of prefix " " separator " => "
rows -= 1 # Subtract the summary

# determine max key width to align the output, caching the strings
ks = Vector{AbstractString}(uninitialized, min(rows, length(t)))
vs = Vector{AbstractString}(uninitialized, min(rows, length(t)))
keylen = 0
vallen = 0
for (i, (k, v)) in enumerate(t)
i > rows && break
ks[i] = sprint(show, k, context=recur_io, sizehint=0)
vs[i] = sprint(show, v, context=recur_io, sizehint=0)
keylen = clamp(length(ks[i]), keylen, cols)
vallen = clamp(length(vs[i]), vallen, cols)
end
if keylen > max(div(cols, 2), cols - vallen)
keylen = max(cld(cols, 3), cols - vallen)
end
else
rows = cols = typemax(Int)
end

for (i, (k, v)) in enumerate(t)
print(io, "\n ")
i == rows < length(t) && (print(io, rpad("", keylen), " => ⋮"); break)

if limit
key = rpad(_truncate_at_width_or_chars(ks[i], keylen, "\r\n"), keylen)
else
key = sprint(show, k, context=recur_io, sizehint=0)
end
print(recur_io, key)
print(io, " => ")

if limit
val = _truncate_at_width_or_chars(vs[i], cols - keylen, "\r\n")
print(io, val)
else
show(recur_io, v)
end
end
end

function show(io::IO, ::MIME"text/plain", f::Function)
ft = typeof(f)
mt = ft.name.mt
if isa(f, Core.IntrinsicFunction)
show(io, f)
id = Core.Intrinsics.bitcast(Int32, f)
print(io, " (intrinsic function #$id)")
elseif isa(f, Core.Builtin)
print(io, mt.name, " (built-in function)")
else
name = mt.name
isself = isdefined(ft.name.module, name) &&
ft == typeof(getfield(ft.name.module, name))
n = length(methods(f))
m = n==1 ? "method" : "methods"
sname = string(name)
ns = (isself || '#' in sname) ? sname : string("(::", ft, ")")
what = startswith(ns, '@') ? "macro" : "generic function"
print(io, ns, " (", what, " with $n $m)")
end
end

function show(io::IO, ::MIME"text/plain", r::LinSpace)
# show 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
end

function show(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

show(io::IO, ::MIME"text/plain", X::AbstractArray) = _display(io, X)
show(io::IO, ::MIME"text/plain", r::AbstractRange) = show(io, r) # always use the compact form for printing ranges

function show(io::IO, ::MIME"text/plain", opt::JLOptions)
println(io, "JLOptions(")
fields = fieldnames(JLOptions)
nfields = length(fields)
for (i, f) in enumerate(fields)
v = getfield(opt, i)
if isa(v, Ptr{UInt8})
v = (v != C_NULL) ? unsafe_string(v) : ""
elseif isa(v, Ptr{Ptr{UInt8}})
v = unsafe_load_commands(v)
end
println(io, " ", f, " = ", repr(v), i < nfields ? "," : "")
end
print(io, ")")
end


# showing exception objects as descriptive error messages

"""
showerror(io, e)
Expand Down
153 changes: 153 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,158 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# first a few multiline show functions for types defined before the MIME type:

show(io::IO, ::MIME"text/plain", r::AbstractRange) = show(io, r) # always use the compact form for printing ranges

function show(io::IO, ::MIME"text/plain", r::LinSpace)
# show 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
end

function show(io::IO, ::MIME"text/plain", f::Function)
ft = typeof(f)
mt = ft.name.mt
if isa(f, Core.IntrinsicFunction)
show(io, f)
id = Core.Intrinsics.bitcast(Int32, f)
print(io, " (intrinsic function #$id)")
elseif isa(f, Core.Builtin)
print(io, mt.name, " (built-in function)")
else
name = mt.name
isself = isdefined(ft.name.module, name) &&
ft == typeof(getfield(ft.name.module, name))
n = length(methods(f))
m = n==1 ? "method" : "methods"
sname = string(name)
ns = (isself || '#' in sname) ? sname : string("(::", ft, ")")
what = startswith(ns, '@') ? "macro" : "generic function"
print(io, ns, " (", what, " with $n $m)")
end
end

function show(io::IO, ::MIME"text/plain", iter::Union{KeySet,ValueIterator})
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", isa(iter,KeySet) ? "Keys" : "Values", ":")
limit::Bool = get(io, :limit, false)
if limit
sz = displaysize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 4 && (cols = 4)
cols -= 2 # For prefix " "
rows -= 1 # For summary
else
rows = cols = typemax(Int)
end

for (i, v) in enumerate(iter)
print(io, "\n ")
i == rows < length(iter) && (print(io, ""); break)

if limit
str = sprint(show, v, context=io, sizehint=0)
str = _truncate_at_width_or_chars(str, cols, "\r\n")
print(io, str)
else
show(io, v)
end
end
end

function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
# show more descriptively, with one line per key/value pair
recur_io = IOContext(io, :SHOWN_SET => t)
limit::Bool = get(io, :limit, false)
if !haskey(io, :compact)
recur_io = IOContext(recur_io, :compact => true)
end

print(io, summary(t))
isempty(t) && return
print(io, ":")
show_circular(io, t) && return
if limit
sz = displaysize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 12 && (cols = 12) # Minimum widths of 2 for key, 4 for value
cols -= 6 # Subtract the widths of prefix " " separator " => "
rows -= 1 # Subtract the summary

# determine max key width to align the output, caching the strings
ks = Vector{AbstractString}(uninitialized, min(rows, length(t)))
vs = Vector{AbstractString}(uninitialized, min(rows, length(t)))
keylen = 0
vallen = 0
for (i, (k, v)) in enumerate(t)
i > rows && break
ks[i] = sprint(show, k, context=recur_io, sizehint=0)
vs[i] = sprint(show, v, context=recur_io, sizehint=0)
keylen = clamp(length(ks[i]), keylen, cols)
vallen = clamp(length(vs[i]), vallen, cols)
end
if keylen > max(div(cols, 2), cols - vallen)
keylen = max(cld(cols, 3), cols - vallen)
end
else
rows = cols = typemax(Int)
end

for (i, (k, v)) in enumerate(t)
print(io, "\n ")
i == rows < length(t) && (print(io, rpad("", keylen), " => ⋮"); break)

if limit
key = rpad(_truncate_at_width_or_chars(ks[i], keylen, "\r\n"), keylen)
else
key = sprint(show, k, context=recur_io, sizehint=0)
end
print(recur_io, key)
print(io, " => ")

if limit
val = _truncate_at_width_or_chars(vs[i], cols - keylen, "\r\n")
print(io, val)
else
show(recur_io, v)
end
end
end

function show(io::IO, ::MIME"text/plain", opt::JLOptions)
println(io, "JLOptions(")
fields = fieldnames(JLOptions)
nfields = length(fields)
for (i, f) in enumerate(fields)
v = getfield(opt, i)
if isa(v, Ptr{UInt8})
v = (v != C_NULL) ? unsafe_string(v) : ""
elseif isa(v, Ptr{Ptr{UInt8}})
v = unsafe_load_commands(v)
end
println(io, " ", f, " = ", repr(v), i < nfields ? "," : "")
end
print(io, ")")
end

function show(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


print(io::IO, s::Symbol) = (write(io,s); nothing)

"""
Expand Down
4 changes: 3 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ struct MIME{mime} end
macro MIME_str(s)
:(MIME{$(Expr(:quote, Symbol(s)))})
end
# fallback text/plain representation of any type:
show(io::IO, ::MIME"text/plain", x) = show(io, x)

# SIMD loops
include("simdloop.jl")
Expand Down Expand Up @@ -416,7 +418,7 @@ include("channels.jl")
include("deepcopy.jl")
include("interactiveutil.jl")
include("summarysize.jl")
include("replutil.jl")
include("errorshow.jl")
include("i18n.jl")
using .I18n

Expand Down
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function choosetests(choices = [])
"floatapprox", "stdlib", "reflection", "regex", "float16",
"combinatorics", "sysinfo", "env", "rounding", "ranges", "mod2pi",
"euler", "show",
"replutil", "sets", "goto", "llvmcall", "llvmcall2", "grisu",
"errorshow", "sets", "goto", "llvmcall", "llvmcall2", "grisu",
"some", "meta", "stacktraces", "libgit2", "docs",
"markdown", "misc", "threads",
"enums", "cmdlineargs", "i18n", "int",
Expand Down
Loading

0 comments on commit 78730ba

Please sign in to comment.