Skip to content

Commit 22b10d5

Browse files
committed
Better node & table print
1 parent 7ad8e1a commit 22b10d5

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/print_MTG/print.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function Base.print(node::Node; leading::AbstractString="", io::IO=stdout, limit
2525
if isroot(node)
2626
syms = attribute(node, :symbols, default=nothing)
2727
scs = attribute(node, :scales, default=nothing)
28-
syms === nothing || print(io, "Symbols: ", syms, "\n")
29-
scs === nothing || print(io, "Scales: ", scs, "\n")
28+
_print_symbols_scales(io, syms, scs)
3029
end
3130

3231
node_vec = get_printing(node; leading=leading)

src/types/ColumnTable.jl

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,65 @@ Tables.getcolumn(table::ColumnTable, i::Int) = table.cols[i]
116116
Tables.getcolumn(table::ColumnTable, name::Symbol) = table.cols[_column_idx(table, name)]
117117
Tables.schema(table::ColumnTable) = Tables.Schema(Tuple(table.col_names), Tuple(eltype.(table.cols)))
118118

119+
@inline function _pad_display_text(text::AbstractString, width::Int)
120+
pad = width - textwidth(text)
121+
return pad > 0 ? text * repeat(" ", pad) : String(text)
122+
end
123+
124+
@inline function _as_display_vector(x)
125+
if x isa AbstractVector
126+
return x
127+
elseif x isa Tuple
128+
return collect(x)
129+
else
130+
return nothing
131+
end
132+
end
133+
134+
function _print_symbols_scales(io::IO, symbols_meta, scales_meta)
135+
if symbols_meta === nothing && scales_meta === nothing
136+
return
137+
end
138+
139+
syms = _as_display_vector(symbols_meta)
140+
scales = _as_display_vector(scales_meta)
141+
142+
if syms !== nothing && scales !== nothing && !isempty(syms) && length(syms) == length(scales)
143+
n = length(syms)
144+
sym_cells = Vector{String}(undef, n)
145+
scale_cells = Vector{String}(undef, n)
146+
widths = Vector{Int}(undef, n)
147+
148+
@inbounds for i in eachindex(syms)
149+
sym_i = string(syms[i])
150+
scale_i = string(scales[i])
151+
sym_cells[i] = sym_i
152+
scale_cells[i] = scale_i
153+
widths[i] = max(textwidth(sym_i), textwidth(scale_i))
154+
end
155+
156+
@inbounds for i in eachindex(widths)
157+
sym_cells[i] = _pad_display_text(sym_cells[i], widths[i])
158+
scale_cells[i] = _pad_display_text(scale_cells[i], widths[i])
159+
end
160+
161+
label_w = max(textwidth("Symbols:"), textwidth("Scales:"))
162+
print(io, _pad_display_text("Symbols:", label_w), " ", join(sym_cells, " "), "\n")
163+
print(io, _pad_display_text("Scales:", label_w), " ", join(scale_cells, " "), "\n")
164+
return
165+
end
166+
167+
symbols_meta === nothing || print(io, "Symbols: ", symbols_meta, "\n")
168+
scales_meta === nothing || print(io, "Scales: ", scales_meta, "\n")
169+
end
170+
119171
function Base.show(io::IO, ::MIME"text/plain", table::ColumnTable)
120172
nrows, ncols = size(table)
121-
title = "ColumnTable($(nrows) x $(ncols))"
173+
title = "Attributes Table ($(nrows) x $(ncols))"
122174

123175
syms = get(table.metadata, :symbols, nothing)
124176
scales = get(table.metadata, :scales, nothing)
125-
syms === nothing || print(io, "Symbols: ", syms, "\n")
126-
scales === nothing || print(io, "Scales: ", scales, "\n")
177+
_print_symbols_scales(io, syms, scales)
127178

128179
if ncols == 0
129180
print(io, title)

0 commit comments

Comments
 (0)