From 736982b20af663fed7884dc39fe050eabdfbd511 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 29 Aug 2008 10:30:29 +0000 Subject: [PATCH] Added choice command to wrap zenity or xmessage. git-svn-id: https://svn.cambridge.iain.cx/profile/trunk@144 6be0d1a5-5cfe-0310-89b6-964be062b18b --- choice | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 choice diff --git a/choice b/choice new file mode 100755 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 ] : [: ...] +# Example: choice "File exists" "Replace:1" "Back up:2" "No action:0" +# + +function usage() { + echo >&2 "Usage: choice [-t ] : [: ...]" + 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 -- 2.7.4