Skip to content

Commit 836f818

Browse files
committed
Non-breaking stacktrace printing. Fixes #92
1 parent 3e44874 commit 836f818

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ComponentArrays"
22
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
33
authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"]
4-
version = "0.10.1"
4+
version = "0.10.2"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/show.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ Base.show(io::IO, ci::ComponentIndex) = print(io, "ComponentIndex($(ci.idx), $(c
2626

2727

2828
# Show ComponentArrays
29-
# Show ComponentArray types (this is pretty hacky because something is broken when I try to do it normally)
30-
Base.show(io::IO, ::MIME"text/plain", ::Type{ComponentArray{T,N,A,Ax}}) where {T,N,A,Ax} = print(io, "ComponentArray{$T,$N,$A,$Ax}") # make `typeof(u)` show the full type
31-
Base.show(io::IO, ::Type{<:ComponentArray{T,N,<:Array}}) where {T,N} = print(io, "ComponentArray{$T,$N}") # do not pollute the stacktrace with verbose type printing
32-
Base.show(io::IO, ::Type{<:ComponentArray{T,1,<:Array}}) where {T} = print(io, "ComponentVector{$T}")
33-
Base.show(io::IO, ::Type{<:ComponentArray{T,2,<:Array}}) where {T} = print(io, "ComponentMatrix{$T}")
34-
Base.show(io::IO, ::Type{<:ComponentArray{T,N,<:SubArray}}) where {T,N} = print(io, "ComponentArray{$T,$N,SubArray...}") # do not pollute the stacktrace with verbose type printing
35-
Base.show(io::IO, ::Type{<:ComponentArray{T,1,<:SubArray}}) where {T} = print(io, "ComponentVector{$T,SubArray...}")
36-
Base.show(io::IO, ::Type{<:ComponentArray{T,2,<:SubArray}}) where {T} = print(io, "ComponentMatrix{$T,SubArray...}")
29+
_print_type_short(io, ca; color=:normal) = _print_type_short(io, typeof(ca); color=color)
30+
_print_type_short(io, T::Type; color=:normal) = printstyled(io, T; color=color)
31+
_print_type_short(io, ::Type{<:ComponentArray{T,N,<:Array}}; color=:normal) where {T,N} = printstyled(io, "ComponentArray{$T,$N}"; color=color) # do not pollute the stacktrace with verbose type printing
32+
_print_type_short(io, ::Type{<:ComponentArray{T,1,<:Array}}; color=:normal) where {T} = printstyled(io, "ComponentVector{$T}"; color=color)
33+
_print_type_short(io, ::Type{<:ComponentArray{T,2,<:Array}}; color=:normal) where {T} = printstyled(io, "ComponentMatrix{$T}"; color=color)
34+
_print_type_short(io, ::Type{<:ComponentArray{T,N,<:SubArray}}; color=:normal) where {T,N} = printstyled(io, "ComponentArray{$T,$N,SubArray...}"; color=color) # do not pollute the stacktrace with verbose type printing
35+
_print_type_short(io, ::Type{<:ComponentArray{T,1,<:SubArray}}; color=:normal) where {T} = printstyled(io, "ComponentVector{$T,SubArray...}"; color=color)
36+
_print_type_short(io, ::Type{<:ComponentArray{T,2,<:SubArray}}; color=:normal) where {T} = printstyled(io, "ComponentMatrix{$T,SubArray...}"; color=color)
37+
38+
@static if VERSION >= v"1.6"
39+
Base.print_type_stacktrace(io, CA::Type{<:ComponentArray}; color=:normal) = _print_type_short(io, CA; color=color)
40+
end
3741

3842
function Base.show(io::IO, x::ComponentVector)
3943
print(io, "(")
@@ -50,28 +54,20 @@ function Base.show(io::IO, x::ComponentVector)
5054
end
5155

5256
function Base.show(io::IO, ::MIME"text/plain", x::ComponentVector)
53-
show(io, typeof(x))
57+
_print_type_short(io, x)
5458
show(io, x)
5559
return nothing
5660
end
5761

58-
function Base.show(io::IO, a::AbstractVector{<:T}) where T<:ComponentVector
59-
elem = a[1]
60-
print(io, "[$elem")
61-
for idx in 2:length(a)
62-
print(io, ", $(a[idx])")
63-
end
64-
print(io, "]")
65-
return nothing
66-
end
67-
6862
function Base.show(io::IO, ::MIME"text/plain", x::ComponentMatrix{T,A,Axes}) where {T,A,Axes}
6963
if !haskey(io, :compact) && length(axes(x, 2)) > 1
7064
io = IOContext(io, :compact => true)
7165
end
7266
axs = getaxes(x)
7367
sz = size(x)
74-
println(io, "$(sz[1])×$(sz[2]) $(typeof(x)) with axes $(axs[1]) × $(axs[2])")
68+
print(io, "$(sz[1])×$(sz[2]) ")
69+
_print_type_short(io, x)
70+
println(io, " with axes $(axs[1]) × $(axs[2])")
7571
Base.print_matrix(io, getdata(x))
7672
return nothing
7773
end

0 commit comments

Comments
 (0)