-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.sh
executable file
·90 lines (80 loc) · 1.61 KB
/
git.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
. "$HOME/.config/git-prompt.sh"
# shellcheck disable=SC2016
PROMPT='\e[38;5;214m\]$(__git_ps1)$([[ -z $(git status --porcelain 2>/dev/null) ]] || echo "*")\[\033[00m\] git > '
hist_ptr=-1
mapfile -t _history < <(sed -nre 's/^git (.*)$/\1/p' "$HISTFILE" | uniq)
function _reverse_search {
_overwrite "$(
fzf --bind=ctrl-z:ignore < <(
IFS=$'\n'
echo "${_history[*]}" | tac
)
)"
}
function _up {
((hist_ptr++))
_overwrite "${_history[((${#_history[@]} - $hist_ptr - 1))]}"
}
function _down {
((hist_ptr--))
if ((hist_ptr < -1)); then
hist_ptr=-1
_overwrite ''
else
_overwrite "${_history[((${#_history[@]} - $hist_ptr - 1))]}"
fi
}
function _overwrite {
line=$1
printf "\33[2K\r${PROMPT@P}%s" "$line"
}
function _eval {
[[ -z $line ]] && echo && return
cmd="git $line"
if [[ ${_history[-1]} != "$line" ]]; then
_history+=("$line")
# echo "$cmd" > "$HISTFILE"
fi
printf "\n"
eval "$cmd"
hist_ptr=-1
}
while true; do
printf "%s" "${PROMPT@P}"
line=''
while IFS= read -rsN 1 ch; do
# printf "%s\n" "${ch@Q}"
case "$ch" in
$'\04') # ^D
EOF=1
break
;;
$'\022') # ^R
_reverse_search
;;
$'\177') # Backspace
printf "\b \b"
((${#line} > 0)) && line=${line::-1}
;;
$'\E')
read -rsN2 code
case $code in
'[A') _up ;;
'[B') _down ;;
# '[C') echo RIGHT ;;
# '[D') echo LEFT ;;
esac
;;
$'\n') break ;;
*)
printf "%s" "$ch"
line="$line$ch"
;;
esac
done
_eval
if ((EOF)); then
break
fi
done