Skip to content

Commit

Permalink
Better Layer printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel committed Jul 22, 2024
1 parent 20ee654 commit 67d666e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 16 additions & 0 deletions src/algebra/layer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/algebra/layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 14 additions & 1 deletion test/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit 67d666e

Please sign in to comment.