From bc28cfdf8f91cfaab496a3e99474a7f6be5b3321 Mon Sep 17 00:00:00 2001 From: Kevin MacMartin Date: Wed, 23 Aug 2023 23:30:45 -0400 Subject: [PATCH] Improve the tty state restoration logic: * Restore the tty state with a reusable function * Add tty_state as a global * Declare globals (including tty_state) as empty variables --- vnotes | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/vnotes b/vnotes index c465a10..79b0f6e 100755 --- a/vnotes +++ b/vnotes @@ -20,9 +20,10 @@ VNOTES_EXTENSION=${NOTES_SUFFIX:='md'} deps=('getkeycodes' 'stty' 'tput') declare -a documents=() -declare one_display -declare upto -max_docwidth=0 +declare one_display='' +declare upto='' +declare tty_state='' +declare max_docwidth=0 # Colour Scheme [[ -t 1 ]] && { @@ -106,10 +107,15 @@ function power { printf '%s\n' "$total" } +# Restore the tty state +function restore_tty_state { + [[ -n "$tty_state" ]] && stty "$tty_state" +} + # Opens a note function open_note { printf '%s %s\n' "${c_y}Opening:" "$c_m${documents[$1]}$c_c" - [[ -n "$tty_state" ]] && stty "$tty_state" + restore_tty_state "$EDITOR" "$VNOTES_FOLDER/${documents[$1]}.$VNOTES_EXTENSION" exit } @@ -234,7 +240,7 @@ function search_notes { tty_state="$(stty -g)" # Trap a function to restore the tty on exit - trap 'stty "$tty_state"; exit' SIGINT SIGQUIT SIGTERM + trap 'restore_tty_state; exit' SIGINT SIGQUIT SIGTERM # Begin to capture input stty cs8 -icanon -echo min 1 time 1 @@ -287,7 +293,7 @@ function search_notes { # Quit if input was given to do so (( value == -1 )) && { printf '\n%s\n' "${c_r}Quitting!$c_c" - [[ -n "$tty_state" ]] && stty "$tty_state" + restore_tty_state exit } @@ -307,8 +313,7 @@ function search_notes { done fi - # Restore the tty state - [[ -n "$tty_state" ]] && stty "$tty_state" + restore_tty_state } # Check for missing dependencies