From 67d666e2f8fcadc7b103e8a7f864dae46e46d3d9 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Mon, 22 Jul 2024 12:00:36 +0200 Subject: [PATCH] Better `Layer` printing --- Project.toml | 2 +- src/algebra/layer.jl | 16 ++++++++++++++++ src/algebra/layers.jl | 9 +++++++++ test/algebra.jl | 15 ++++++++++++++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 838d6a6ea..440d526be 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AlgebraOfGraphics" uuid = "cbdf2221-f076-402e-a563-3d30da359d67" authors = ["Pietro Vertechi", "Julius Krumbiegel"] -version = "0.7.1" +version = "0.7.2" [deps] Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" diff --git a/src/algebra/layer.jl b/src/algebra/layer.jl index 52a715b06..28897a2de 100644 --- a/src/algebra/layer.jl +++ b/src/algebra/layer.jl @@ -512,3 +512,19 @@ function compute_attributes(pl::ProcessedLayer, # remove unnecessary information return filterkeys(!in((:col, :row, :layout, :alpha, :group)), attrs) end + +function Base.show(_io::IO, l::Layer; indent = 0, index = nothing) + io = IOContext(_io, :limit => true) + ind = " " ^ indent + printstyled(io, ind, "Layer ", index === nothing ? "" : index, "\n", bold = true) + println(io, ind, " transformation: ", l.transformation) + println(io, ind, " data: ", typeof(l.data)) # print only type here as data source could be anything and print a lot of stuff + println(io, ind, " positional:") + for (i, pos) in enumerate(l.positional) + println(io, ind, " ", i, ": ", pos) + end + println(io, ind, " named:") + for (name, named) in pairs(l.named) + println(io, ind, " ", name, ": ", named) + end +end diff --git a/src/algebra/layers.jl b/src/algebra/layers.jl index 65e9645c6..1b95d0cc1 100644 --- a/src/algebra/layers.jl +++ b/src/algebra/layers.jl @@ -453,4 +453,13 @@ function to_entry(P::Type{Heatmap}, p::ProcessedLayer, categoricalscales::Dictio ] Entry(P, positional, merge(p.named, p.primary, p.attributes, color_attributes)) +end + +function Base.show(io::IO, layers::Layers; indent = 0) + ind = " " ^ indent + printstyled(io, ind, "Layers", bold = true) + println(io, ind, " with $(length(layers.layers)) elements:") + for (i, layer) in enumerate(layers) + show(io, layer; indent = indent + 1, index = i) + end end \ No newline at end of file diff --git a/test/algebra.jl b/test/algebra.jl index 47ffd804f..967938992 100644 --- a/test/algebra.jl +++ b/test/algebra.jl @@ -131,4 +131,17 @@ end @test processedlayers[6].positional[1] == exp.(df.x[df.c .== "c"]) @test processedlayers[6].positional[2] == df.z[df.c .== "c"] @test processedlayers[6].named[:markersize] == df.w[df.c .== "c"] -end \ No newline at end of file +end + +@testset "printing" begin + spec = data((; x = 1:10, y = 11:20)) * mapping(:x, :y, color = :y) * visual(BarPlot) + mapping(:x) * visual(HLines) + # testing printing exactly is finicky across Julia versions, just make sure it's not completely broken + printout = @test_nowarn repr(spec) + @test occursin("Layers with 2 elements", printout) + @test occursin("Layer 1", printout) + @test occursin("Layer 2", printout) + @test occursin("transformation:", printout) + @test occursin("data:", printout) + @test occursin("positional:", printout) + @test occursin("named:", printout) +end