From 1ae18daa5cf65385efb5bc899b8e5942d6e3b8cd Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sat, 4 Feb 2017 18:41:07 +0100 Subject: [PATCH] fix redraw issue --- git-recall | 60 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/git-recall b/git-recall index b716ef6..b18cd24 100644 --- a/git-recall +++ b/git-recall @@ -127,16 +127,7 @@ NI=${#COMMITS[@]} # Total number of items. SN=$(( `tput lines` - 1 )) # Screen's number of lines. CN=$(tput cols) # Screen's number of columns. TN=$(( $NI < $((SN -1)) ? $NI : $((SN -1)))) # Number of lines that we will display. -OFFSET=0 - -# Add +1 to OFFSET if a commit's length is bigger than the current terminal session's width. (This is to fix a redraw issue) -for C in "${COMMITS[@]}" -do - ELT="$(echo "$C" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes - if [[ ${#ELT} -gt $CN ]]; then - OFFSET=$(( OFFSET + 1 )) - fi -done +OFFSET=0 #Incremented by one each time a commit's length is higher than teminal width. # If there is no items, exit. if [[ $NI = 0 ]]; then @@ -147,6 +138,15 @@ if [[ $NI = 0 ]]; then fi fi +# Add +1 to OFFSET if a commit's length is bigger than the current terminal session's width. (This is to fix a redraw issue) +for C in "${COMMITS[@]}" +do + ELT="$(echo "$C" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes + if [[ ${#ELT} -gt $CN ]]; then + OFFSET=$(( OFFSET + 1 )) + fi +done + # Set keys. au="`echo -e '\e[A'`" # arrow up au_1="k" # arrow up @@ -206,6 +206,20 @@ function print_diff() { fi } +# Calculate OFFSET to avoid bad redraw. (Really bad performance.) +function calculate_offset { + # Add +1 to OFFSET if a commit's length is bigger than the current terminal session's width. (This is to fix a redraw issue) + OFFSET=0 + limit=$(( SN + SI )) + tmp=("${COMMITS[@]:$SI:$limit}") + for C in "${tmp[@]}" + do + ELT="$(echo "$C" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes + if [[ ${#ELT} -gt $CN ]]; then + OFFSET=$(( OFFSET + 1 )) + fi + done +} { # capture stdout to stderr @@ -218,7 +232,19 @@ END=false # end while loop while ! $END do - for i in `seq $SI $(( $TN + $SI -1 ))` + # Set last index to print. (based on OFFSET) + if [[ $TN == $NI ]]; then + END_INDEX=$TN + elif [[ $TN == $(( SN - 1 )) ]]; then + # Calculate new OFFSET. + if [[ $OFFSET != 0 ]]; then + calculate_offset #This involve really bad performance. Should be reconsidered. + fi + END_INDEX=$(( TN + SI -1 - OFFSET )) + fi + + # Loop and echo commits + for i in `seq $SI $END_INDEX` do echo -n "$NORMAL" [[ $CP == $i ]] && echo -n "$REVERSE" @@ -236,18 +262,22 @@ do "$au" | "$au_1") CP=$(( CP - 1 )) - [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) + [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$(( NI - OFFSET )) && SI=$(( NI - SN + 2 )) [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - tput cuu $(( TN + OFFSET )) + + [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET )) + [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( SN - 1 )) [[ $SI != 1 ]] && tput ed # clear screen ;; "$ad" | "$ad_1") CP=$(( CP + 1 )) [[ $CP == $(( NI + 1 )) ]] && CP=1 && SI=1 - [[ $CP == $(( SN + SI - 1)) ]] && [[ $TN == $(( SN - 1 )) ]] && SI=$(( SI + 1 )) - tput cuu $(( TN + OFFSET )) + [[ $CP == $(( SN + SI - 1 - OFFSET )) ]] && [[ $TN == $(( SN - 1 )) ]] && SI=$(( SI + 1 )) + + [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET )) + [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( SN - 1 )) [[ $SI != 1 ]] && tput ed # clear screen [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;;