Skip to content

Commit c872256

Browse files
authored
Compact printing of NamedDimsArray type (#48)
1 parent f2535d1 commit c872256

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NamedDimsArrays"
22
uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractnameddimsarray.jl

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,47 @@ end
935935

936936
# Printing
937937

938+
# Copy of `Base.dims2string` defined in `show.jl`.
939+
function dims_to_string(d)
940+
isempty(d) && return "0-dimensional"
941+
length(d) == 1 && return "$(d[1])-element"
942+
return join(map(string, d), '×')
943+
end
944+
945+
using TypeParameterAccessors: type_parameters, unspecify_type_parameters
946+
function concretetype_to_string_truncated(type::Type; param_truncation_length=typemax(Int))
947+
isconcretetype(type) || throw(ArgumentError("Type must be concrete."))
948+
alias = Base.make_typealias(type)
949+
base_type, params = if isnothing(alias)
950+
unspecify_type_parameters(type), type_parameters(type)
951+
else
952+
base_type_globalref, params_svec = alias
953+
base_type_globalref.name, params_svec
954+
end
955+
str = string(base_type)
956+
if isempty(params)
957+
return str
958+
end
959+
str *= '{'
960+
param_strings = map(params) do param
961+
param_string = string(param)
962+
if length(param_string) > param_truncation_length
963+
return ""
964+
end
965+
return param_string
966+
end
967+
str *= join(param_strings, ", ")
968+
str *= '}'
969+
return str
970+
end
971+
972+
function Base.summary(io::IO, a::AbstractNamedDimsArray)
973+
print(io, dims_to_string(nameddimsindices(a)))
974+
print(io, ' ')
975+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
976+
return nothing
977+
end
978+
938979
function Base.show(io::IO, mime::MIME"text/plain", a::AbstractNamedDimsArray)
939980
summary(io, a)
940981
println(io)
@@ -943,7 +984,8 @@ function Base.show(io::IO, mime::MIME"text/plain", a::AbstractNamedDimsArray)
943984
end
944985

945986
function Base.show(io::IO, a::AbstractNamedDimsArray)
946-
print(io, "nameddimsarray(")
987+
show(io, unspecify_type_parameters(typeof(a)))
988+
print(io, "(")
947989
show(io, dename(a))
948990
print(io, ", ", nameddimsindices(a), ")")
949991
return nothing

test/test_basics.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,21 @@ using Test: @test, @test_throws, @testset
368368
@test s′[3] == "c"
369369
end
370370
end
371+
@testset "show" begin
372+
a = NamedDimsArray([1 2; 3 4], ("i", "j"))
373+
function ref(prefix)
374+
return "named(Base.OneTo(2), \"i\")×named(Base.OneTo(2), \"j\") $(prefix)NamedDimsArray{Int64, 2, Matrix{Int64}, …}\n2×2 Matrix{Int64}:\n 1 2\n 3 4"
375+
end
376+
res = sprint(show, "text/plain", a)
377+
# Could be either one depending on the namespacing.
378+
@test (res == ref("")) || (res == ref("NamedDimsArrays."))
379+
380+
a = NamedDimsArray([1 2; 3 4], ("i", "j"))
381+
function ref(prefix)
382+
return "$(prefix)NamedDimsArray([1 2; 3 4], (named(Base.OneTo(2), \"i\"), named(Base.OneTo(2), \"j\")))"
383+
end
384+
res = sprint(show, a)
385+
# Could be either one depending on the namespacing.
386+
@test (res == ref("")) || (res == ref("NamedDimsArrays."))
387+
end
371388
end

0 commit comments

Comments
 (0)