Skip to content

Commit

Permalink
move show code to Common area
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Jan 23, 2024
1 parent 2adb0b9 commit 004890e
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/tangent_types/structural_tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ function Base.map(f, tangent::StructuralTangent{P}) where {P}
end
end


function Base.show(io::IO, tangent::StructuralTangent{P}) where {P}
if tangent isa MutableTangent
print(io, "Mutable")
end
print(io, "Tangent{")
str = sprint(show, P, context = io)
i = findfirst('{', str)
if isnothing(i)
print(io, str)
else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line:
print(io, str[1:prevind(str, i)])
if length(str) < 80
printstyled(io, str[i:end], color=:light_black)
else
printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black)
end
end
print(io, "}")
if isempty(backing(tangent))
print(io, "()") # so it doesn't show `NamedTuple()`
else
# allow Tuple or NamedTuple `show` to do the rendering of brackets etc
show(io, backing(tangent))
end
end

"""
backing(x)
Expand Down Expand Up @@ -334,32 +361,6 @@ Base.:(==)(a::Tangent{P}, b::Tangent{Q}) where {P,Q} = false

Base.hash(a::Tangent, h::UInt) = Base.hash(backing(canonicalize(a)), h)

function Base.show(io::IO, tangent::StructuralTangent{P}) where {P}
if tangent isa MutableTangent
print(io, "Mutable")
end
print(io, "Tangent{")
str = sprint(show, P, context = io)
i = findfirst('{', str)
if isnothing(i)
print(io, str)
else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line:
print(io, str[1:prevind(str, i)])
if length(str) < 80
printstyled(io, str[i:end], color=:light_black)
else
printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black)
end
end
print(io, "}")
if isempty(backing(tangent))
print(io, "()") # so it doesn't show `NamedTuple()`
else
# allow Tuple or NamedTuple `show` to do the rendering of brackets etc
show(io, backing(tangent))
end
end

Base.iszero(::Tangent{<:,NamedTuple{}}) = true
Base.iszero(::Tangent{<:,Tuple{}}) = true

Expand Down

0 comments on commit 004890e

Please sign in to comment.