0

I have a little banner that I display at login

fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); figlet -w $(tput cols) -f univers $(date +"%H:%M"); }

This works fine if the additional fonts are installed to get univers, but that's not part of the default installation, so I can do the following:

fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); if [ -f /usr/share/figlet/univers.flf ]; then figlet -w $(tput cols) -f univers $(date +"%H:%M"); else figlet -w $(tput cols) $(date +"%H:%M"); fi; }

This also works fine, but I was curious if the if/else/fi could be removed and the logic could be done inside the command itself, i.e. something like:

fignow() { figlet -w $(tput cols) $(date +"%b %d, Week %V"); figlet -w $(tput cols) -f $(use-univers-or-failback-to-standard.flf-if-univers.flf-is-not-available) $(date +"%H:%M"); }

Is something like that possible?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
YorSubs
  • 3,194
  • 7
  • 37
  • 60

3 Answers3

2

I suggest:

fignow() {
  local opts
  figlet -w $(tput cols) $(date +"%b %d, Week %V")
  [ -f /usr/share/figlet/univers.flf ] && opts="-f univers"
  figlet -w $(tput cols) $opts $(date +"%H:%M")
}

Only if the file exists the variable $opts contains the necessary options.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    This is a good approach, but will fail if opts is set outside the function. You need to do: `fignow() { local opts=""; ....}` – William Pursell Nov 21 '20 at 11:01
  • @WilliamPursell: Thanks for the hint, I have included it in my answer. I suggest to leave your comment here. – Cyrus Nov 21 '20 at 11:06
2

Checking for the file or parsing the output of figlist or showfigfonts is inherently fragile. Just call figlet to see if the option is available. Something like:

fignow() {
        local font=${1-universe}
        figlet -f "$font" a > /dev/null 2>&1 || font=
        figlet -w $(tput cols) $(date +"%b %d, Week %V")
        figlet -w $(tput cols) ${font:+-f "$font"} $(date +"%H:%M")
}

Note that this is one of those cases where you must not use double quotes around the variable, since you want ${font:+-f "$font"} to expand to the empty string when $font is empty. If you use "${font:+-f "$font"}", the semantics change and an empty string is passed to figlet.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

I suggest to use figlist to avoid hardcoding the themes path:

figlet -w "$(tput cols)" -f "$((figlist | /bin/grep -hw universe) || echo "standard")" "foo"
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I never knew `figlist` existed, that's nice to know. Relating to that, `figlist` shows the `.flc` files, like `moscow.flc` as if they are usable, but these are unreadable by `figlet`. i.e. `figlet -f moscow HelloWorld` says "unable to open font file". Do you know why `figlist` lists them as if they are usable? – YorSubs Nov 21 '20 at 10:47
  • 2
    @YorSubs: `moscow.flc` is no fontfile (.flf), it is a controlfile. See `man figlet` and its option `-C`. – Cyrus Nov 21 '20 at 10:56
  • 1
    @YorSubs Yeah, unfortunately there is no option to filter either the one other the other from figlist's output. But since you hardcode the name `universe` it shouldn't be an issue, right? I mean, figlist will at least tell you reliably if it's installed or not – hek2mgl Nov 21 '20 at 11:23
  • 1
    This might help: `find $(figlet -I2) -name '*.flf'` – Cyrus Nov 21 '20 at 11:34
  • Cool. Pulling together various things in this thread, I got this down to `figlet -w $(tput cols) -f $([ -f $(figlet -I2)/univers.flf ] && echo "univers" || echo "standard") foo`. Any problems with this; is my logic correct that `&&` and `||` used like this works properly? Avoids relying on the `figlist` script and gets the font folder from `figlet -I2`. – YorSubs Nov 21 '20 at 14:35