Print two paragraphs side by side

Hi,

I'm a bit stuck. I'd like to output to the console two variables, each one containing a pargraph (~10-15 lines each) side by side.

println! ({} {}, x, x) obviously doesn't work because of EOF line characters (line break) which results displaying one paragraph after the other.

plz help :disappointed_relieved:

let left_pad = a.lines().map(|l| l.len()).max().unwrap_or(0);
for (a,b) in a.lines().zip(b.lines()) {
    println!("{:width$} {}", a, b, width = left_pad);
}

.zip() assumes both have equal amount of lines. If not, add lines to the shorter one.

If you're doing fancy layouts in the terminal, find a crate for it: https://crates.io/keywords/tui

4 Likes

thanks a lot @kornel :+1:

A crate such as textwrap can be complementary to @kornel's solution. Textwrap reformats a paragraph so that it fits a given line width, splitting at spaces or using hyphenation. There is even a function temwidth to query the width of the terminal.