File tree 1 file changed +43
-3
lines changed
1 file changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -8,10 +8,50 @@ ctrl-branch-command() {
8
8
# split command by newline into array using f parameter expansion
9
9
# https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion-Flags
10
10
MENU=( ${(f)CMD} )
11
- # traverse array and print line by line
12
- for v in $MENU ; do
13
- print $v
11
+
12
+ local BRANCHES=()
13
+ local HOTKEYS=()
14
+ # traverse non-colored lines, detect branch and hotkey
15
+ CMD_NOCOLOR=$( print $CMD | sed -e ' s/\x1b\[[0-9;]*m//g' )
16
+ NCLINES=( ${(f)CMD_NOCOLOR} )
17
+ i=1
18
+ for v in $NCLINES ; do
19
+ # # get branch name
20
+ # strip the column with current branch marker
21
+ branch=${v[3,-1]}
22
+ # cut after space
23
+ branch=${branch%% * }
24
+ BRANCHES+=($branch )
25
+
26
+ # # get shortcut
27
+ # XXX get symbol shortcut instead of numeric
28
+ # XXX empty labels when exhausted
29
+ # XXX [m] always main
30
+ HOTKEYS+=($i )
31
+ (( i++ ))
32
+ done
33
+
34
+ i=1
35
+ for m in $MENU ; do
36
+ print $HOTKEYS [$i ] $m
37
+ (( i++ ))
14
38
done
39
+
40
+ # read $keypress
41
+ # https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html#index-read
42
+ read -s -k keypress
43
+
44
+ IDX=$HOTKEYS [(I)$keypress ]
45
+ if [[ $IDX == 0 ]]; then
46
+ # printf "%q\n" $keypress
47
+ if [[ $keypress != $' \n ' && $keypress != $' \033 ' ]]; then
48
+ # print "Key: $IDX #keypress"
49
+ printf " Undefined key: %q\n" $keypress
50
+ fi
51
+ else
52
+ BRANCH=$BRANCHES [$IDX ]
53
+ git switch $BRANCH
54
+ fi
15
55
}
16
56
17
57
# bind ctrl-b to type string
You can’t perform that action at this time.
0 commit comments