Merge branch 'refs/heads/t/help-a' into pu
[gsu.git] / gui
diff --git a/gui b/gui
index 2d3e270..4297564 100644 (file)
--- a/gui
+++ b/gui
@@ -1,8 +1,10 @@
 #!/bin/bash
+# Copyright (C) 2006 Andre Noll
+# Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
 
 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
        gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
-       . $gsu_dir/common || exit 1
+       . "$gsu_dir/common" || exit 1
        _gsu_setup
 fi
 
@@ -18,10 +20,23 @@ _get_geometry()
        fi
        x="${result#* }"
        y="${result%% *}"
-       (($x > 190)) && x=190
+       ((x > 190)) && x=190
        result="$y $x"
 }
 
+_set_dialog_ret()
+{
+       local ec="$1"
+
+       case "$ec" in
+       0) ret=$GSU_SUCCESS;;
+       1) ret=1;; # cancelled
+       *)
+               result="dialog exit code $ec"
+               ret=-$E_GSU_DIALOG
+       esac
+}
+
 # Open a dialog box which asks the user to input a text
 #
 # Usage: gsu_input_box <text> <init>
@@ -37,12 +52,7 @@ gsu_inputbox()
        _get_geometry
        g="$result"
        result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
-       if (($? != 0)); then
-               ret=-$E_GSU_DIALOG
-               result='inputbox'
-               return
-       fi
-       ret=$GSU_SUCCESS
+       _set_dialog_ret $?
 }
 
 # Show the given file in a text box
@@ -59,8 +69,8 @@ gsu_textbox()
 
        ret=-$E_GSU_DIALOG
        result='textbox'
-       dialog --textbox "$file" $g || return
-       ret=$GSU_SUCCESS
+       dialog --textbox "$file" $g
+       _set_dialog_ret $?
 }
 
 # Show a message in a text box
@@ -70,15 +80,14 @@ gsu_textbox()
 # This is like gsu_textbox() but the text is passed as a string.
 gsu_msgbox()
 {
+       local tmp
+
        # Some versions of dialog segfault if the text is too long. Hence we
        # always use a temporary file.
-       local tmp="$(mktemp gsu_msgbox.XXXXXXXXXX)"
-
-       if (($? != 0)); then
-               ret=-$E_GSU_MKTEMP
-               result='temp file for textbox'
-               return
-       fi
+       gsu_make_tempfile 'gsu_msgbox.XXXXXXXXXX'
+       ((ret < 0)) && return
+       tmp="$result"
+       trap "rm -f $tmp" EXIT
        echo "$1" > "$tmp"
        gsu_textbox "$tmp"
        rm -f "$tmp" # ignore errors
@@ -88,7 +97,7 @@ _gsu_menu()
 {
        local header="${1:-root}"
        local items="$2"
-       local i state opts num=0
+       local i opts num=0
 
        _get_geometry
        opts="$result 16"
@@ -97,13 +106,7 @@ _gsu_menu()
                opts+=" $i $num"
        done
        result="$(dialog --menu "$gsu_banner_txt ($header)" $opts 3>&1 1>&2 2>&3 3>&-)"
-       case $? in
-       0) ret=$GSU_SUCCESS;;
-       1) ret=1;; # cancelled
-       *)
-               result="menu error $ret"
-               ret=-$E_GSU_DIALOG
-       esac
+       _set_dialog_ret $?
 }
 
 _get_level()
@@ -129,7 +132,7 @@ _get_subtree()
        #echo "line: $line_num, root: $root, indent level: $level"
        result="$(sed -e "1,${line_num}d;" <<< "$tree" \
                | sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
-               | sed -e "/^$TAB\{$(($level + 2))\}/d")"
+               | sed -e "/^$TAB\{$((level + 2))\}/d")"
        if (($? != 0)); then
                ret=-$E_GSU_MENU_TREE
                result="sed command for subtree $root failed"
@@ -158,15 +161,15 @@ _browse()
 
        while :; do
                _gsu_menu "$header" "$subtree"
-               (($ret < 0)) && return
+               ((ret < 0)) && return
                [[ -z "$result" ]] && return # menu was cancelled
                if [[ "${result%/}" != "$result" ]]; then
                        old_header="$header"
                        header="$result"
                        _get_subtree "$tree" "$header"
-                       (($ret < 0)) && return
+                       ((ret < 0)) && return
                        _browse "$header" "$tree" "$result"
-                       (($ret < 0)) && return
+                       ((ret < 0)) && return
                        header="$old_header"
                        continue
                fi