Handle locale a bit (but not much) more sensibly.
[profile.git] / choice
1 #!/bin/bash
2 #
3 # $Id$
4 #
5 # choice: Display an X dialogue to allow a choice.
6 # Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]
7 # Example: choice "File exists" "Replace:1" "Back up:2" "No action:0"
8 #
9
10 function usage() {
11   echo >&2 "Usage: choice [-t <timeout>] <description> <text>:<ret> [<text>:<ret> ...]"
12   exit 100
13 }
14
15 while getopts ":t:" arg; do
16   case $arg in
17     t) timeout="$OPTARG";;
18     *) usage;;
19   esac
20 done
21 shift $((OPTIND-1))
22
23 description="$1"; shift
24 [ -z "$description" ] && usage
25 for i in ${1+"$@"}; do
26   [ "${i/:/}" = "$i" ] && usage
27 done
28
29 args=
30 if which zenity 2>/dev/null | grep ^/ &>/dev/null; then
31   [ ! -z "$timeout" ] && timeout="--timeout=$timeout"
32   for i in ${1+"$@"}; do
33     args="$args \"${i/:/\" }"
34   done
35   ret=$(eval zenity --list --text="\"$description\"" --column=Choice --column=hidden --hide-column=2 --print-column=2 $args $timeout)
36   exit $ret
37 else
38   [ ! -z "$timeout" ] && timeout="-timeout $timeout"
39   for i in ${1+"$@"}; do
40     args="$args,\"$i\""
41   done
42   args=${args/,/}
43   eval exec xmessage -center $timeout -buttons $args "$description"
44 fi