diff --git a/git-recall b/git-recall index a35323f..5500dff 100644 --- a/git-recall +++ b/git-recall @@ -192,15 +192,23 @@ function get_diff() { DIFF_LINES_NUMBER=$(( DIFF_LINES_NUMBER + off )) } +cleanup_diff() +{ + clear_screen + cleanup_and_exit +} + # This function will print the diff according the commit's tip. If the diff is too long, the result will be displayed using 'less'. function print_diff() { get_diff if [[ $(( TN + DIFF_LINES_NUMBER + OFFSET )) -ge $(( `tput lines` - 1 )) ]]; then + trap cleanup_diff INT if [[ $LESSKEY = true ]]; then echo "$DIFF" | less -r -k /tmp/lsh_less_keys_tmp else echo "$DIFF" | less -r fi + trap cleanup_and_exit INT clear_screen else stop=false @@ -247,6 +255,24 @@ function calculate_offset { done } +# Reset changes made by this script before exiting +cleanup_and_exit() +{ + # remove temporary less keybindings + [[ $LESSKEY = true ]] && rm /tmp/lsh_less_keys_tmp + + tput cnorm # unhide cursor + echo "$NORMAL" # normal colors + stty "$orig_stty" > /dev/null 2>&1 + exit +} + +# Store original terminal settings +orig_stty=$(stty -g) + +# Catch a control-C interrupt and perform cleanup operations before exiting +trap cleanup_and_exit INT + { # capture stdout to stderr tput civis # hide cursor. @@ -324,11 +350,7 @@ do esac done -# remove temporary less keybindings -[[ $LESSKEY = true ]] && rm /tmp/lsh_less_keys_tmp - -tput cnorm # unhide cursor -echo "$NORMAL" # normal colors +cleanup_and_exit } >&2 # END capture