Display two columns in terminal such that multiple lines can be copied from one column

45 Views Asked by At

When implementing a CLI that shows two columns, one possible implementation is to fill the left column with spaces to align the right column. Example implementation:

from itertools import zip_longest
from shutil import get_terminal_size

left_lines = ["aaa", "bbb"]
right_lines = ["xxx", "yyy", "zzz"]

columns = min(get_terminal_size().columns, 80)
column_width = (columns - 1) // 2

for left, right in zip_longest(left_lines, right_lines, fillvalue=""):
    assert len(left) <= column_width
    assert len(right) <= column_width
    print(left.ljust(column_width) + "|" + right)

Resulting in:

aaa                                    |xxx
bbb                                    |yyy
                                       |zzz

However, that causes the problem that it’s not possible to copy multiple lines from one column. E.g. when trying to copy the lines “aaa” and “bbb” from the left column (in my case in urxvt using primary selection or the clipboard), I get:

aaa                                    |xxx
bbb

Is there another implementation that avoids this problem, at least in some terminal emulators?

0

There are 0 best solutions below