ascii-boxes / boxes

Command line ASCII boxes unlimited!
https://boxes.thomasjensen.com/
GNU General Public License v3.0
613 stars 75 forks source link

How to align / center a box within the terminal window? #86

Closed Davidy22 closed 3 years ago

Davidy22 commented 3 years ago

Figlet has the -lrcx options for aligning the output and -tw for either getting the terminal width or setting an outer width bound for purposes of justfying the final output. -t and -w are meaningless in the context of this program, but it'd be nice if boxes could have some equivalent of figlet's -lrcx flags so that I can have a pretty box with text print in the middle or right of my terminal instead of always on the left. Probably need to do something equivalent to figlet's -t to get the terminal width so that these flags can work.

tsjensen commented 3 years ago

Well, boxes has -a (docs) which should be very similar to Figlet's -lrcx.
You can even center Figlet text, e.g. figlet foo | boxes -a hc -s 50.

Terminal width is normally something that boxes is agnostic of, because boxes will always try to match the input text. Additional padding can be specified with -p.

Let me know if maybe that already solves your case!

tsjensen commented 3 years ago

I just found that on my terminal (Debian / bash), I can

figlet "Hello World !" | boxes -d peek -a hc -s $COLUMNS

To get a box which spans exactly the terminal width. Don't know who keeps the $COLUMNS up-to-date, but it works out of the box.

Davidy22 commented 3 years ago

The -p and -a flags let me shift the text around inside an oversized box, but I want to realign the box itself to be centered in the terminal, but still fitted to the width of the text. $COLUMNS gets us a bit of the way there, but currently we only have the option to have our box take up the full width, can't center or right justify a smaller box within the width of $COLUMNS.

tsjensen commented 3 years ago

Ah, I think I get your point now. Thanks for clarifying.
Try this.

Centered box with figlet text:

figlet centered | boxes -d unicornthink | boxes -s $COLUMNS -c x -a hc -i none | cut -c 2-

Right-aligned box with figlet text:

figlet right-aligned | boxes -d unicornthink | boxes -s $COLUMNS -c x -a hr -i none | sed -e 's/^x/ /'

The basic idea is to call boxes twice, the second time with only a few x in the first column of the result, which we remove again with the cut command. In this way, boxes centers the box.
Some googling gave me that one might use $(tput cols) instead of $COLUMNS for slightly better coverage of different shells.

Davidy22 commented 3 years ago

well i guess that works, a lotta extra work over just a flag. Actually if this is going to be the recommended approach, maybe there could be a blank "box" that prints nothing, is only for alignment purposes and is appropriately named, so that the command chain gets cut down to:

figlet right-aligned | boxes -d unicornthink | boxes -s $COLUMNS -d align -a hr

Saving a sed or cut command, making this a lot more discoverable and easier to figure out independently. A box that prints nothing should be easy enough for me to write actually.

tsjensen commented 3 years ago

In order to make this easier to discover, I added a section to the FAQ.

While trying out the commands above, I found that a blank box actually causes an error boxes may not consist entirely of whitespace. I'll open a new issue as a reminder to myself to find out why that is. Maybe we can enable a new use case for boxes.

In general, unix philosophy says that tools should do one thing, and do it well. So, aligning stuff in a terminal window should be a task for another tool. (On Mac, fmt is supposed to be able to do that.) It would be hard for boxes to reliably determine the terminal width on all of the platforms it runs on. For example, I wouldn't know how to do this on Windows.

Davidy22 commented 3 years ago

I guess the blank box for formatting would handle this well enough, closing in favor of the other ticket