Skip to content

Instantly share code, notes, and snippets.

@henrikh
Created October 11, 2010 17:32
Show Gist options
  • Save henrikh/620901 to your computer and use it in GitHub Desktop.
Save henrikh/620901 to your computer and use it in GitHub Desktop.
# !/bin/bash
# # figlet-dialog - A "graphical" figlet frontend.
# ## Intro
# So I wanted to learn some shell/bash scripting. I found this
# program called `dialog`, which would allow me to make "GUI"s in
# shell, so I decided to make something with it.
# The following program is a `figlet` GUI. It will display a textbox
# for your message, and a list of fonts to select.
# Eventually this also turned into a litterate programming exercise.
# ## Program
# ### Initial code
# First I list the fonts you can choose from.
# This is just an array with the names of the fonts. To add a font,
# just add it to the list.
fonts=( standard banner script term )
# #### The font-tag list
# Since `display` needs a "tag" for each element in the list (and
# it needs the list in a "tag1 name1 tag2 name2"-string) I have to
# iterate over the list and format it.
# I'm 100% certain this could be done in awk. Guess there are things
# to do in v2
# n starts at one, because it looks nicer.
n=1
font_tag_list=""
for index in ${fonts[@]}
# The "${fonts[@]}" says returns all elements in the font, so it
# can be iterated over.
do
# Here we make the string with the tags and names. Notice how
# bash is untyped, i.e. there are no types and things are combined
# as needed (Weird like Javascript).
font_tag_list=$font_tag_list" "$n" "$index
let "n += 1" # Increase n by one (1).
done
# ### "GUI"
# With the list of fonts ready, the GUI can be set up. It isn't truly
# a GUI, since it's all text based. Using something like `gdialog`
# og `xdialog` would allow me to make GUIs.
# Anyway, first I ask for the message that is going to be printed.
message=`dialog --inputbox "What message to print?" 6 40 --stdout`
# The `--stdout` is needed because the output is going to be saved
# in a variable. If another program was piped to it, the flag would
# be needed too.
# Next up is the font. `$font_tag_list` expands to the
# space-seperated tag-name pairs.
font=`dialog --menu "Choose a font." 11 40 $n\
$font_tag_list\
--stdout`
# The `$n` is needed because `dialog` needs to know how many pairs
# there are.
# The terminal is cleared so the GUI doesn't mess up the terminal.
clear
# ### Printing
# Printing is done using `figlet` which has many, many fonts. The
# fonts are ASCII-art fonts, but they are still fairly complex.
# Before the message can be printed, there is one minor tweak.
# The list of fonts was zero-indexed, and the list in the GUI
# was one-indexed, I need to substract 1 to get the font from the
# original list.
let "font -= 1"
# Finally I can print the font with `figlet`
figlet $message -f ${fonts[$font]}
# That's it!
# ##References
# Advanced Bash-Scripting Guide:
# http://tldp.org/LDP/abs/html/index.html
# dialog man page: http://linux.die.net/man/1/dialog
# Dialog: An Introductory Tutorial:
# http://www.linuxjournal.com/article/2807
# FIGlet fonts library: http://www.jave.de/figlet/fonts/overview.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment