Merge pull request #32 from Fakerr/mac-fix

fix mac os issues
This commit is contained in:
Walid Berrahal 2017-06-11 19:45:50 +01:00 committed by GitHub
commit 103fb21cfa
2 changed files with 29 additions and 21 deletions

View file

@ -86,6 +86,7 @@ $ sudo make install
``` ```
## Requirements ## Requirements
- OS: Linux or OSX - OS: Linux or OSX
- Bash 4.3 or more
- Tools: git, less, sed - Tools: git, less, sed
##### Optional Requirements ##### Optional Requirements

View file

@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# usage info # usage info
usage() { usage() {
cat <<EOF cat <<EOF
@ -23,9 +24,9 @@ GIT_LOG=""
COMMITS="" COMMITS=""
SINCE="1 days ago" # show logs for last day by default SINCE="1 days ago" # show logs for last day by default
COMMITS_UNCOL=() # commits without colors COMMITS_UNCOL=() # commits without colors
SED_BIN="" # Sed option to use according OS. SED_CMD="" # Sed command to use according OS.
LESSKEY=false LESSKEY=false
VERSION="1.1.12" VERSION="1.2.0"
# Set key bindings. # Set key bindings.
au="`echo -e '\e[A'`" # arrow up au="`echo -e '\e[A'`" # arrow up
@ -37,7 +38,7 @@ ad_1="j" # arrow down
nl_1="e" # expand nl_1="e" # expand
# Colored output. # Colored output.
colored() { function colored() {
GREEN=$(tput setaf 4) GREEN=$(tput setaf 4)
YELLOW=$(tput setaf 3) YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr0) NORMAL=$(tput sgr0)
@ -45,13 +46,19 @@ colored() {
} }
# Uncolored output. # Uncolored output.
uncolored() { function uncolored() {
GREEN="" GREEN=""
YELLOW="" YELLOW=""
NORMAL="" NORMAL=""
REVERSE="" REVERSE=""
} }
# Clear screen from cursor to end of screen
function clear_screen() {
echo -e "\E[J"
tput cuu1
}
# Are we in a git repo? # Are we in a git repo?
if [[ ! -d ".git" ]] && ! git rev-parse --git-dir &>/dev/null; then if [[ ! -d ".git" ]] && ! git rev-parse --git-dir &>/dev/null; then
echo "abort: not a git repository." 1>&2 echo "abort: not a git repository." 1>&2
@ -100,11 +107,6 @@ else
uncolored uncolored
fi fi
# Check if lesskey is installed.
if command -v lesskey &> /dev/null; then
LESSKEY=true
fi
# Set AUTHOR to current user if no param passed or display for all users if param equal to "all". # Set AUTHOR to current user if no param passed or display for all users if param equal to "all".
if [[ ! -n $AUTHOR ]]; then if [[ ! -n $AUTHOR ]]; then
AUTHOR=$(git config user.name 2>/dev/null) AUTHOR=$(git config user.name 2>/dev/null)
@ -117,7 +119,7 @@ if [[ $FETCH == true ]]; then
echo "${GREEN}Fetching changes...${NORMAL}" echo "${GREEN}Fetching changes...${NORMAL}"
git fetch --all &> /dev/null git fetch --all &> /dev/null
tput cuu1 tput cuu1
tput ed # clear screen clear_screen
fi fi
# Log template. # Log template.
@ -148,16 +150,21 @@ if [[ $NI = 0 ]]; then
fi fi
fi fi
# Set correct sed option according OS's type # Check if lesskey is installed.
if command -v lesskey &> /dev/null; then
LESSKEY=true
fi
# Set correct sed command according OS's type
case "$OSTYPE" in case "$OSTYPE" in
darwin*) SED_BIN="sed -E" ;; darwin*) SED_CMD="sed -E s/"$'\E'"\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g" ;;
*) SED_BIN="sed -r" ;; *) SED_CMD="sed -r s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" ;;
esac esac
# Create array with uncolred commits (removing escape sequences using sed) # Create array with uncolred commits (removing escape sequences using sed)
for elt in "${COMMITS[@]}" for elt in "${COMMITS[@]}"
do do
ELT="$(echo "$elt" | $SED_BIN "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" # remove colors escape codes ELT="$(echo "$elt" | $SED_CMD)" # remove colors escape codes
COMMITS_UNCOL+=("$ELT") COMMITS_UNCOL+=("$ELT")
done done
@ -180,7 +187,7 @@ function get_diff() {
DIFF_TIP=${ELT:0:7} DIFF_TIP=${ELT:0:7}
DIFF_CMD="git show $DIFF_TIP --color=always" DIFF_CMD="git show $DIFF_TIP --color=always"
DIFF=$(eval ${DIFF_CMD} 2>/dev/null) DIFF=$(eval ${DIFF_CMD} 2>/dev/null)
tmp_diff="$(echo "$DIFF" | $SED_BIN "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")" tmp_diff="$(echo "$DIFF" | $SED_CMD)"
off=$(echo "$tmp_diff" | grep -c ".\{$CN\}") # Number of lines in the diff that are longer than terminal width. off=$(echo "$tmp_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="$(echo "$DIFF" | wc -l)"
DIFF_LINES_NUMBER=$(( DIFF_LINES_NUMBER + off )) DIFF_LINES_NUMBER=$(( DIFF_LINES_NUMBER + off ))
@ -195,10 +202,10 @@ function print_diff() {
else else
echo "$DIFF" | less -r echo "$DIFF" | less -r
fi fi
tput ed # Clear screen clear_screen
else else
stop=false stop=false
tput ed clear_screen
for i in `seq 1 $TN` for i in `seq 1 $TN`
do do
echo -n "$NORMAL" echo -n "$NORMAL"
@ -220,7 +227,7 @@ function print_diff() {
;; ;;
esac esac
done done
[[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && tput ed [[ $END = false ]] && tput cuu $(( TN + DIFF_LINES_NUMBER + OFFSET )) && clear_screen
[[ $END = true ]] && tput cuu 1 [[ $END = true ]] && tput cuu 1
fi fi
} }
@ -291,7 +298,7 @@ do
[[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 )) [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 ))
[[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT )) [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT ))
[[ $SI != 1 ]] && tput ed # clear screen [[ $SI != 1 ]] && clear_screen
;; ;;
"$ad" | "$ad_1") "$ad" | "$ad_1")
CP=$(( CP + 1 )) CP=$(( CP + 1 ))
@ -300,8 +307,8 @@ do
[[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 )) [[ $TN != $(( SN - 1 )) ]] && tput cuu $(( TN + OFFSET_2 ))
[[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT )) [[ $TN == $(( SN - 1 )) ]] && tput cuu $(( TN + EXT ))
[[ $SI != 1 ]] && tput ed # clear screen [[ $SI != 1 ]] && clear_screen
[[ $SI = 1 ]] && [[ $CP = 1 ]] && tput ed # clear screen [[ $SI = 1 ]] && [[ $CP = 1 ]] && clear_screen
;; ;;
"$nl" | "$nl_1") "$nl" | "$nl_1")
[[ $TN == $NI ]] && tput cuu $(( TN + OFFSET_2 )) [[ $TN == $NI ]] && tput cuu $(( TN + OFFSET_2 ))