Brighten up your console with cows and ponies!

fun
terminal
notes
howto
Learn how to brighten your console with a fortune told by a cow or a colorful pony.
Author

Kasia Kedzierska

Published

September 20, 2024

Modified

October 8, 2024

Looking to add a splash of fun to your command line? In this post, I’ll show you how to brighten up your console with a fortune told by a cow or a colorful pony! We’ll use a simple Bash function to display a random fortune, colorize it, and present it in a speech bubble delivered by a cow or a pony.

Screenshot of terminal session with a graphical welcome message

Prerequisites

This should work in unix terminal, so far I used it in ✓ Ubuntu Bash, ✓ Mac Bash and ✓ Mac Zsh.

You’ll need the following programs installed:

  • fortune and fortune-mod: For sampling random fortunes.
  • fortunes Provides a collection of fortune cookies.
  • cowsay and ponysay To display messages in speech bubbles authored by cows or ponies.
  • lolcat A fun tool to colorize your terminal output.

To install all prerequisites, run the following command in your terminal:

sudo apt install fortune \
  fortune-mod \
  fortunes \
  cowsay \
  lolcat
sudo snap install ponysay
brew tap daviderestivo/fortune-mod
brew install fortune cowsay lolcat ponysay

tl;dr

If you want to be greated by a fancy cow or pony to brighten up your day, add below code to your .bashrc, .bash_profile, or Zsh equivalent.

fancy_console_greeting() {
  # Displays a fancy greeting message in the console.
  #
  # (1) The function checks for required dependencies and prompts to install
  # them if missing.
  # (2) Then, it composes a welcome message that includes the current date
  # and a fortune snippet.
  # (3) The message is then displayed using either cowsay or ponysay, randomly
  # chosen, with color effects applied by lolcat.

  # Usage:
  #   fancy_console_greeting [--verbose/-v]
  #
  # Options:
  #   --verbose/-v   Enable verbose mode.


  # Constants
  readonly DEPENDENCIES=(cowsay fortune lolcat ponysay)

  # Default settings
  local verbose=false

   # Parse arguments
  for arg in "$@"; do
    case $arg in
      --verbose | -v)
        verbose=true
        shift
        ;;
      *)
        echo "Unknown option: $arg"
        return 1
        ;;
    esac
  done

  # Local variables
  local missing_deps=()
  local dep
  local current_date
  local fortune_msg
  local message
  local cow_file

  # Check for missing dependencies
  for dep in "${DEPENDENCIES[@]}"; do
    if ! command -v "${dep}" &>/dev/null; then
      missing_deps+=("${dep}")
    fi
  done

  # If there are any dependencies to install, print a message and exit
  if [[ ${#missing_deps[@]} -gt 0 ]]; then
    echo "To use fancy_console_greeting, you need to install the following " \
      "dependencies: ${missing_deps[*]}"
    return 1
  fi

  # Compose the message
  current_date=$(date '+%H:%M on %A, %B %d, %Y')
  fortune_msg=$(fortune -s wisdom)
  message="Welcome!\n\n"
  message+="It's ${current_date}.\n\n"
  message+="Today's wisdom:\n"
  message+="${fortune_msg}"

  # Randomly choose between a cow or a pony
  if ((RANDOM % 2 == 0)); then
    # Sample an image from the cowsay set
    cow_file=$(cowsay -l | tail -n +2 | tr ' ' '\n' | shuf | head -n 1)
    echo -e "${message}" | cowsay -f "${cow_file}" -W 45 | lolcat -F 0.01
  else
    if [ "$verbose" = true ]; then
      echo -e "${message}" | ponysay --compact
    else
      echo -e "${message}" | ponysay --compact 2>/dev/null
    fi
  fi
}

fancy_console_greeting

or save a script and run it in your terminal.

There is an instance of unescaped bakslash in the docstring of ponysay. Since recent versions of Python 3.10, this will raise a SyntaxWarning.

/opt/homebrew/bin/ponysay/balloon.py:43: SyntaxWarning: invalid escape sequence '\-'
/opt/homebrew/bin/ponysay/balloon.py:43: SyntaxWarning: invalid escape sequence '\-'

To silence the warning, by default, the script will run ponysay and redirect the stderr to /dev/null. If you want to see the warnings, you can run the script with the --verbose flag.

A pull request to fix the issue has been sumitted, but it has not been merged yet.

Detailed walkthrough

Let’s dive deeper into how each component works.

Fortunes and sayings in the termianl

The fortune command displays a random fortune from a collection of fortune cookies. The fortune-mod package provides a collection of fortune cookies that can be displayed using the fortune command. fortune, created by Ken Arnold, dates back to the 1970s and is a fun way to display a random saying or quote in the terminal.

$ fortune
An investment in knowledge always pays the best interest.
        -- Benjamin Franklin

You can, for example select a subject of the requested fortunes.

$ fortune science
Statistics are no substitute for judgement.
                -- Henry Clay

To see all available subjects of fortunes you can run fortune -f that will list all available files from which fortunes can be sourced. The available files can differ between the sources of packages.

Lolcat - colorize your terminal

Inspired by the “nyan cat” meme, lolcat was created to add rainbow colors to your terminal output. To colorize the output of a command, simply pipe it to lolcat.

$ echo 'Hello, world!' | lolcat

Screenshot of terminal session with a colorful 'Hello, world!' message

You can control the colors of the output with the -F/--freq flag with higher values resulting in more colors and lower values in monochromous output.

With an -a flag you can animate the output (controlling the duration of the animation with -d flag and the speed with -s flag).

Animated output of lolcat

Cowsay - let the cow deliver your message

Created by Tony Monroe in 1999, cowsay is a program that generates ASCII pictures of a cow with a message. The cow can say (cowsay) or think (cowthink) the message, and you can choose from a variety of cow designs.

$ cowsay 'Hello there!'
 ______________
< Hello there! >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

There are sveral ways we can modify how the cow would look like. For example, here’s the excerpt from the cosway manual (accessible via man cowsay):

The -b option initiates Borg mode; -d causes the cow to appear dead; -g invokes greedy mode; -p causes a state of paranoia to come over the cow; -s makes the cow appear thoroughly stoned; -t yields a tired cow; -w is somewhat the opposite of -t, and initiates wired mode; -y brings on the cow’s youthful appearance.

We’re not limited to cows either – there are some figures to choose from.

$ cowsay -f moofasa 'Look, Simba. Everything the light touches is our kingdom.'
 ___________________________________
/ Look, Simba. Everything the light \
\ touches is our kingdom.           /
 -----------------------------------
       \    ____
        \  /    \
          | ^__^ |
          | (oo) |______
          | (__) |      )\/\
           \____/|----w |
                ||     ||

             Moofasa

To list all available cows, run the following command:

$ cowsay -l 
Cow files in /path/to/cows:
beavis.zen blowfish bong bud-frogs bunny cheese cower daemon default dragon
dragon-and-cow elephant elephant-in-snake eyes flaming-sheep ghostbusters
head-in hellokitty kiss kitty koala kosh luke-koala meow milk moofasa moose
mutilated ren satanic sheep skeleton small stegosaurus stimpy supermilker
surgery three-eyes turkey turtle tux udder vader vader-koala www

And to select a specific cow, use the -f flag followed by the cow’s name:

$ cowsay -f tux 'Hello there!'
 ______________
< Hello there! >
 --------------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

And to randomly select a cow, you can use the following:

cowsay -f $(cowsay -l | tail -n +2 | tr ' ' '\n' | shuf -n 1) 'Hello'
 _______
< Hello >
 -------
  \            .    .     .
   \      .  . .     `  ,
    \    .; .  : .' :  :  : .
     \   i..`: i` i.i.,i  i .
      \   `,--.|i |i|ii|ii|i:
           UooU\.'@@@@@@`.||'
           \__/(@@@@@@@@@@)'
                (@@@@@@@@)
                `YY~~~~YY'
                 ||    ||

Ponysay - a modern twist on cowsay

Ponysay is a fun tool that is similar to cowsay, but instead of a cow, it uses a pony from the My Little Pony series.

To have a pony say a message, you can simply use the ponysay command:

$ fortune | ponysay

There is an instance of unescaped bakslash in the docstring of ponysay. Since recent versions of Python 3.10, this will raise a SyntaxWarning.

/opt/homebrew/bin/ponysay/balloon.py:43: SyntaxWarning: invalid escape sequence '\-'
/opt/homebrew/bin/ponysay/balloon.py:43: SyntaxWarning: invalid escape sequence '\-'

To silence the warning, you can redirect the stderr to /dev/null. If you want to see the warnings, you can run the script with the --verbose flag.

A pull request to fix the issue has been sumitted, but it has not been merged yet.

Screenshot of terminal session with a graphical welcome message

You can also select a specific pony to say the message:

$ fortune -s wisdom | ponysay -F owl 2>/dev/null

Screenshot of terminal session in which owl says 'Ninety percent of everything is crap.' -- Theodore Sturgeon

To list all available ponies you can specify the -A flag to list all ponies, or -l to list only the MLP ponies.

To list all options of ponysay, you can run ponysay --help.

And if you’re dedicated enough, you can even create your own pony! Check out the Providing ponies section of the ponysay documentation for more information.

Putting it all together

To comopose the message, I will take the current date and a random fortune message.

current_date=$(date '+%H:%M on %A, %B %d, %Y')
fortune_msg=$(fortune -s wisdom)

message="Welcome! \n\nIt's ${current_date}. \n\nToday's wisdom: \n${fortune_msg}"

The message will look like this:

$ echo -e "${message}"
Welcome! 

It's 09:28 on Tuesday, September 24, 2024. 

Today's wisdom: 
You climb to reach the summit, but once there, discover that all roads
lead down.
                -- Stanislaw Lem, "The Cyberiad"

Then, I will display the message using either cowsay or ponysay, randomly chosen, with color effects applied by lolcat.

# Randomly choose between a cow or a pony
if ((RANDOM % 2 == 0)); then
  # Sample an image from the cowsay set
  cow_file=$(cowsay -l | tail -n +2 | tr ' ' '\n' | shuf | head -n 1)
  echo -e "${message}" | cowsay -f "${cow_file}" -W 45 | lolcat -F 0.01
else
  if [ "$verbose" = true ]; then
    echo -e "${message}" | ponysay --compact
  else
    echo -e "${message}" | ponysay --compact 2>/dev/null
  fi
fi

Conclusion

By combining these fun command-line tools, you can make your terminal sessions more enjoyable. Whether you’re a fan of cows, ponies, or just colorful text, these commands offer a delightful way to personalize your console.

Happy hacking!