|
| 1 | +## |
| 2 | +## |
| 3 | +# BIW-TOOLS - Bash Inline Widget Tools |
| 4 | +# Copyright 2017 by Chad Juliano |
| 5 | +# |
| 6 | +# Licensed under GNU Lesser General Public License v3.0 only. Some rights |
| 7 | +# reserved. See LICENSE. |
| 8 | +# |
| 9 | +# File: biw-controller.sh |
| 10 | +# Description: Controller functions for panels |
| 11 | +## |
| 12 | + |
| 13 | +# returned by actions to indicate if the menu contents changed. |
| 14 | +declare -ri CTL_ACT_IGNORED=1 |
| 15 | +declare -ri CTL_ACT_CHANGED=0 |
| 16 | + |
| 17 | +# max values to load for history and file lists |
| 18 | +declare -ri CTL_LIST_MAX=50 |
| 19 | + |
| 20 | +# max number of files to populate in file browser |
| 21 | +declare -ir CTL_BROWSE_MAX_FILES=50 |
| 22 | + |
| 23 | +# used so we can generate a relative path |
| 24 | +declare -r CTL_ORIG_PWD=$PWD |
| 25 | + |
| 26 | +function fn_ctl_theme_set_default() |
| 27 | +{ |
| 28 | + fn_theme_set_idx_active $theme_saved_idx |
| 29 | + fn_hmenu_redraw |
| 30 | +} |
| 31 | + |
| 32 | +function fn_ctl_process_key() |
| 33 | +{ |
| 34 | + local _key_ref=$1 |
| 35 | + local _timeout=${2:-''} |
| 36 | + |
| 37 | + # don't print debug if we are animating something |
| 38 | + if [ -z "$_timeout" ] |
| 39 | + then |
| 40 | + fn_biw_debug_print |
| 41 | + fi |
| 42 | + |
| 43 | + if ! fn_csi_read_key $_key_ref $_timeout |
| 44 | + then |
| 45 | + # got timeout |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + |
| 49 | + fn_biw_debug_msg "_key=<%s>" "${!_key_ref}" |
| 50 | + |
| 51 | + fn_hmenu_actions "${!_key_ref}" |
| 52 | + if [ $? == $CTL_ACT_CHANGED ] |
| 53 | + then |
| 54 | + # hmenu was changed so panel is being switched |
| 55 | + # return 1 so the controller will exit |
| 56 | + return 1 |
| 57 | + fi |
| 58 | + |
| 59 | + # return 0 so the loop will continue |
| 60 | + return 0 |
| 61 | +} |
| 62 | + |
| 63 | +function fn_ctl_credits_controller() |
| 64 | +{ |
| 65 | + # change to matrix theme |
| 66 | + fn_theme_idx_from_name THEME_TYPE_MATRIX |
| 67 | + local -i _theme_idx=$? |
| 68 | + fn_theme_set_idx_active $_theme_idx |
| 69 | + |
| 70 | + fn_hmenu_redraw |
| 71 | + |
| 72 | + # show animation |
| 73 | + fn_cred_show |
| 74 | + if [ $? == 0 ] |
| 75 | + then |
| 76 | + # if redraw terminated normally then wait for user input |
| 77 | + local _key |
| 78 | + while fn_ctl_process_key _key |
| 79 | + do |
| 80 | + # ignore all input not from hmenu |
| 81 | + echo -n |
| 82 | + done |
| 83 | + fi |
| 84 | + |
| 85 | + fn_ctl_theme_set_default |
| 86 | + |
| 87 | + return 0 |
| 88 | +} |
| 89 | + |
| 90 | +function fn_ctl_list_controller() |
| 91 | +{ |
| 92 | + local _panel_command=$1 |
| 93 | + local -a _values |
| 94 | + |
| 95 | + fn_ctl_theme_set_default |
| 96 | + |
| 97 | + # read command into _values |
| 98 | + mapfile -t -n $CTL_LIST_MAX _values < <($_panel_command) |
| 99 | + |
| 100 | + # remove first 2 leading blanks for history case |
| 101 | + _values=("${_values[@]#[[:blank:]][[:blank:]]}") |
| 102 | + |
| 103 | + fn_vmenu_init _values[@] |
| 104 | + fn_vmenu_redraw |
| 105 | + |
| 106 | + local _key |
| 107 | + while fn_ctl_process_key _key |
| 108 | + do |
| 109 | + fn_vmenu_actions "$_key" |
| 110 | + if [ $? == $CTL_ACT_CHANGED ] |
| 111 | + then |
| 112 | + continue |
| 113 | + fi |
| 114 | + |
| 115 | + if [ "$_key" == $CSI_KEY_ENTER ] |
| 116 | + then |
| 117 | + # we got the enter key so close the menu |
| 118 | + fn_vmenu_get_current_val "biw_selection_result" |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + done |
| 122 | + |
| 123 | + return 0 |
| 124 | +} |
| 125 | + |
| 126 | +function fn_ctl_theme_controller() |
| 127 | +{ |
| 128 | + # load theme data into menu |
| 129 | + fn_vmenu_init "theme_name_list[@]" $theme_active_idx |
| 130 | + vmenu_ind_values=( [theme_active_idx]=$BIW_CHAR_BULLET ) |
| 131 | + fn_vmenu_redraw |
| 132 | + |
| 133 | + local _key |
| 134 | + while fn_ctl_process_key _key |
| 135 | + do |
| 136 | + fn_vmenu_actions "$_key" |
| 137 | + if [ $? == $CTL_ACT_CHANGED ] |
| 138 | + then |
| 139 | + # use the vmenu index to determine the selected theme |
| 140 | + fn_theme_set_idx_active $vmenu_idx_selected |
| 141 | + fn_hmenu_redraw |
| 142 | + fn_vmenu_redraw |
| 143 | + fi |
| 144 | + |
| 145 | + case "$_key" in |
| 146 | + $CSI_KEY_ENTER) |
| 147 | + # Save selected Theme |
| 148 | + fn_theme_save |
| 149 | + |
| 150 | + # update checkbox |
| 151 | + vmenu_ind_values=( [theme_active_idx]=$BIW_CHAR_BULLET ) |
| 152 | + fn_vmenu_redraw |
| 153 | + ;; |
| 154 | + esac |
| 155 | + done |
| 156 | + |
| 157 | + return 0 |
| 158 | +} |
| 159 | + |
| 160 | +function fn_ctl_browse_controller() |
| 161 | +{ |
| 162 | + fn_ctl_theme_set_default |
| 163 | + |
| 164 | + fn_ctl_browse_update |
| 165 | + |
| 166 | + local _key |
| 167 | + while fn_ctl_process_key _key |
| 168 | + do |
| 169 | + fn_vmenu_actions "$_key" |
| 170 | + if [ $? == $CTL_ACT_CHANGED ] |
| 171 | + then |
| 172 | + continue |
| 173 | + fi |
| 174 | + |
| 175 | + if [ "$_key" == $CSI_KEY_ENTER ] |
| 176 | + then |
| 177 | + fn_ctl_browse_select || return 1 |
| 178 | + continue |
| 179 | + fi |
| 180 | + done |
| 181 | + |
| 182 | + return 0 |
| 183 | +} |
| 184 | + |
| 185 | +function fn_ctl_browse_select() |
| 186 | +{ |
| 187 | + local _selected_file |
| 188 | + fn_vmenu_get_current_val '_selected_file' |
| 189 | + |
| 190 | + if [ ! -d "$_selected_file" ] |
| 191 | + then |
| 192 | + # we got the enter key so close the menu |
| 193 | + local _rel_dir |
| 194 | + fn_ctl_get_relpath '_rel_dir' "$CTL_ORIG_PWD" "$PWD" |
| 195 | + biw_selection_result="${_rel_dir}/${_selected_file}" |
| 196 | + return 1 |
| 197 | + fi |
| 198 | + |
| 199 | + # change real directories |
| 200 | + cd "$_selected_file" || exit 1 |
| 201 | + |
| 202 | + # update panel with new directory |
| 203 | + fn_ctl_browse_update |
| 204 | + |
| 205 | + return 0 |
| 206 | +} |
| 207 | + |
| 208 | +function fn_ctl_browse_update() |
| 209 | +{ |
| 210 | + # fetch files |
| 211 | + local -a _ls_output |
| 212 | + local -r _ls_command="/bin/ls -a -p -1" |
| 213 | + |
| 214 | + # read ls command results into an array |
| 215 | + mapfile -n$CTL_BROWSE_MAX_FILES -s1 -t _ls_output < <($_ls_command) |
| 216 | + |
| 217 | + local -a _file_list=() |
| 218 | + local -a _file_list_ind=() |
| 219 | + local -a _dir_list=() |
| 220 | + local -a _dir_list_ind=() |
| 221 | + |
| 222 | + local -i _file_idx |
| 223 | + local _file |
| 224 | + |
| 225 | + for((_file_idx=0; _file_idx < ${#_ls_output[@]}; _file_idx++)) |
| 226 | + do |
| 227 | + _file=${_ls_output[_file_idx]} |
| 228 | + |
| 229 | + if [ "$_file" == '../' ] |
| 230 | + then |
| 231 | + _dir_list+=( "$_file" ) |
| 232 | + _dir_list_ind+=( $BIW_CHAR_TRIANGLE_LT ) |
| 233 | + elif [[ "$_file" =~ ^(.*)/$ ]] |
| 234 | + then |
| 235 | + _dir_list+=( "$_file" ) |
| 236 | + _dir_list_ind+=( $BIW_CHAR_TRIANGLE_RT ) |
| 237 | + else |
| 238 | + _file_list+=( "$_file" ) |
| 239 | + _file_list_ind+=( $BIW_CHAR_BULLET ) |
| 240 | + fi |
| 241 | + done |
| 242 | + |
| 243 | + local -a _dir_view=( "${_dir_list[@]}" ) |
| 244 | + |
| 245 | + if [ ${#_file_list[@]} != 0 ] |
| 246 | + then |
| 247 | + _dir_view+=( "${_file_list[@]}" ) |
| 248 | + fi |
| 249 | + |
| 250 | + fn_vmenu_init _dir_view[@] |
| 251 | + vmenu_ind_values=( "${_dir_list_ind[@]}" "${_file_list_ind[@]:-}" ) |
| 252 | + |
| 253 | + fn_vmenu_redraw |
| 254 | +} |
| 255 | + |
| 256 | +# Return relative path from canonical absolute dir path $1 to canonical |
| 257 | +# absolute dir path $2 ($1 and/or $2 may end with one or no "/"). |
| 258 | +# Does only need POSIX shell builtins (no external command) |
| 259 | +# source: http://stackoverflow.com/a/18898782/4316647 |
| 260 | +function fn_ctl_get_relpath() |
| 261 | +{ |
| 262 | + local _result_ref=$1 |
| 263 | + local _source_path="${2%/}" |
| 264 | + local _dest_path="${3%/}/" |
| 265 | + |
| 266 | + local _up_path='' |
| 267 | + |
| 268 | + while [ "${_dest_path#"$_source_path"/}" = "$_dest_path" ] |
| 269 | + do |
| 270 | + _source_path="${_source_path%/*}" |
| 271 | + _up_path="../${_up_path}" |
| 272 | + done |
| 273 | + |
| 274 | + _dest_path="${_up_path}${_dest_path#"$_source_path"/}" |
| 275 | + _dest_path="${_dest_path%/}" |
| 276 | + |
| 277 | + printf -v $_result_ref '%s' "${_dest_path:-.}" |
| 278 | +} |
0 commit comments