From 2c0685565793f6f6fb70d03d68f705a646579361 Mon Sep 17 00:00:00 2001 From: Fakerr Date: Mon, 30 Jan 2017 10:03:44 +0100 Subject: [PATCH 1/8] update --- git-recall | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 git-recall diff --git a/git-recall b/git-recall new file mode 100644 index 0000000..75b0a8d --- /dev/null +++ b/git-recall @@ -0,0 +1,277 @@ +#!/usr/bin/env bash + + +# usage info +usage() { + cat <&2 + exit 1 +fi + +# Parse options +while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do + case $1 in + -v | --version ) + echo $version + exit + ;; + -d | --date ) + SINCE="$2 days ago" + shift; + ;; + -a | --author ) + AUTHOR="$2" + shift + ;; + -f | --fetch ) + FETCH=true + ;; + -h | --help ) + usage + exit + ;; + * ) + echo "abort: unknown argument" 1>&2 + exit 1 + esac + shift +done +if [[ "$1" == "--" ]]; then shift; fi + + +# Colored output. +function colored() { + GREEN=$(tput setaf 4) + YELLOW=$(tput setaf 3) + NORMAL=$(tput sgr0) + REVERSE=$(tput rev) +} + +# Uncolored output. +function uncolored() { + GREEN="" + YELLOW="" + NORMAL="" + REVERSE="" +} + +# Enable colors if supported by terminal. +if [[ -t 1 ]] && [[ -n "$TERM" ]] && which tput &>/dev/null && tput colors &>/dev/null; then + ncolors=$(tput colors) + if [[ -n "$ncolors" ]] && [[ "$ncolors" -ge 8 ]] ; then + colored + else + uncolored + fi +else + uncolored +fi + +# Check if lesskey is correctly installed. +if ! which lesskey &>/dev/null; then + echo "abort: the program lesskey is not installed. Make sure to install it before running git-recall" 1>&2 + exit 1 +fi + +# Set AUTHOR to current user if no param passed or display for all users if param equal to "all". +if [[ ! -n $AUTHOR ]]; then + AUTHOR=$(git config user.name 2>/dev/null) +elif [[ $AUTHOR = "all" ]]; then + AUTHOR=".*" +fi + +# Fetch changes before. +if [[ $FETCH == true ]]; then + echo "${GREEN}Fetching changes...${NORMAL}" + git fetch --all &> /dev/null + tput cuu1 + tput ed # clear screen +fi + +# Log template. +GIT_FORMAT="%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" + +# Log command. +GIT_LOG="git log --pretty=format:'${GIT_FORMAT}' + --author \"$AUTHOR\" + --since \"$SINCE\" --abbrev-commit" + +# Change temporary the IFS to store GIT_LOG's output into an array. +IFS=$'\n' +COMMITS=($(eval ${GIT_LOG} 2>/dev/null)) +unset IFS + +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 + +# If there is no items, exit. +if [[ $NI = 0 ]]; then + if [[ $AUTHOR = ".*" ]]; then + echo "${YELLOW}All contributors did nothing during this period.${NORMAL}" && exit 0 + else + echo "${YELLOW}The contributor \"${AUTHOR}\" did nothing during this period.${NORMAL}" && exit 0 + fi +fi + +# Set keys. +au="`echo -e '\e[A'`" # arrow up +au_1="k" # arrow up +ad="`echo -e '\e[B'`" # arrow down +ad_1="j" # arrow down +ec="`echo -e '\e'`" # escape +nl="`echo -e '\n'`" # newline + +# Create a temporary lesskey file to change the keybindings so the user can use the TAB key to quit less. (more convenient) +echo "\t quit" | lesskey -o $HOME/.lsh_less_keys_tmp -- - &> /dev/null + +## Get commit's diff. +function get_diff() { + ELT="$(echo "${COMMITS[$CP-1]}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes + DIFF_TIP=${ELT:0:7} + DIFF_CMD="git show $DIFF_TIP --color=always" + DIFF=$(eval ${DIFF_CMD} 2>/dev/null) + DIFF_LINES_NUMBER="$(echo "$DIFF" | wc -l)" +} + +## 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 )) -ge $(( `tput lines` - 1 )) ]]; then + echo -e "$REVERSE ${COMMITS[$CP - 1]}\n\n $DIFF" | less -r -k $HOME/.lsh_less_keys_tmp + else + stop=false + tput ed + for i in `seq 1 $TN` + do + echo -n "$NORMAL" + [[ $CP == $i ]] && echo -n "$REVERSE" + echo "${COMMITS[$i - 1]}" + + if [[ $CP == $i ]]; then + echo "$DIFF" + fi + done + + while ! $stop + do + read -sn 1 key + case "$key" in + "$nl") + stop=true + ;; + "q") + stop=true + END=true + ;; + esac + done + [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed + [[ $END = true ]] && tput cuu 1 + fi +} + + +{ # capture stdout to stderr + +tput civis # hide cursor. +CP=1 # current position +SI=1 # index +END=false # end while loop + +## Loops, reads inputs and prints commits until user presses 'q' to exit or TAB to show the diff. +while ! $END +do + + for i in `seq $SI $(( $TN - $OFFSET + $SI -1 ))` + do + echo -n "$NORMAL" + [[ $CP == $i ]] && echo -n "$REVERSE" + echo "${COMMITS[$i - 1]}" + done + + read -sn 1 key + [[ "$key" == "$ec" ]] && + { + read -sn 2 k2 + key="$key$k2" + } + + case "$key" in + + "$au" | "$au_1") + CP=$(( CP - 1 )) + [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) + [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN + [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) + tput cuu $(( TN + OFFSET )) + [[ $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 )) + [[ $SI != 1 ]] && tput ed # clear screen + [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen + ;; + + "$nl") + tput cuu $(( TN + OFFSET )) + print_diff + ;; + + "q") + si=false + END=true + tput cuu 1 + ;; + + * ) + tput cuu $TN + esac + +done + +rm $HOME/.lsh_less_keys_tmp # remove temporary less keybindings +tput cnorm # unhide cursor +echo "$NORMAL" # normal colors + +} >&2 # END capture + From 94263c19bf1a92f27f755a5066ff7fb71611b6ee Mon Sep 17 00:00:00 2001 From: Fakerr Date: Mon, 30 Jan 2017 10:10:29 +0100 Subject: [PATCH 2/8] fix bad merging --- git-recall | 23 ++-- git-recall~HEAD | 277 ------------------------------------------------ 2 files changed, 17 insertions(+), 283 deletions(-) delete mode 100644 git-recall~HEAD diff --git a/git-recall b/git-recall index 1daaac9..75b0a8d 100644 --- a/git-recall +++ b/git-recall @@ -124,8 +124,19 @@ COMMITS=($(eval ${GIT_LOG} 2>/dev/null)) unset IFS NI=${#COMMITS[@]} # Total number of items. -SN=$(( `tput lines` - 1 )) # Screen number of lines. +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 # If there is no items, exit. if [[ $NI = 0 ]]; then @@ -190,7 +201,7 @@ function print_diff() { ;; esac done - [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER )) && tput ed + [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed [[ $END = true ]] && tput cuu 1 fi } @@ -207,7 +218,7 @@ END=false # end while loop while ! $END do - for i in `seq $SI $(( $TN + $SI -1 ))` + for i in `seq $SI $(( $TN - $OFFSET + $SI -1 ))` do echo -n "$NORMAL" [[ $CP == $i ]] && echo -n "$REVERSE" @@ -228,7 +239,7 @@ do [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - tput cuu $TN + tput cuu $(( TN + OFFSET )) [[ $SI != 1 ]] && tput ed # clear screen ;; @@ -236,13 +247,13 @@ do CP=$(( CP + 1 )) [[ $CP == $(( NI + 1 )) ]] && CP=1 && SI=1 [[ $CP == $(( SN + SI - 1)) ]] && [[ $TN == $(( SN - 1 )) ]] && SI=$(( SI + 1 )) - tput cuu $TN + tput cuu $(( TN + OFFSET )) [[ $SI != 1 ]] && tput ed # clear screen [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;; "$nl") - tput cuu $TN + tput cuu $(( TN + OFFSET )) print_diff ;; diff --git a/git-recall~HEAD b/git-recall~HEAD deleted file mode 100644 index 75b0a8d..0000000 --- a/git-recall~HEAD +++ /dev/null @@ -1,277 +0,0 @@ -#!/usr/bin/env bash - - -# usage info -usage() { - cat <&2 - exit 1 -fi - -# Parse options -while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do - case $1 in - -v | --version ) - echo $version - exit - ;; - -d | --date ) - SINCE="$2 days ago" - shift; - ;; - -a | --author ) - AUTHOR="$2" - shift - ;; - -f | --fetch ) - FETCH=true - ;; - -h | --help ) - usage - exit - ;; - * ) - echo "abort: unknown argument" 1>&2 - exit 1 - esac - shift -done -if [[ "$1" == "--" ]]; then shift; fi - - -# Colored output. -function colored() { - GREEN=$(tput setaf 4) - YELLOW=$(tput setaf 3) - NORMAL=$(tput sgr0) - REVERSE=$(tput rev) -} - -# Uncolored output. -function uncolored() { - GREEN="" - YELLOW="" - NORMAL="" - REVERSE="" -} - -# Enable colors if supported by terminal. -if [[ -t 1 ]] && [[ -n "$TERM" ]] && which tput &>/dev/null && tput colors &>/dev/null; then - ncolors=$(tput colors) - if [[ -n "$ncolors" ]] && [[ "$ncolors" -ge 8 ]] ; then - colored - else - uncolored - fi -else - uncolored -fi - -# Check if lesskey is correctly installed. -if ! which lesskey &>/dev/null; then - echo "abort: the program lesskey is not installed. Make sure to install it before running git-recall" 1>&2 - exit 1 -fi - -# Set AUTHOR to current user if no param passed or display for all users if param equal to "all". -if [[ ! -n $AUTHOR ]]; then - AUTHOR=$(git config user.name 2>/dev/null) -elif [[ $AUTHOR = "all" ]]; then - AUTHOR=".*" -fi - -# Fetch changes before. -if [[ $FETCH == true ]]; then - echo "${GREEN}Fetching changes...${NORMAL}" - git fetch --all &> /dev/null - tput cuu1 - tput ed # clear screen -fi - -# Log template. -GIT_FORMAT="%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" - -# Log command. -GIT_LOG="git log --pretty=format:'${GIT_FORMAT}' - --author \"$AUTHOR\" - --since \"$SINCE\" --abbrev-commit" - -# Change temporary the IFS to store GIT_LOG's output into an array. -IFS=$'\n' -COMMITS=($(eval ${GIT_LOG} 2>/dev/null)) -unset IFS - -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 - -# If there is no items, exit. -if [[ $NI = 0 ]]; then - if [[ $AUTHOR = ".*" ]]; then - echo "${YELLOW}All contributors did nothing during this period.${NORMAL}" && exit 0 - else - echo "${YELLOW}The contributor \"${AUTHOR}\" did nothing during this period.${NORMAL}" && exit 0 - fi -fi - -# Set keys. -au="`echo -e '\e[A'`" # arrow up -au_1="k" # arrow up -ad="`echo -e '\e[B'`" # arrow down -ad_1="j" # arrow down -ec="`echo -e '\e'`" # escape -nl="`echo -e '\n'`" # newline - -# Create a temporary lesskey file to change the keybindings so the user can use the TAB key to quit less. (more convenient) -echo "\t quit" | lesskey -o $HOME/.lsh_less_keys_tmp -- - &> /dev/null - -## Get commit's diff. -function get_diff() { - ELT="$(echo "${COMMITS[$CP-1]}" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes - DIFF_TIP=${ELT:0:7} - DIFF_CMD="git show $DIFF_TIP --color=always" - DIFF=$(eval ${DIFF_CMD} 2>/dev/null) - DIFF_LINES_NUMBER="$(echo "$DIFF" | wc -l)" -} - -## 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 )) -ge $(( `tput lines` - 1 )) ]]; then - echo -e "$REVERSE ${COMMITS[$CP - 1]}\n\n $DIFF" | less -r -k $HOME/.lsh_less_keys_tmp - else - stop=false - tput ed - for i in `seq 1 $TN` - do - echo -n "$NORMAL" - [[ $CP == $i ]] && echo -n "$REVERSE" - echo "${COMMITS[$i - 1]}" - - if [[ $CP == $i ]]; then - echo "$DIFF" - fi - done - - while ! $stop - do - read -sn 1 key - case "$key" in - "$nl") - stop=true - ;; - "q") - stop=true - END=true - ;; - esac - done - [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed - [[ $END = true ]] && tput cuu 1 - fi -} - - -{ # capture stdout to stderr - -tput civis # hide cursor. -CP=1 # current position -SI=1 # index -END=false # end while loop - -## Loops, reads inputs and prints commits until user presses 'q' to exit or TAB to show the diff. -while ! $END -do - - for i in `seq $SI $(( $TN - $OFFSET + $SI -1 ))` - do - echo -n "$NORMAL" - [[ $CP == $i ]] && echo -n "$REVERSE" - echo "${COMMITS[$i - 1]}" - done - - read -sn 1 key - [[ "$key" == "$ec" ]] && - { - read -sn 2 k2 - key="$key$k2" - } - - case "$key" in - - "$au" | "$au_1") - CP=$(( CP - 1 )) - [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) - [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN - [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - tput cuu $(( TN + OFFSET )) - [[ $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 )) - [[ $SI != 1 ]] && tput ed # clear screen - [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen - ;; - - "$nl") - tput cuu $(( TN + OFFSET )) - print_diff - ;; - - "q") - si=false - END=true - tput cuu 1 - ;; - - * ) - tput cuu $TN - esac - -done - -rm $HOME/.lsh_less_keys_tmp # remove temporary less keybindings -tput cnorm # unhide cursor -echo "$NORMAL" # normal colors - -} >&2 # END capture - From 64b3407c54ab1e0a78d6290322891a6de3a20f0a Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sat, 4 Feb 2017 01:03:57 +0100 Subject: [PATCH 3/8] tentative fix --- git-recall | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/git-recall b/git-recall index 75b0a8d..fd4329f 100644 --- a/git-recall +++ b/git-recall @@ -130,13 +130,13 @@ TN=$(( $NI < $((SN -1)) ? $NI : $((SN -1)))) # Number of lines that we will disp 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 +#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 # If there is no items, exit. if [[ $NI = 0 ]]; then @@ -201,8 +201,8 @@ function print_diff() { ;; esac done - [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed - [[ $END = true ]] && tput cuu 1 + [[ $END = false ]] && tput rc && tput ed #restore saved position and clear screen. + [[ $END = true ]] && tput cuu 1 #move cursor up one line. fi } @@ -218,6 +218,7 @@ END=false # end while loop while ! $END do + tput sc for i in `seq $SI $(( $TN - $OFFSET + $SI -1 ))` do echo -n "$NORMAL" @@ -239,28 +240,33 @@ do [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - tput cuu $(( TN + OFFSET )) - [[ $SI != 1 ]] && tput ed # clear screen + #tput cuu $(( TN )) + tput rc + tput ed + #[[ $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 )) - [[ $SI != 1 ]] && tput ed # clear screen - [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen + #tput cuu $(( TN )) + tput rc + tput ed + #[[ $SI != 1 ]] && tput ed # clear screen + #[[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;; "$nl") - tput cuu $(( TN + OFFSET )) + #tput cuu $(( TN + OFFSET )) + tput rc print_diff ;; "q") si=false END=true - tput cuu 1 + tput cuu 1 #move cursor up one line. (remove extra line) ;; * ) From 9d50eda776402cdb8c7df6a96ab654b7d3613992 Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sat, 4 Feb 2017 15:54:14 +0100 Subject: [PATCH 4/8] add offset when commit's length is bigger than temrminal widht --- git-recall | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/git-recall b/git-recall index fd4329f..b716ef6 100644 --- a/git-recall +++ b/git-recall @@ -130,13 +130,13 @@ TN=$(( $NI < $((SN -1)) ? $NI : $((SN -1)))) # Number of lines that we will disp 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 +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 # If there is no items, exit. if [[ $NI = 0 ]]; then @@ -201,8 +201,8 @@ function print_diff() { ;; esac done - [[ $END = false ]] && tput rc && tput ed #restore saved position and clear screen. - [[ $END = true ]] && tput cuu 1 #move cursor up one line. + [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed + [[ $END = true ]] && tput cuu 1 fi } @@ -218,8 +218,7 @@ END=false # end while loop while ! $END do - tput sc - for i in `seq $SI $(( $TN - $OFFSET + $SI -1 ))` + for i in `seq $SI $(( $TN + $SI -1 ))` do echo -n "$NORMAL" [[ $CP == $i ]] && echo -n "$REVERSE" @@ -240,26 +239,21 @@ do [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 )) [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - #tput cuu $(( TN )) - tput rc - tput ed - #[[ $SI != 1 ]] && tput ed # clear screen + tput cuu $(( TN + OFFSET )) + [[ $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 )) - tput rc - tput ed - #[[ $SI != 1 ]] && tput ed # clear screen - #[[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen + tput cuu $(( TN + OFFSET )) + [[ $SI != 1 ]] && tput ed # clear screen + [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;; "$nl") - #tput cuu $(( TN + OFFSET )) - tput rc + tput cuu $(( TN + OFFSET )) print_diff ;; @@ -270,7 +264,7 @@ do ;; * ) - tput cuu $TN + tput cuu $(( TN + OFFSET )) esac done From 1ae18daa5cf65385efb5bc899b8e5942d6e3b8cd Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sat, 4 Feb 2017 18:41:07 +0100 Subject: [PATCH 5/8] 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 ;; From 6de246fe28b5f64c269ce18265e6e532cf979ffe Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sun, 12 Feb 2017 22:50:29 +0100 Subject: [PATCH 6/8] Fix broken redraw --- git-recall | 84 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/git-recall b/git-recall index 6b2e787..4e188fb 100644 --- a/git-recall +++ b/git-recall @@ -22,7 +22,9 @@ FETCH=false GIT_FORMAT="" GIT_LOG="" COMMITS="" +COMMITS_UNCOL=() # commits without colors LESSKEY=false +SED_BIN="" # Sed option to use according OS. VERSION="1.1.4" # Are we in a git repo? @@ -138,11 +140,23 @@ 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[@]}" +# Set correct sed option according OS's type +case "$OSTYPE" in + darwin*) SED_BIN="sed -E" ;; + *) SED_BIN="sed -r" ;; +esac + +# Create array with uncolred commits (removing escape sequences using sed) +for elt 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 + ELT="$(echo "$elt" | $SED_BIN "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes + COMMITS_UNCOL+=("$ELT") +done + +# 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_UNCOL[@]}" +do + if [[ ${#C} -gt $CN ]]; then OFFSET=$(( OFFSET + 1 )) fi done @@ -162,16 +176,13 @@ fi ## Get commit's diff. function get_diff() { - case "$OSTYPE" in - darwin*) SED_BIN="sed -E" ;; - *) SED_BIN="sed -r" ;; - esac - - ELT="$(echo "${COMMITS[$CP-1]}" | $SED_BIN "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes + ELT="$(echo "${COMMITS_UNCOL[$CP-1]}")" DIFF_TIP=${ELT:0:7} DIFF_CMD="git show $DIFF_TIP --color=always" DIFF=$(eval ${DIFF_CMD} 2>/dev/null) + off=$(echo "$DIFF" | grep -c ".\{$CN\}") #Number of lines in the diff that are longer than terminal width. DIFF_LINES_NUMBER="$(echo "$DIFF" | wc -l)" + DIFF_LINES_NUMBER=$(( DIFF_LINES_NUMBER + off )) } ## This function will print the diff according the commit's tip. If the diff is too long, the result will be displayed using 'less'. @@ -179,7 +190,7 @@ function print_diff() { get_diff - if [[ $(( TN + DIFF_LINES_NUMBER )) -ge $(( `tput lines` - 1 )) ]]; then + if [[ $(( TN + DIFF_LINES_NUMBER + OFFSET )) -ge $(( `tput lines` - 1 )) ]]; then if [[ $LESSKEY = true ]]; then echo "$DIFF" | less -r -k $HOME/.lsh_less_keys_tmp else @@ -217,18 +228,19 @@ function print_diff() { fi } -# Calculate OFFSET to avoid bad redraw. (Really bad performance.) +# Calculate OFFSET to avoid bad redraw. 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[@]}" + tmp=1 + index=$(( SI -1 )) + while [[ $tmp -lt $SN ]] 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 + el=${COMMITS_UNCOL[$index]} + if [[ ${#el} -gt $CN ]] && [[ $CP -lt $((SN -1)) ]]; then + OFFSET_2=$(( OFFSET_2 + 1 )) + tmp=$(( tmp + 1 )) + fi + tmp=$(( tmp + 1 )) + index=$(( index + 1 )) done } @@ -238,20 +250,27 @@ tput civis # hide cursor. CP=1 # current position SI=1 # index END=false # end while loop +EXT=0 # Used to extend the number of lines to display. ## Loops, reads inputs and prints commits until user presses 'q' to exit or TAB to show the diff. while ! $END do + + END_INDEX=0 # Index for the last item to display + # When the number of item is higher than screen's number of lines, OFFSET_2 is recalculated each time we select a new item # Set last index to print. (based on OFFSET) if [[ $TN == $NI ]]; then END_INDEX=$TN + OFFSET_2=$OFFSET elif [[ $TN == $(( SN - 1 )) ]]; then # Calculate new OFFSET. if [[ $OFFSET != 0 ]]; then - calculate_offset #This involve really bad performance. Should be reconsidered. + [[ $CP -lt $((SN -1)) ]] && OFFSET_2=0 + EXT=1 + calculate_offset fi - END_INDEX=$(( TN + SI -1 - OFFSET )) + END_INDEX=$(( TN + SI -1 + EXT - OFFSET_2 )) fi # Loop and echo commits @@ -273,29 +292,30 @@ do "$au" | "$au_1") CP=$(( CP - 1 )) - [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$(( NI - OFFSET )) && SI=$(( NI - SN + 2 )) + [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $(( SN - 1 )) ]] && CP=$NI && SI=$(( NI - SN + 2 + OFFSET_2 )) [[ $CP == 0 ]] && [[ $SI=1 ]] && [[ $TN == $NI ]] && CP=$TN [[ $CP == $(( SI - 1 )) ]] && [[ $SI != 1 ]] && SI=$(( SI - 1 )) - [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET )) - [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( SN - 1 )) + [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 )) + [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT )) [[ $SI != 1 ]] && tput ed # clear screen ;; "$ad" | "$ad_1") CP=$(( CP + 1 )) [[ $CP == $(( NI + 1 )) ]] && CP=1 && SI=1 - [[ $CP == $(( SN + SI - 1 - OFFSET )) ]] && [[ $TN == $(( SN - 1 )) ]] && SI=$(( SI + 1 )) + [[ $CP == $(( SN + SI - 1 + EXT - OFFSET_2 )) ]] && [[ $TN == $(( SN - 1 )) ]] && SI=$(( SI + 1 )) - [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET )) - [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( SN - 1 )) + [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 )) + [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT )) [[ $SI != 1 ]] && tput ed # clear screen [[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen ;; "$nl") - tput cuu $(( TN + OFFSET )) - print_diff + [[ $TN == $NI ]] && tput cuu $(( TN + OFFSET_2 )) + [[ $TN != $NI ]] && tput cuu $(( TN + EXT )) + print_diff ;; "q") @@ -305,7 +325,7 @@ do ;; * ) - tput cuu $(( TN + OFFSET )) + tput cuu $(( TN + OFFSET_2 )) esac done From 3eb94792525d675e9239ffdbfde4a9a543f1fa6c Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sun, 12 Feb 2017 22:58:40 +0100 Subject: [PATCH 7/8] Clear screen after less --- git-recall | 1 + 1 file changed, 1 insertion(+) diff --git a/git-recall b/git-recall index 4e188fb..db32189 100644 --- a/git-recall +++ b/git-recall @@ -196,6 +196,7 @@ function print_diff() { else echo "$DIFF" | less -r fi + tput ed # Clear screen else stop=false tput ed From 391bc9b420c19f138baf8e29d5ee85356ab39eb4 Mon Sep 17 00:00:00 2001 From: Fakerr Date: Sun, 12 Feb 2017 22:59:39 +0100 Subject: [PATCH 8/8] Update version --- git-recall | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-recall b/git-recall index db32189..1191c7f 100644 --- a/git-recall +++ b/git-recall @@ -25,7 +25,7 @@ COMMITS="" COMMITS_UNCOL=() # commits without colors LESSKEY=false SED_BIN="" # Sed option to use according OS. -VERSION="1.1.4" +VERSION="1.1.5" # Are we in a git repo? if [[ ! -d ".git" ]] && ! git rev-parse --git-dir &>/dev/null; then