# Printing with nested indentation

**URL:** https://discourse.julialang.org/t/printing-with-nested-indentation/122924
**Category:** General Usage
**Tags:** io, pretty-printing
**Created:** [November 22, 2024, 6:59am UTC](https://discourse.julialang.org/t/printing-with-nested-indentation/122924 "2024-11-22T06:59:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 22, 2024, 6:59am UTC](https://discourse.julialang.org/t/printing-with-nested-indentation/122924/1 "2024-11-22T06:59:12Z")

</div>

Is there some generic reusable solution for the problem of printing nested objects with indentation? For example a custom show method for an array-like type where each item should be printed with an indent. But the items themselves might need indents, too, so you cannot hardcore the amount of spaces. I tend to reimplement that whenever needed but it seems like this should be an `IOContext` parameter or something.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 22, 2024, 8:33am UTC](https://discourse.julialang.org/t/printing-with-nested-indentation/122924/2 "2024-11-22T08:33:12Z")

</div>

The package PrettyPrint.jl does some sort of nested indentation, is it adequate?

Example with a `Vector{Vector{Vector{Int64}}}`:

```julia
using PrettyPrint
w = [[collect(1:rand(1:10)) for _ in 1:5] for _ in 1:3]
pprint(w)

# output:
[
  [
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2],
  ],
  [
    [1, 2, 3],
    [1, 2, 3, 4],
    [1, 2, 3, 4, 5, 6],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [1, 2, 3, 4, 5, 6, 7],
  ],
  [
    [1,],
    [1, 2, 3, 4, 5, 6, 7],
    [1, 2],
    [1, 2, 3, 4, 5, 6, 7, 8, 9],
    [1, 2, 3, 4],
  ],
]

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 22, 2024, 9:14am UTC](https://discourse.julialang.org/t/printing-with-nested-indentation/122924/3 "2024-11-22T09:14:59Z")

</div>

Thanks, although I was rather looking for a simpler solution that only handled indented printing. I found this which looks like it could work [GitHub - tpapp/IndentWrappers.jl: Wrapper type for indentation management for plain text printing.](https://github.com/tpapp/IndentWrappers.jl)

---

<div class="post-metadata">

### Author: ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)
#### Post date: [November 22, 2024, 9:30am UTC](https://discourse.julialang.org/t/printing-with-nested-indentation/122924/4 "2024-11-22T09:30:07Z")

</div>

IndentWrappers.jl looks like a very nice solution for that kind of problem. I was looking for something like that before but ended up writing my own messy solution. Please let us know if your going to revamp that package!

My personal wishlist for those IO wrappers would also include:

- available line width (so you can line wrap things for the given terminal size without breaking the indent)
- more complex prefixes besides spaces, for example something like `lineprefix="│ "` for nested tree-like printing with unicode box drawings.
