2 # Copyright (C) 2006 Andre Noll
3 # Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
5 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
6 gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
7 . "$gsu_dir/common" || exit 1
11 _gsu_node_name_pattern='[a-zA-Z_]'
18 gsu_msg "fatal: could not get terminal geometry"
33 1|255) ret=1;; # cancelled
35 result="dialog exit code $ec"
40 # Open a dialog box which asks the user to input a text
42 # Usage: gsu_input_box <text> <init>
44 # <text> is displayed above of the input field, which is is preset to <init>.
45 # The entered text is returned in $result. On success (user pressed OK)
46 # the function returns zero. If the user selected Cancel, the return value is
47 # one. On dialog errors, a negative error code is returned.
50 local g text="$1" init="$2"
54 result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
58 # Show the given file in a text box
60 # Usage: gsu_textbox <path>
62 # The box has an OK button which closes the box when activated.
72 dialog --textbox "$file" $g
76 # Show a message in a text box
78 # Usage: gsu_msgbox <text>
80 # This is like gsu_textbox() but the text is passed as a string.
85 # Some versions of dialog segfault if the text is too long. Hence we
86 # always use a temporary file.
87 gsu_make_tempfile 'gsu_msgbox.XXXXXXXXXX'
90 trap "rm -f $tmp" EXIT
93 rm -f "$tmp" # ignore errors
98 local header=${1:-root} dflt_item=$2 items=$3
103 result="$(dialog --no-lines --no-items \
104 --default-item "$dflt_item" \
105 --menu "$gsu_banner_txt ($header)" \
106 $geom 16 $items 3>&1 1>&2 2>&3 3>&-)"
112 local tmp="${1%%$_gsu_node_name_pattern*}"
118 local tree="$1" root="${2%/}"
121 ret=-$E_GSU_MENU_TREE
122 result="subtree grep failed"
123 first="$(grep -n "$TAB\{1,\}$root/" <<< "$tree")" || return
124 [[ -z "$first" ]] && return
126 line_num="${first%%:*}"
127 _get_level "${first#*:}"
130 #echo "line: $line_num, root: $root, indent level: $level"
131 result="$(sed -e "1,${line_num}d;" <<< "$tree" \
132 | sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
133 | sed -e "/^$TAB\{$((level + 2))\}/d")"
135 ret=-$E_GSU_MENU_TREE
136 result="sed command for subtree $root failed"
144 local tree="$1" TAB=' '
146 result="$(grep "^${TAB}${_gsu_node_name_pattern}" <<< "$tree")"
148 ret=-$E_GSU_MENU_TREE
149 result="root node grep failed"
157 local header=$1 tree=$2
158 local -a subtree=($3)
159 local old_header dflt_item=${subtree[0]}
162 _gsu_menu "$header" "$dflt_item" "${subtree[*]}"
163 ((ret < 0)) && return
165 [[ -z "$result" ]] && return # menu was cancelled
166 if [[ "${result%/}" != "$result" ]]; then
169 _get_subtree "$tree" "$header"
170 ((ret < 0)) && return
171 _browse "$header" "$tree" "$result"
172 ((ret < 0)) && return
176 eval ${gsu_name}_$result
182 local tree="$1" subtree
184 type -t dialog &> /dev/null
186 gsu_msg "dialog executable not found"
189 _get_root_nodes "$tree"
191 _browse "main menu" "$tree" "$subtree"