|
| 1 | +## |
| 2 | +# BIW-TOOLS - Bash Inline Widget Tools |
| 3 | +# Copyright 2017 by Chad Juliano |
| 4 | +# |
| 5 | +# Licensed under GNU Lesser General Public License v3.0 only. Some rights |
| 6 | +# reserved. See LICENSE. |
| 7 | +## |
| 8 | + |
| 9 | +# constants |
| 10 | +declare -r CRED_ANIMATE_DELAY=0.03 |
| 11 | +declare -ir CRED_CURSOR_PERIOD=14 |
| 12 | +declare -ir CRED_CURSOR_MAX=$((CRED_CURSOR_PERIOD * 3)) |
| 13 | + |
| 14 | +# panel layout |
| 15 | +declare -i cred_height |
| 16 | +declare -i cred_width |
| 17 | +declare -i cred_row_pos |
| 18 | + |
| 19 | +declare -a cred_line_data |
| 20 | +declare -i cred_line_data_size |
| 21 | + |
| 22 | +declare -i cred_canvas_row_pos |
| 23 | +declare -i cred_canvas_col_pos |
| 24 | +declare -i cred_canvas_width |
| 25 | +declare -i cred_canvas_height |
| 26 | + |
| 27 | +declare -a cred_color_map |
| 28 | +declare -i cred_color_map_size |
| 29 | +declare -a cred_alpha_map |
| 30 | + |
| 31 | +function fn_cred_show() |
| 32 | +{ |
| 33 | + # Panel geometry |
| 34 | + cred_height=$((BIW_PANEL_HEIGHT - HMENU_HEIGHT)) |
| 35 | + cred_width=$BIW_PANEL_WIDTH |
| 36 | + cred_row_pos=$HMENU_HEIGHT |
| 37 | + |
| 38 | + # geometry of drawing canvas |
| 39 | + cred_canvas_row_pos=$cred_row_pos |
| 40 | + cred_canvas_col_pos=1 |
| 41 | + cred_canvas_width=$((cred_width - 2)) |
| 42 | + cred_canvas_height=$((cred_height - 1)) |
| 43 | + |
| 44 | + cred_alpha_map[cred_canvas_width]=0 |
| 45 | + |
| 46 | + fn_cred_blank_panel |
| 47 | + fn_cred_load_data |
| 48 | + fn_generate_color_map |
| 49 | + fn_cred_print_data |
| 50 | + |
| 51 | + # if animation was canceled then we get a non-zero status |
| 52 | + return $? |
| 53 | +} |
| 54 | + |
| 55 | +function fn_generate_color_map() |
| 56 | +{ |
| 57 | + fn_theme_get_sgr $SGR_ATTR_FG $TATTR_TEXT |
| 58 | + _sgr_text_color=$(($? - SGR_ATTR_FG)) |
| 59 | + |
| 60 | + cred_color_map=( |
| 61 | + [1]=$_sgr_text_color |
| 62 | + [2]=$SGR_COL_YELLOW |
| 63 | + [3]=$SGR_COL_YELLOW |
| 64 | + [4]=$SGR_COL_YELLOW |
| 65 | + [5]=$SGR_COL_YELLOW |
| 66 | + [6]=$SGR_COL_YELLOW |
| 67 | + [7]=$SGR_COL_YELLOW |
| 68 | + [8]=$SGR_COL_YELLOW |
| 69 | + [9]=$SGR_COL_YELLOW |
| 70 | + [10]=$SGR_COL_YELLOW |
| 71 | + [11]=$SGR_COL_YELLOW |
| 72 | + [12]=$SGR_COL_YELLOW |
| 73 | + ) |
| 74 | + |
| 75 | + cred_color_map_size=${#cred_color_map[@]} |
| 76 | +} |
| 77 | + |
| 78 | +function fn_cred_print_data() |
| 79 | +{ |
| 80 | + local -i _line_idx |
| 81 | + local -i _persist_cursor=0 |
| 82 | + |
| 83 | + for((_line_idx = 0; _line_idx < cred_canvas_height; _line_idx++)) |
| 84 | + do |
| 85 | + local _line_val="${cred_line_data[_line_idx]:-}" |
| 86 | + fn_cred_canvas_set_cursor $_line_idx 0 |
| 87 | + |
| 88 | + if((_line_idx == (cred_canvas_height - 1))) |
| 89 | + then |
| 90 | + _persist_cursor=1 |
| 91 | + fi |
| 92 | + |
| 93 | + if ! fn_cred_print_line "${_line_val}" $_persist_cursor |
| 94 | + then |
| 95 | + # cancel by user input |
| 96 | + return 1 |
| 97 | + fi |
| 98 | + done |
| 99 | + |
| 100 | + return 0 |
| 101 | +} |
| 102 | + |
| 103 | +function fn_cred_print_line() |
| 104 | +{ |
| 105 | + local _line_val=$1 |
| 106 | + local -i _persist_cursor=$2 |
| 107 | + |
| 108 | + local -i _line_width=${#_line_val} |
| 109 | + |
| 110 | + if((_line_width == 0)) |
| 111 | + then |
| 112 | + return 0 |
| 113 | + fi |
| 114 | + |
| 115 | + # init zero valued array |
| 116 | + local _char_idx |
| 117 | + for((_char_idx=0; _char_idx < _line_width; _char_idx++)) |
| 118 | + do |
| 119 | + cred_alpha_map[_char_idx]=0 |
| 120 | + done |
| 121 | + |
| 122 | + # set the cursor in a position that will center the text |
| 123 | + local -i _line_start=$(((cred_canvas_width - _line_width) / 2)) |
| 124 | + |
| 125 | + # sprite indicators |
| 126 | + local -i _text_active=1 |
| 127 | + local -i _alpha_active=1 |
| 128 | + local -i _cursor_active=0 |
| 129 | + |
| 130 | + local -i _char_idx=0 |
| 131 | + local -i _cursor_counter=$CRED_CURSOR_MAX |
| 132 | + local _result_char |
| 133 | + |
| 134 | + # start main animation loop |
| 135 | + while fn_biw_process_key _result_char $CRED_ANIMATE_DELAY |
| 136 | + do |
| 137 | + # activate chars in alpha map |
| 138 | + if((_text_active)) |
| 139 | + then |
| 140 | + if ! fn_cred_animate_text $_char_idx $_line_width |
| 141 | + then |
| 142 | + _text_active=0 |
| 143 | + _cursor_active=1 |
| 144 | + fi |
| 145 | + ((_char_idx++)) |
| 146 | + fi |
| 147 | + |
| 148 | + # process alpha map |
| 149 | + if((_alpha_active)) |
| 150 | + then |
| 151 | + if ! fn_cred_print_alpha "$_line_val" $_line_start |
| 152 | + then |
| 153 | + _alpha_active=0 |
| 154 | + fi |
| 155 | + fi |
| 156 | + |
| 157 | + # process cursor |
| 158 | + if((_cursor_active)) |
| 159 | + then |
| 160 | + if ! fn_cred_animate_cursor $((_line_start + _line_width)) |
| 161 | + then |
| 162 | + _cursor_active=$_persist_cursor |
| 163 | + fi |
| 164 | + ((_cursor_counter--)) |
| 165 | + fi |
| 166 | + |
| 167 | + if((!_alpha_active && !_cursor_active)) |
| 168 | + then |
| 169 | + # nothing left for fn_cred_print_alpha |
| 170 | + # normal termination |
| 171 | + return 0 |
| 172 | + fi |
| 173 | + done |
| 174 | + |
| 175 | + # cancel by user input |
| 176 | + return 1 |
| 177 | +} |
| 178 | + |
| 179 | +function fn_cred_animate_text() |
| 180 | +{ |
| 181 | + local -i _char_idx=$1 |
| 182 | + local -i _line_width=$2 |
| 183 | + |
| 184 | + # add characters to the alpha map |
| 185 | + if((_char_idx < _line_width)) |
| 186 | + then |
| 187 | + cred_alpha_map[_char_idx]=${#cred_color_map[@]} |
| 188 | + else |
| 189 | + return 1 |
| 190 | + fi |
| 191 | + |
| 192 | + return 0 |
| 193 | +} |
| 194 | + |
| 195 | +function fn_cred_animate_cursor() |
| 196 | +{ |
| 197 | + local -i _cursor_pos=$1 |
| 198 | + |
| 199 | + if(( _cursor_counter % (CRED_CURSOR_PERIOD / 2) )) |
| 200 | + then |
| 201 | + # nothing to do |
| 202 | + return 0 |
| 203 | + fi |
| 204 | + |
| 205 | + fn_biw_set_col_pos $_cursor_pos |
| 206 | + |
| 207 | + local _sgr_color=${cred_color_map[cred_color_map_size - 1]} |
| 208 | + fn_sgr_set $((SGR_ATTR_FG + _sgr_color)) |
| 209 | + |
| 210 | + if((_cursor_counter <= 0)) |
| 211 | + then |
| 212 | + # terminate |
| 213 | + _cursor_counter=$((CRED_CURSOR_MAX + 1)) |
| 214 | + fn_sgr_print " " |
| 215 | + return 1 |
| 216 | + fi |
| 217 | + |
| 218 | + local _sgr_char=" " |
| 219 | + if(( !(_cursor_counter % CRED_CURSOR_PERIOD) )) |
| 220 | + then |
| 221 | + _sgr_char="_" |
| 222 | + fi |
| 223 | + |
| 224 | + fn_sgr_print "$_sgr_char" |
| 225 | + |
| 226 | + return 0 |
| 227 | +} |
| 228 | + |
| 229 | +function fn_cred_print_alpha() |
| 230 | +{ |
| 231 | + local _line_val=$1 |
| 232 | + local -i _line_start=$2 |
| 233 | + local -i _line_width=${#_line_val} |
| 234 | + |
| 235 | + local -i _alpha_idx |
| 236 | + local -i _alpha_val |
| 237 | + local -i _alpha_start=-1 |
| 238 | + |
| 239 | + # find the first nonzero alpha value |
| 240 | + for((_alpha_idx=0; _alpha_idx < $_line_width; _alpha_idx++)) |
| 241 | + do |
| 242 | + _alpha_val=${cred_alpha_map[_alpha_idx]} |
| 243 | + |
| 244 | + if((_alpha_val > 0)) |
| 245 | + then |
| 246 | + _alpha_start=_alpha_idx |
| 247 | + break |
| 248 | + fi |
| 249 | + done |
| 250 | + |
| 251 | + if((_alpha_start < 0)) |
| 252 | + then |
| 253 | + # nothing to do |
| 254 | + return 1 |
| 255 | + fi |
| 256 | + |
| 257 | + fn_biw_set_col_pos $((_line_start + _alpha_start)) |
| 258 | + |
| 259 | + fn_sgr_seq_start |
| 260 | + |
| 261 | + local _char_val |
| 262 | + local -i _alpha_color |
| 263 | + |
| 264 | + for((_alpha_idx=_alpha_start; _alpha_idx < $_line_width; _alpha_idx++)) |
| 265 | + do |
| 266 | + _alpha_val=${cred_alpha_map[_alpha_idx]} |
| 267 | + if((_alpha_val == 0)) |
| 268 | + then |
| 269 | + # we hit the end |
| 270 | + break |
| 271 | + fi |
| 272 | + |
| 273 | + _alpha_color=${cred_color_map[_alpha_val]} |
| 274 | + fn_sgr_set $((SGR_ATTR_FG + _alpha_color)) |
| 275 | + |
| 276 | + _char_val=${_line_val:_alpha_idx:1} |
| 277 | + fn_sgr_print "${_char_val}" |
| 278 | + |
| 279 | + # each value in the alpha list needs to get decremented |
| 280 | + cred_alpha_map[_alpha_idx]=$((_alpha_val - 1)) |
| 281 | + done |
| 282 | + |
| 283 | + # this will flush the buffer and print the output |
| 284 | + fn_sgr_seq_flush |
| 285 | + |
| 286 | + return 0 |
| 287 | +} |
| 288 | + |
| 289 | +function fn_cred_load_data() |
| 290 | +{ |
| 291 | + mapfile -n${cred_height} -t cred_line_data <<-EOM |
| 292 | +
|
| 293 | +BIW - Bash Inline Widgets |
| 294 | +Copyright 2017 by Chad Juliano |
| 295 | + |
| 296 | +
|
| 297 | +Find it at: |
| 298 | +https://github.com/chadj2/biw-tools |
| 299 | +EOM |
| 300 | + |
| 301 | + cred_line_data_size=${#cred_line_data[*]} |
| 302 | +} |
| 303 | + |
| 304 | +function fn_cred_canvas_set_cursor() |
| 305 | +{ |
| 306 | + local -i _row_pos=$1 |
| 307 | + local -i _col_pos=$2 |
| 308 | + fn_biw_set_cursor_pos \ |
| 309 | + $((cred_canvas_row_pos + _row_pos)) \ |
| 310 | + $((cred_canvas_col_pos + _col_pos)) |
| 311 | + |
| 312 | + fn_theme_set_bg_attr $TATTR_BG_INACTIVE |
| 313 | +} |
| 314 | + |
| 315 | +function fn_cred_blank_panel() |
| 316 | +{ |
| 317 | + local -i _line_idx |
| 318 | + |
| 319 | + for((_line_idx = 0; _line_idx < cred_height; _line_idx++)) |
| 320 | + do |
| 321 | + local -i _row_pos=$((cred_row_pos + _line_idx)) |
| 322 | + fn_biw_set_cursor_pos $_row_pos 0 |
| 323 | + |
| 324 | + fn_sgr_seq_start |
| 325 | + fn_theme_set_bg_attr $TATTR_BG_INACTIVE |
| 326 | + |
| 327 | + if((_line_idx < cred_canvas_height)) |
| 328 | + then |
| 329 | + fn_cred_blank_line |
| 330 | + else |
| 331 | + fn_cred_draw_bottom |
| 332 | + fi |
| 333 | + fn_sgr_seq_flush |
| 334 | + done |
| 335 | +} |
| 336 | + |
| 337 | +function fn_cred_blank_line() |
| 338 | +{ |
| 339 | + local _line_val |
| 340 | + fn_cred_repeat_chars "_line_val" $cred_canvas_width |
| 341 | + fn_sgr_print $CSI_CHAR_LINE_VERT |
| 342 | + fn_sgr_print "$_line_val" |
| 343 | + fn_sgr_print $CSI_CHAR_LINE_VERT |
| 344 | +} |
| 345 | + |
| 346 | +function fn_cred_draw_bottom() |
| 347 | +{ |
| 348 | + local _dsc_start=$'\e(0' |
| 349 | + local _dsc_horiz_line=$'\x71' |
| 350 | + local _dsc_end=$'\e(B' |
| 351 | + |
| 352 | + # draw bottom box |
| 353 | + fn_sgr_print $CSI_CHAR_LINE_BL |
| 354 | + fn_sgr_print $_dsc_start |
| 355 | + |
| 356 | + local _bottom_line |
| 357 | + fn_cred_repeat_chars "_bottom_line" $cred_canvas_width $_dsc_horiz_line |
| 358 | + fn_sgr_print $_bottom_line |
| 359 | + |
| 360 | + fn_sgr_print $CSI_CHAR_LINE_BR |
| 361 | + fn_sgr_print $_dsc_end |
| 362 | +} |
| 363 | + |
| 364 | +function fn_cred_repeat_chars() |
| 365 | +{ |
| 366 | + local _var_name=$1 |
| 367 | + local -i _pad_width=$2 |
| 368 | + local _pad_char=${3:- } |
| 369 | + |
| 370 | + printf -v $_var_name '%*s' $_pad_width |
| 371 | + local _result_val=${!_var_name// /${_pad_char}} |
| 372 | + eval $_var_name='$_result_val' |
| 373 | +} |
0 commit comments