# Nested indentation in show

**URL:** https://discourse.julialang.org/t/nested-indentation-in-show/7191
**Category:** General Usage
**Tags:** question
**Created:** [November 20, 2017, 3:17pm UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191 "2017-11-20T15:17:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 20, 2017, 3:17pm UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/1 "2017-11-20T15:17:58Z")

</div>

When writing some methods for `show` for types in a package, I was wondering if there is any mechanism (or package) for reusing `show` methods for fields but nesting the indentation.

Eg suppose a type prints as

```julia
This is a Foo with 3x2 elements.
    [1.0 1.0 1.0; 1.0 1.0 1.0; 1.0 1.0 1.0]

```

and a type `Bar` prints as

```julia
This is a Bar with critical value 0.7

```

Then for a type `Baz`

```julia
struct Bar
    foo::Foo 
    bar::Bar 
end

```

is there an organized way to write a `show` method that

1. reuses `show` code for the fields,
2. indents the fields.

Eg

```julia
This is a Bar with fields
    This is a Foo with 3x2 elements.
        [1.0 1.0 1.0; 1.0 1.0 1.0; 1.0 1.0 1.0]
    This is a Bar with critical value 0.7    

```

Solutions I have thought of:

1. use something other than indentation, eg `•`,
2. pass around the current indentation as an optional 3rd argument of `show`, defaulting to `0`.

Neither of these is very clean.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [November 20, 2017, 3:30pm UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/2 "2017-11-20T15:30:26Z")

</div>

> [@Tamas\_Papp](#):
>
> pass around the current indentation as an optional 3rd argument of show, defaulting to 0.

I’m not aware of a generic solution for indentation, but the official way to pass arguments to `show` is to use `IOContext`. Eg. `show(IOContext(indent=get(io, :indent, 0)+2), mime, child_object)`. See also [this example](https://github.com/cstjean/FProfile.jl/blob/3cbfdb2e0e99a113366bf1df1da5882d19e583de/src/FProfile.jl#L293-L311) for displaying a tree. It works, but I do dearly miss CL’s special variables.

---

<div class="post-metadata">

### Author: ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)
#### Post date: [November 20, 2017, 3:32pm UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/3 "2017-11-20T15:32:24Z")

</div>

When I’ve need to do this in the past I’ve added an `indent` field to  
[`IOContext`]  
([https://docs.julialang.org/en/stable/stdlib/io-network/#Base.IOContext](https://docs.julialang.org/en/stable/stdlib/io-network/#Base.IOContext))  
that gets added to whenever we need to nest another level. It won’t work  
with `Base` types or types from other packages, but within a package you  
can use it for all your types that could be nested within each other.  
-s

---

<div class="post-metadata">

### Author: ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)
#### Post date: [April 4, 2024, 9:59am UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/4 "2024-04-04T09:59:26Z")

</div>

updated link  
[https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext)

---

<div class="post-metadata">

### Author: ![saulrh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saulrh/32/214498_2.png) [@saulrh](https://discourse.julialang.org/u/saulrh)
#### Post date: [January 3, 2025, 7:34am UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/5 "2025-01-03T07:34:39Z")

</div>

If you want a kludgey half-solution that works even for types you don’t own, you can have your types make and pass down an `IOBuffer` instead of the `io` arg when they recurse into `show` on their children, then re-emit each line of that buffer with indentation added to the front. It looks sort of like this:

```julia
function Base.show(io::IO, mime, x::Bar)
    println(io, "This is a Bar with fields")
    buffer = IOBuffer()
    for c in x.children
        show(buffer, mime, c)
        for line in split(String(take!(buffer)), "\n")
            println(io, " ", line)
        end
    end
end

```

Other types won’t be able to add new levels of indentation, so e.g. `Dict`s might be a touch ugly, but at least now even multiline children will be properly indented inside your own types.

(Yes, the performance is horrible, almost certainly outright quadratic in the depth unless `IOBuffer` and `String` and `take!` are all doing some _deep_ cooperative pointer-shuffling magic, but it’s pretty-printing for human inspection so N is like eight at most, lol. 😛 )

---

<div class="post-metadata">

### Author: ![frankier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankier/32/35101_2.png) [@frankier](https://discourse.julialang.org/u/frankier)
#### Post date: [June 17, 2025, 11:32am UTC](https://discourse.julialang.org/t/nested-indentation-in-show/7191/6 "2025-06-17T11:32:02Z")

</div>

I agree that that code might be the best pragmatic solution at the moment. There has been [GitHub - KristofferC/IOIndents.jl: Simple writing of indented and aligned text](https://github.com/KristofferC/IOIndents.jl) and [GitHub - tpapp/IndentWrappers.jl: Wrapper type for indentation management for plain text printing.](https://github.com/tpapp/IndentWrappers.jl) and [GitHub - tpapp/IndentWrappers.jl: Wrapper type for indentation management for plain text printing.](https://github.com/tpapp/IndentWrappers.jl) but both have been abandoned.
