My-Tiny.Net :: Networking with Virtual Machines



Fun with ASCII Art



Back in the 1980s the "Berkeley Standard Distribution" (BSD) Unix included a package of games - there were not really any GUIs at the time, so they were all terminal (text) based. By the early 1990s people came up with the idea of creating ASCII Art text, using combinations of normal characters and whitespace to make pictures.

We have a few examples we can play with to sharpen our understanding of shell command substitution and redirection: caeser and fortune from the BSD games, figlet and boxes from the ASCII art community. The GCC and Make page on the menu has instructions you can use to compile two more ASCII art applications, the Steam Locomotive (sl, just for fun) and TOIlet which is like a supercharged figlet.

So, check the man pages and Standard I/O::Redirection on the menu, relax, get your imagination working, and play around with these examples to get started.

One thing to note: in bash, when command lines are quite long we can use \ (backslash) to break them up into two lines. The \ means in this case "to be continued" or "more to come". A command ending with \ in your terminal will reply with PS2, which means it is waiting for the rest of your command.

IMPORTANT: If there is a space after the \ the command WILL NOT work.


Figlet and Boxes

Boxes is a text filter which can draw any kind of box around its input text. Box design choices range from simple boxes to complex ASCII art. It was designed to be used with text editors that support filters to create comments in programming code, but we can use it on the command line by piping (|) text output through it.

Run boxes -l | less to list all designs with their description. Any user may define box designs by copying the system-wide config file to $HOME/.boxes, and editing it. The syntax of the config file is explained on the boxes home page, along with several examples: http://boxes.thomasjensen.com/docs/

As usual, man boxes has all the details (type q to quit), but here is a quickstart list of switches:
  • -d selects a design from your config file.

  • -a can be used to position the input text inside a box. The factory default setting is -a hlvtjl which stands for "horizontally centered, vertically top, justification left". Since no one really wants to all type that just to center text inside a box, there are shorthand notations: -a l is short for -a hlvcjl, -a c is short for -a hcvcjc, and -a r is short for -a hrvcjr.

  • -p sets text padding: -p h0v0 sets padding to "horizontal zero, vertical zero". -p a1l2 stands for "all 1, left 2", and tells boxes to put one space (or empty line, in the vertical direction) around the input text block, except for the left side where two spaces shall be used.

  • -s sets the box size. Using -s 40 sets the box width to 40 characters. Using -s 40x11 sets both width and height. Set the height only with -s x11 (note the leading x on the argument).

FIGlet makes large character ASCII banners out of ordinary text. For the full story see http://www.figlet.org/figlet_history.html, and for a nice demo see http://www.kammerl.de/ascii/AsciiSignature.php. As usual, try figlet --help for a quick list of switches, and man figlet to see all the details.

So let's try it out (-f chooses a font)

showfigfonts |less

figlet $(date +%A)

figlet -f slant $(date +%x) | boxes -d peek

echo -e "\e[01;32m"; figlet $(date +%R) |boxes; echo -e "\e[00m"
NOTE: The -e option of the echo command enables parsing of the escape sequences. Check Quick Reference :: Escape Codes on the menu for a list of possibilities.

fortune

You can have a random fortune or quote printed each time a user logs in, by simply setting the execute bits on /etc/profile.d/login-fortune.sh
Use man fortune to view the options (type q to exit the man page).

It's super easy to create your own fortune data file: see /usr/share/games/fortunes/fortunes for an example. You can create a new file in this directory, and type in your quotes with a % on a line by itself to separate each quote. The last step is to run strfile filename to create a .dat file for your fortunes, which contains a header structure and a table of file offsets for each group of lines. This allows random access to the strings.

One thing to note though: the name of the original text file is embedded in the .dat file, and it cannot follow symlinks. So - No Renaming!

fortune, boxes, and wordwrap

When you try these examples using fortune |boxes; you will find that the wordwrap is pretty messy, because the screen is always 80 characters wide and long lines are automatically wrapped (we see this a lot with long command lines). The way to correct this with fortune is to pipe the output through to the fold command, like this:

fortune |fold -s -w 70 |boxes -d columns;

where -s means to wrap on spaces (proper wordwrap) and -w says to have only 70 characters on a line rather than the default of 80, which is good for a complex design like columns.

Do a little experimentation at the command line with the -w and -d switches to get the design and the size right!

caeser

rot13 is a simple encryption method, said to date back to "nn" BC when it was used by the Roman emperor Julius Caeser to keep communications with his army in the field confidential. It works by shifting each character forward 13 times, so that A becomes N, B becomes O, etc. The number 13 is chosen because scrambling and descrambling uses the exact same algorithm. Only A-Z and a-z is scrambled, other characters and symbols are left untouched. caesar is a utility from BSD games for doing this, using any number for the shift:

echo "You are in a twisty maze of passages, all alike." |caesar 13

echo "Lbh ner va n gjvfgl znmr bs cnffntrf, nyy nyvxr." |caesar 13

All Together at Login Time

It can be fun to combine these in your login scripts. At login time, user environment variables are initialized from various global system files like /etc/profile or /etc/bashrc and user files ~/.bash_profile, ~/.bash_login, ~/.profile, ~/.bashrc (exactly which one depends on your system). Use man bash to see details - you can type /search term to find words in a man page, and q to exit.

On our system, /etc/profile will call all of the executable scripts in /etc/profile.d that end with .sh

So all we need to do is set the execute bits on /etc/profile.d/login-fortune.sh and then edit it to take away the nonexistent fortunz file (the default file is /usr/games/fortunes which we have, so we can leave just fortune there), and finally pick a nice color (33m is just an example) like this:

      echo -e "\e[01;33m"; fortune |fold -s -w 70 |boxes; echo -e "\e[00m" 


or perhaps
      # save the fortune to a variable
      FF=$(fortune)

      # show cyphertext
      echo -e "\e[01;31m"; echo $FF |caeser 13 |boxes; echo -e "\e[00m"

      read -p "Press any key to decrypt\n"

      # show plaintext
      echo -e "\e[01;32m"; echo $FF |boxes; echo -e "\e[00m"