Gitify the working tree.
[profile.git] / opt / bin / choice
diff --git a/opt/bin/choice b/opt/bin/choice
new file mode 100755 (executable)
index 0000000..8f80781
--- /dev/null
@@ -0,0 +1,42 @@
+#!/bin/bash
+#
+# choice: Display an X dialogue to allow a choice.
+# Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]
+# Example: choice "File exists" "Replace:1" "Back up:2" "No action:0"
+#
+
+function usage() {
+  echo >&2 "Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]"
+  exit 100
+}
+
+while getopts ":t:" arg; do
+  case $arg in
+    t) timeout="$OPTARG";;
+    *) usage;;
+  esac
+done
+shift $((OPTIND-1))
+
+description="$1"; shift
+[ -z "$description" ] && usage
+for i in ${1+"$@"}; do
+  [ "${i/:/}" = "$i" ] && usage
+done
+
+args=
+if which zenity 2>/dev/null | grep ^/ &>/dev/null; then
+  [ ! -z "$timeout" ] && timeout="--timeout=$timeout"
+  for i in ${1+"$@"}; do
+    args="$args \"${i/:/\" }"
+  done
+  ret=$(eval zenity --list --text="\"$description\"" --column=Choice --column=hidden --hide-column=2 --print-column=2 $args $timeout)
+  exit $ret
+else
+  [ ! -z "$timeout" ] && timeout="-timeout $timeout"
+  for i in ${1+"$@"}; do
+    args="$args,\"$i\""
+  done
+  args=${args/,/}
+  eval exec xmessage -center $timeout -buttons $args "$description"
+fi