# Package to format simple text output

**URL:** https://discourse.julialang.org/t/package-to-format-simple-text-output/73481
**Category:** New to Julia
**Tags:** question, package, strings
**Created:** [December 22, 2021, 11:27am UTC](https://discourse.julialang.org/t/package-to-format-simple-text-output/73481 "2021-12-22T11:27:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [December 22, 2021, 11:27am UTC](https://discourse.julialang.org/t/package-to-format-simple-text-output/73481/1 "2021-12-22T11:27:49Z")

</div>

Ok, there is the nice module [PrettyTables](https://github.com/ronisbr/PrettyTables.jl), but it seems that I am looking for something else.  
I would like to structure my text output regarding column width and I would like to plot separation  
lines. At the end I gave up to search for a suitable module and coded something that goes into the  
right direction. Below I share my code and I am interested, if there is a package available that does  
similar things.

```julia
txt_in = string("Some time ago, I had a dream, it was happy it was lasting, it was free! ",
          "And now in life, oh, can't you see how we can make that dream into reality? ",
          "Oh, the music, it was playing, oh, the firelight, it was dancing. ",
          "All the children they were singing. All the people they were loving. ",
          "From: Chick Corea – \"Sometime Ago/La Fiesta\""
          )

function h_line(;column_width::Integer = 100, char_::Char = '-')
    println(string(char_, " ", char_^(column_width - 6), " ", char_))
    return nothing
end

function text_block(txt_in::String = ""; column_width::Integer = 100)
    idx_frst = 1
    idx_last = 1
    length_txt = length(txt_in)
    n_lines = Integer(ceil(length_txt/column_width))
    for i_lines = 1 : n_lines
        global idx_frst, idx_last, txt_in, column_width
        idx_last = findlast(" ",
                   txt_in[range(1, min(idx_frst + column_width, length_txt)
                                )
                        ]
                   )[1]
        if i_lines < n_lines
            println(txt_in[idx_frst:idx_last])
        else
            println(txt_in[idx_frst:end])
        end
        idx_frst = idx_last + 1
    end
    return nothing
end

h_line()
text_block(txt_in)
h_line(char_ = '#')

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 22, 2021, 11:47am UTC](https://discourse.julialang.org/t/package-to-format-simple-text-output/73481/2 "2021-12-22T11:47:27Z")

</div>

Nice. No need for those “global” statements now that you have the code wrapped in a function.

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [December 22, 2021, 11:50am UTC](https://discourse.julialang.org/t/package-to-format-simple-text-output/73481/3 "2021-12-22T11:50:41Z")

</div>

Thanks for the tip! I have removed this unnecessary “global” statements.

---

<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: [December 22, 2021, 11:55am UTC](https://discourse.julialang.org/t/package-to-format-simple-text-output/73481/4 "2021-12-22T11:55:38Z")

</div>

There is a related [post here](https://discourse.julialang.org/t/line-feed-by-spaces/71439) with some code samples and a link to a [TextWrap.jl](https://discourse.julialang.org/t/line-feed-by-spaces/71439/11) package.
