Package to format simple text output

Ok, there is the nice module PrettyTables, 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.

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_ = '#')
1 Like

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

1 Like

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

There is a related post here with some code samples and a link to a TextWrap.jl package.

1 Like