Skip to content

Commit 01597e3

Browse files
committed
simplify gradedarray printing
1 parent 89dfa6c commit 01597e3

File tree

1 file changed

+28
-50
lines changed

1 file changed

+28
-50
lines changed

src/spaces/gradedspace.jl

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -200,72 +200,50 @@ end
200200
Base.summary(io::IO, V::GradedSpace) = print(io, type_repr(typeof(V)))
201201

202202
function Base.show(io::IO, V::GradedSpace)
203-
pre = (get(io, :typeinfo, Any)::DataType == typeof(V) ? "" : type_repr(typeof(V)))
204-
205-
io = IOContext(io, :typeinfo => Pair{sectortype(V), Int})
206-
207-
pre *= "("
203+
opn = (get(io, :typeinfo, Any)::DataType == typeof(V) ? "" : type_repr(typeof(V)))
204+
opn *= "("
208205
if isdual(V)
209-
post = ")'"
206+
cls = ")'"
210207
V = dual(V)
211208
else
212-
post = ")"
209+
cls = ")"
213210
end
214-
hdots = " \u2026 "
215-
sep = ", "
216-
sepsize = length(sep)
217211

218-
limited = get(io, :limit, false)::Bool
219-
screenwidth = limited ? displaysize(io)[2] : typemax(Int)
220-
screenwidth -= length(pre) + length(post)
212+
v = [c => dim(V, c) for c in sectors(V)]
221213

222-
cs = reshape(collect([c => dim(V, c) for c in sectors(V)]), 1, :)
223-
ncols = length(cs)
224-
225-
maxpossiblecols = screenwidth ÷ (1 + sepsize)
226-
if ncols > maxpossiblecols
227-
cols = [1:(maxpossiblecols - 1); (ncols - maxpossiblecols + 1):ncols]
214+
# logic stolen from Base.show_vector
215+
limited = get(io, :limit, false)::Bool
216+
io = IOContext(io, :typeinfo => eltype(v))
217+
218+
if limited && length(v) > 20
219+
axs1 = axes(v, 1)
220+
f, l = first(axs1), last(axs1)
221+
Base.show_delim_array(io, v, opn, ",", "", false, f, f + 9)
222+
print(io, "")
223+
Base.show_delim_array(io, v, "", ",", cls, false, l - 9, l)
228224
else
229-
cols = collect(1:ncols)
225+
Base.show_delim_array(io, v, opn, ",", cls, false)
230226
end
231-
232-
A = Base.alignment(io, cs, [1], cols, screenwidth, screenwidth, sepsize, ncols)
233-
234-
if ncols <= length(A) # fits on screen
235-
print(io, pre)
236-
Base.print_matrix_row(io, cs, A, 1, cols, sep, ncols)
237-
print(io, post)
238-
else # doesn't fit on screen
239-
half = (screenwidth - length(hdots) + 1) ÷ 2 + 1
240-
Ralign = reverse(Base.alignment(io, cs, [1], reverse(cols), half, half, sepsize, ncols))
241-
half = screenwidth - sum(map(sum, Ralign)) - (length(Ralign) - 1) * sepsize - length(hdots)
242-
Lalign = Base.alignment(io, cs, [1], cols, half, half, sepsize, ncols)
243-
print(io, pre)
244-
Base.print_matrix_row(io, cs, Lalign, 1, cols[1:length(Lalign)], sep, ncols)
245-
print(io, hdots)
246-
Base.print_matrix_row(io, cs, Ralign, 1, (length(cs) - length(Ralign)) .+ cols, sep, length(cs))
247-
print(io, post)
248-
end
249-
250227
return nothing
251228
end
252229

253230
function Base.show(io::IO, ::MIME"text/plain", V::GradedSpace)
254-
# print small summary, e.g.
255-
# d-element Vect[I] or d-element dual(Vect[I])
256-
d = reduceddim(V)
257-
print(io, d, "-element ")
258-
isdual(V) && print(io, "dual(")
259-
print(io, type_repr(typeof(V)))
260-
isdual(V) && print(io, ")")
231+
# print small summary, e.g.: Vect[I](…) of dim d
232+
d = dim(V)
233+
print(io, type_repr(typeof(d)), "(…)")
234+
isdual(V) && print(io, "'")
235+
print(io, " of dim ", d)
261236

262237
compact = get(io, :compact, false)::Bool
263238
(iszero(d) || compact) && return nothing
264239

265-
# print detailed sector information
266-
print(io, ":\n ")
267-
ioc = IOContext(io, :typeinfo => typeof(V))
268-
show(ioc, V)
240+
# print detailed sector information - hijack Base.Vector printing
241+
print(io, ":\n")
242+
isdual(V) && (V = dual(V))
243+
print_data = [c => dim(V, c) for c in sectors(V)]
244+
ioc = IOContext(io, :typeinfo => eltype(print_data))
245+
Base.print_matrix(ioc, print_data)
246+
269247
return nothing
270248
end
271249

0 commit comments

Comments
 (0)