# \[ANN\] Announcing AutoPrettyPrinting.jl

**URL:** <https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410>\
**Category:** Package Announcements\
**Tags:** package, pretty-printing\
**Created:** [January 29, 2024, 5:30pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410 "2024-01-29T17:30:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![curtd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curtd/32/15868_2.png) [@curtd](https://discourse.julialang.org/u/curtd)\
**Post date:** [January 29, 2024, 5:30pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/1 "2024-01-29T17:30:12Z")

</div>

Hey everyone. I’m happy to announce [AutoPrettyPrinting.jl](https://github.com/curtd/AutoPrettyPrinting.jl) `v0.1`, which makes generating data-dependent pretty printing code a (relative) breeze. This package makes use of the excellent [PrettyPrinting.jl](https://github.com/MechanicalRabbit/PrettyPrinting.jl), along with some sane-ish layouting logic, to automatically choose, at object printing runtime, between a compact horizontal layout (for sufficiently small objects) and a more expansive vertical layout. It supports assigning different layout-generating code to different MIME-types, so that, e.g., your pretty printing code doesn’t have to overwrite external types that define their own `Base.show` methods for `MIME"text/plain"` types.

Using the `@def_pprint` macro, simple + complicated types will now print out nicely

```julia
julia> using AutoPrettyPrinting

julia> struct A 
           key1::Int 
           key2::String
       end

julia> @def_pprint mime_types="text/plain" base_show=true A

julia> a = A(10, "abcd")
A(key1 = 10, key2 = abcd)

```

This package was written with composition in mind, so that types composed of other types registered with this package “just work”

```julia
julia> struct B 
           data::Vector{A}
       end

julia> @def_pprint mime_types="text/plain" base_show=true B 

julia> b = B([A(i, "a"^i) for i in 1:2])
B(data = [A(key1 = 1, key2 = a), A(key1 = 2, key2 = aa)])

```

And when the objects themselves are too big to be printed nicely on a single line, the vertical layout for these objects is used.

```julia

julia> b = B([A(i, "a"^i) for i in 1:10])
B(
  data = [
           A(key1 = 1, key2 = a)
           A(key1 = 2, key2 = aa)
           A(key1 = 3, key2 = aaa)
           A(key1 = 4, key2 = aaaa)
           A(key1 = 5, key2 = aaaaa)
           A(key1 = 6, key2 = aaaaaa)
           A(key1 = 7, key2 = aaaaaaa)
           A(key1 = 8, key2 = aaaaaaaa)
           A(key1 = 9, key2 = aaaaaaaaa)
           A(key1 = 10, key2 = aaaaaaaaaa)
         ]
)

```

And if you want more control over how your objects are rendered, you can use the `@custom_tile` macro

```julia
julia> struct D 
           value::Int
       end

julia> @custom_tile mime_types="text/plain" base_show=true D => AutoPrettyPrinting.literal("CustomD $(_obj_.value^2)")

julia> d = D(10)
CustomD 100

```

Happy pretty printing!

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [January 29, 2024, 5:41pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/2 "2024-01-29T17:41:28Z")

</div>

This looks great! I’ve wanted prettier default printing for structs and especially NamedTuples in Base for a while now. Love to see efforts in this space!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [February 8, 2024, 8:08pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/3 "2024-02-08T20:08:42Z")

</div>

@curtd thank you for sharing this, it can save a lot of copy/paste in large projects.

Can you please comment on the current dependencies of AutoPrettyPrinting.jl? Can we reduce the list?

---

<div class="post-metadata">

**Author:** ![curtd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curtd/32/15868_2.png) [@curtd](https://discourse.julialang.org/u/curtd)\
**Post date:** [February 8, 2024, 10:18pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/5 "2024-02-08T22:18:48Z")

</div>

Probably not unfortunately! As far as the dependencies go,

- `Dates` and `Sockets` standard libraries are just there to provide pretty printing for types in those standard libraries.
- `MacroUtilities` provides the expression parsing for the various macros in the package
- `PrecompileTools` is standard for providing package precompilation
- `ScopedValues` provides the ability to change the generated layout dynamically, and
- `PrettyPrinting` does the actual heavy lifting of printing the objects

And there are package extensions for `TimeZones` and `Dictionaries`, for including those types into this framework.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [February 9, 2024, 11:57am UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/6 "2024-02-09T11:57:50Z")

</div>

> [@curtd](#):
>
> `Dates` and `Sockets` standard libraries are just there to provide pretty printing for types in those standard libraries.

Does it mean that the package is pirating the Base types, redefining their show methods?

Thanks for explaining the dependencies!

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [February 9, 2024, 1:31pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/7 "2024-02-09T13:31:03Z")

</div>

> [@curtd](#):
>
> `Dates` and `Sockets` standard libraries are just there to provide pretty printing for types in those standard libraries.

Couldn’t you use package extension for this as well? Just like for `TimeZones` and `Dictionaries`?

---

<div class="post-metadata">

**Author:** ![curtd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curtd/32/15868_2.png) [@curtd](https://discourse.julialang.org/u/curtd)\
**Post date:** [February 10, 2024, 7:04pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/8 "2024-02-10T19:04:06Z")

</div>

I remember trying that when I was developing the package but, as I recall, Julia kept complaining to me when I was trying to run precompilation workloads in the extensions. Possibly it’s because they’re stdlibs, possibly I did something wrong, I’m not sure. I’ll have another look at it when I have some time.

---

<div class="post-metadata">

**Author:** ![curtd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/curtd/32/15868_2.png) [@curtd](https://discourse.julialang.org/u/curtd)\
**Post date:** [February 10, 2024, 7:06pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/9 "2024-02-10T19:06:05Z")

</div>

Nope, no type piracy here. There’s just the `repr_pretty` function and the `PPrintContext` type to pretty print types, and tools for defining `Base.show` using this machinery for your own types.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 11, 2024, 2:13pm UTC](https://discourse.julialang.org/t/ann-announcing-autoprettyprinting-jl/109410/10 "2024-02-11T14:13:14Z")

</div>

> [@curtd](#):
>
> Julia kept complaining to me when I was trying to run precompilation workloads in the extensions.

That’s a known bug in Julia, [Strange "missing from cache"/"does not support precompilation"/... errors · Issue #52132 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/52132). Should be fixed in some upcoming new version.
