DialogVsWhiptail

nicolaw 8th April 2018 at 11:38am
Bash CodeSnippets curses dialog TUI whiptail

whiptail is used by the apt package manager on Debian-based systems, as well as being being the default diahlog program on many RedHat derived systems. This probably makes whiptail the slightly more portable / reliable of the two to use if you must choose between them.

However, from the whiptail README:

whiptail is designed to be drop-in compatible with dialog(1), but has less features: some dialog boxes are not implemented, such as tailbox, timebox, calendarbox, etc.

Because of this, why not just use whichever program happens to be installed?

#!/usr/bin/env bash

set -euo pipefail

is_in_path () {
  type -P "$1" >/dev/null 2>&1
}

dia_menu () {
  declare dia="exit" dia_esc=""
  while : ; do
    is_in_path dialog && DIA=dialog && DIA_ESC=-- && break
    is_in_path whiptail && DIA=whiptail && break
    >&2 echo "Missing dialog command; exiting!" && exit 2
  done

  declare -A arg="$1"; shift
  $dia \
    --backtitle "${arg[backtitle]}" \
    --title "${arg[title]}" \
    --menu "${arg[question]}" \
    0 0 0 $dia_esc "$@"
}

main () {
  dia_menu '([backtitle]="Backtitle"
            [title]="Title"
            [question]="Please choose:")' \
            "Option A"  "Description A"   \
            "Option B"  "Description B"   \
            "Option C"  "Description C"
}

main "$@"