Added choice command to wrap zenity or xmessage.
authorIain Patterson <me@iain.cx>
Fri, 29 Aug 2008 10:30:29 +0000 (10:30 +0000)
committerIain Patterson <me@iain.cx>
Fri, 29 Aug 2008 10:30:29 +0000 (10:30 +0000)
git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@144 6be0d1a5-5cfe-0310-89b6-964be062b18b

choice [new file with mode: 0755]

diff --git a/choice b/choice
new file mode 100755 (executable)
index 0000000..882e39e
--- /dev/null
+++ b/choice
@@ -0,0 +1,44 @@
+#!/bin/bash
+#
+# $Id$
+#
+# 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