Skip to content

Commit

Permalink
support compact show mode (#113)
Browse files Browse the repository at this point in the history
* simplify show
* support compact show mode
* add tests
* bump
  • Loading branch information
aplavin authored Jun 8, 2023
1 parent 6d57418 commit e862651
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Accessors"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
authors = ["Takafumi Arakaki <[email protected]>", "Jan Weidner <[email protected]> and contributors"]
version = "0.1.31"
version = "0.1.32"

[deps]
CompositionsBase = "a33af91c-f02d-484b-be07-31d278c5ca2b"
Expand Down
16 changes: 10 additions & 6 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,18 +444,22 @@ function show_optic(io, optic)
show(io, opcompose(outer...))
print(io, "")
end
print(io, "(@optic ", reduce(_shortstring, inner; init="_"), ")")
shortstr = reduce(_shortstring, inner; init="_")
if get(io, :compact, false)
print(io, shortstr)
else
print(io, "(@optic ", shortstr, ")")
end
end

Base.show(io::IO, optic::Union{IndexLens, PropertyLens}) = print(io, "(@optic $(_shortstring("_", optic)))")
Base.show(io::IO, optic::Union{IndexLens, PropertyLens}) = show_optic(io, optic)
Base.show(io::IO, ::MIME"text/plain", optic::Union{IndexLens, PropertyLens}) = show(io, optic)

# only need show(io, optic) here because Base defines show(io::IO, ::MIME"text/plain", c::ComposedFunction) = show(io, c)
Base.show(io::IO, optic::ComposedFunction{<:Any, <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic)
# resolve method ambiguity:
# resolve method ambiguity with Base:
Base.show(io::IO, optic::ComposedFunction{typeof(!), <:Union{IndexLens, PropertyLens}}) = show_optic(io, optic)

Base.show(io::IO, ::MIME"text/plain", optic::Union{IndexLens, PropertyLens}) = show(io, optic)
Base.show(io::IO, ::MIME"text/plain", optic::ComposedFunction{<:Any, <:Union{IndexLens, PropertyLens}}) = show(io, optic)

# debugging
show_composition_order(optic) = (show_composition_order(stdout, optic); println())
show_composition_order(io::IO, optic) = show(io, optic)
Expand Down
7 changes: 7 additions & 0 deletions test/test_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ end
@test (@optic _ |> _[1] |> _[2] |> _[3]) === @optic _[1][2][3]
end

@testset "full and compact show" begin
@test sprint(show, (@optic _.a)) == "(@optic _.a)"
@test sprint(show, (@optic log(_.a[2]))) == "(@optic log(_.a[2]))"
@test sprint(show, (@optic log(_).a[2])) == "(@optic _.a[2]) ∘ log" # could be shorter, but difficult to dispatch correctly without piracy
@test sprint(show, (@optic log(_.a[2])); context=:compact => true) == "log(_.a[2])"
end

@testset "text/plain show" begin
@testset for lens in [
LensIfTextPlain()
Expand Down

2 comments on commit e862651

@aplavin
Copy link
Member Author

@aplavin aplavin commented on e862651 Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/85130

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.32 -m "<description of version>" e862651516f2164da557d77b7675d70993745966
git push origin v0.1.32

Please sign in to comment.