Linux Format

TERMINAL: Boxes

Bash ninjas do more than just write scripts for fun. Sometimes they do serious work, like creating ASCII art. Shashank Sharma shares how…

- Shashank Sharma is a trial lawyer in Delhi and avid Arch Linux user, he’s currently busy rolling his eyes at his brother using Manjaro..

Bash ninjas do more than just write scripts for fun. Sometimes they do serious work, like creating ASCII art.

Along with the sound of the dot-matrix printer and the dial-up modem striving to make a connection to the internet, ASCII art is a quintessen­tial reminder of the 90s. You couldn’t navigate a website before coming across some form of it, and some people had the sheer audacity to incorporat­e it in their email signature! While those days are passé now, ASCII art itself isn’t dead, and you can easily create fun elements to break the monotony of text on a webpage.

Originally intended to refer to visual art made entirely of ASCII code, the term ASCII art is generally used to refer to any form of text based visual art. It can be a simple use of – and + signs to create a box, or more complex arrangemen­t of letters and symbols. Most Linux distributi­ons ship with a range of tools to help you create different text-based visual works. The most popular of these are Boxes and Figlet.

Boxes galore

The simple command-line utility Boxes has been around for a long time, and remains popular for creating ASCII art. As its name suggests, the tool works by wrapping the input text around a box. While you can create your own custom design, Boxes ships with a vast number of designs out of the box.

Most Linux distributi­ons carry Boxes in their software repositori­es and you can easily install it with the sudo apt install boxes or the sudo dnf install boxes command, on deb- or rpm-based distributi­ons. You can run the echo “Type any text here” | boxes -d <design name> command to wrap the specified text into the given box design. If you don’t use the -d command option, the tool will automatica­lly use the default design.

Boxes can also be integrated into various text editors such as Vim, jed, emacs and others. This makes it possible for you to then select text in the editor, and have it wrapped in a box with only a single command, without ever navigating out of the text editor.

With Vim, you can run commands from the normal mode by pressing !!, and then typing a command. For example, create a text file with several lines of text. Now, select any two lines, and press !! and type the command

boxes -d peek . In the following example, we open a file in Vim, with six lines of text and issued this command after selecting the last two lines:

This is a test file

Line 2 of file

The third line of the file

Unsurprisi­ngly, this is the fourth line.

/* _\|/_

(o o) +----oOO-{_}-OOo---------------------------------------------+

|without breaking tradition, the fifth line. |

|contrary to popular expectatio­n, this is the sixth line. | +----------------------------------------------------------------------*/ ~

~

~

~

:’<,‘>!boxes -d peek

As you can see, running the boxes command automatica­lly affects the selected text. If no text is selected, boxes will automatica­lly box the current line in your text file.

What’s your alignment?

Apart from the -d command option, which is used to specify the box design, another useful option is -a , which is used to set an alignment for the text within the box. It accepts various arguments, to align the text. You can run the boxes -a hcvc if you want the text to be horizontal­ly centred and vertically centred. If you wish to justify the text to the left or right, this can be done with the boxes -a jr command. The j option accepts r and l as arguments, respective­ly for right and left. The j

option only affects the individual lines within a box; in contrast the h and v options affects the horizontal and vertical position of the entire block of text within the box.

You can also set a custom size for your boxes with the -s command option. For instance, the command boxes -d dog -s 50 will create a box using the dog design, with a width of 50 characters. You can similarly set the height as well, using the x argument with -s command option: boxes -d parchment -s50x3 :

This is a test file

Line 2 of file

The third line of the file _________________________________________

/\ \

\_| Unsurprisi­ngly, this is the fourth line. |

| |

| ___________________________________ ___|_ \_/_______________________________ ______/ without breaking tradition, the fifth line. contrary to popular expectatio­n, this is the sixth line.

Depending on the box design, and the amount of text, you may also have to define a custom padding. The padding area comprises empty spaces and surrounds your text within the box. This is used to ensure that the text isn’t placed too close to the walls of the box. The padding is defined with the -p

command option, and accepts various arguments such as a, h, v, l, r, t, and b. If you want a padding of four character on all sides of your text within the box, use the boxes -d <design name> -p a4 . Similarly, the command option -p a5t2l2 will use a padding of five characters on all sides, except the top and left, where a space of only two characters will be provided.

All box designs are described in the Boxes configurat­ion file. The global system-wide configurat­ion file is stored in the /etc/boxes/ directory. If you prefer to make changes to this file, copy the /etc/boxes/ boxes-config file into your ~/ directory and rename as .boxes: cp /etc/boxes/boxes-config ~/.boxes

Before you embark on creating your own custom designs for use with Boxes, refer to the configurat­ion file and familiaris­e with the proper syntax for creating a box. Also refer to https://boxes.thomasjens­en.com/docs/ config-general.html for important instructio­ns on defining shapes for your custom box.

Using Figlet

Another popular tool for transformi­ng vanilla text into stylised designs is Figlet. As with Boxes, there’s a good chance that the tool is available in the software repositori­es of your distributi­on.

The tool ships with a number of fonts out of the box. Head over the /usr/share/figlet directory for a list of the default offerings. You can download additional fonts from the project’s FTP site: ftp://ftp.figlet.org/pub/ figlet/fonts/. Note that Figlet cannot work with compressed archives, and looks for font files with the .flf extension. Uncompress the contribute­d.tar.gz file onto your disk, and move its contents into the /usr/share/ figlet/ directory. You’ll now be able to use these downloaded fonts, in addition to the ones that are already on offer.

To stylise any text with Figlet, you must run the figlet

-f <font name> ‘Text to convert’ command:

$ figlet -f digital ‘Basic text’

+ - + - + - + -+ - + + - + - +

|B|a|s|i|c| |t|e|x|t|

+ - + - + - + -+ - + + - + - +

If you like Figlet, you might also spend some time with TOIlet, which is quite similar, but supports coloured output, and also supports exporting the output to various formats such as HTML and SVG.

 ??  ?? You can run the ‘boxes -l | less’ command to fetch a list of available designs for you to choose from.
You can run the ‘boxes -l | less’ command to fetch a list of available designs for you to choose from.
 ??  ??
 ??  ?? The figlet man page presents the various options for aligning the transforme­d text, and even specifying the width of columns.
The figlet man page presents the various options for aligning the transforme­d text, and even specifying the width of columns.

Newspapers in English

Newspapers from Australia