1

Basically I want a way for figlet to choose a random font. It doesn't seem to have an inbuilt method, or at least I haven't found one in the manual or in the web.

I want the message to pop up every time I open up a new xterm, which is easy enough adding figlet -f <a font> Hello World to bash.rc

But I also want the font to be random. I'm wondering if there is a way for <a font> to be selected randomly from the autocomplete suggestions.

1 Answer 1

2

Bash doesn't appear to have autocompletion for figlet. And even if it did, I don't think there's a generic way to generate completions for a specific context, you have to know how the completion code works for this particular command.

You can, however, list the functions without reference to the completion code. Mimicking the zsh completion code, the fonts are *.flf files in the directory shown by figlet -I2. The font names are the file base names, but you can actually pass a full path to a file.

figlet_fonts=("$(figlet -I2)"/*.flf)

So what remains to be done is to take a random element of this array. The special variable RANDOM yields a random number each time it's read. Divide by the size of the array and take the remainder to get a random array index. (There's a slight bias towards the beginning of the array, which doesn't matter here.)

figlet -f "${figlet_fonts[$RANDOM % ${#figlet_fonts[@]}]}" Hello world
3
  • Thank you! it works perfectly! Exactly the outcome I wanted.
    – bob_banana
    Commented Oct 16, 2021 at 15:29
  • Found another method using shuf and kind of combined it with yours figlet -f $(ls "$(figlet -I2)"/*.flf | shuf -n1) Hello World !
    – bob_banana
    Commented Nov 15, 2021 at 15:33
  • And of course you can go really crazy with figlet -f $(ls $(figlet -I2)/*.flf | shuf -n1) Hello World ! | cowsay -n -f $(ls /usr/share/cowsay/cows/ | shuf -n1) | lolcat
    – bob_banana
    Commented Nov 15, 2021 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .