-
Notifications
You must be signed in to change notification settings - Fork 18
/
hint_mode.sh
executable file
·195 lines (146 loc) · 4.33 KB
/
hint_mode.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
current_pane_id=$1
picker_pane_id=$2
last_pane_id=$3
picker_window_id=$4
pane_input_temp=$5
eval "$(tmux show-env -g -s | grep ^PICKER)"
match_lookup_table=$(mktemp)
# exporting it so they can be properly deleted inside handle_exit trap
export match_lookup_table
function lookup_match() {
local input=$1
input="$(echo "$input" | tr "A-Z" "a-z")"
grep -i "^$input:" $match_lookup_table | sed "s/^$input://" | head -n 1
}
function get_pane_contents() {
cat $pane_input_temp
}
function extract_hints() {
clear
export NUM_HINTS_NEEDED=$(get_pane_contents | gawk -f $CURRENT_DIR/counter.awk)
get_pane_contents | gawk -f $CURRENT_DIR/hinter.awk 3> $match_lookup_table
}
function show_hints_again() {
local picker_pane_id=$1
tmux swap-pane -s "$current_pane_id" -t "$picker_pane_id" # hide screen clearing glitch
extract_hints
tmux swap-pane -s "$current_pane_id" -t "$picker_pane_id"
}
function show_hints_and_swap() {
current_pane_id=$1
picker_pane_id=$2
extract_hints
tmux swap-pane -s "$current_pane_id" -t "$picker_pane_id"
}
BACKSPACE=$'\177'
input=''
result=''
function is_pane_zoomed() {
local pane_id=$1
tmux list-panes \
-F "#{pane_id}:#{?pane_active,active,nope}:#{?window_zoomed_flag,zoomed,nope}" \
| grep -c "^${pane_id}:active:zoomed$"
}
function zoom_pane() {
local pane_id=$1
tmux resize-pane -Z -t "$pane_id"
}
function revert_to_original_pane() {
tmux swap-pane -s "$current_pane_id" -t "$picker_pane_id"
if [[ ! -z "$last_pane_id" ]]; then
tmux select-pane -t "$last_pane_id"
tmux select-pane -t "$current_pane_id"
fi
[[ $pane_was_zoomed == "1" ]] && zoom_pane "$current_pane_id"
}
function handle_exit() {
rm -rf "$pane_input_temp" "$match_lookup_table"
revert_to_original_pane
if [[ ! -z "$result" ]]; then
run_picker_copy_command "$result" "$input"
fi
tmux kill-window -t "$picker_window_id"
}
function is_valid_input() {
local input=$1
local is_valid=1
if [[ $input == "" ]] || [[ $input == "<ESC>" ]]; then
is_valid=1
else
for (( i=0; i<${#input}; i++ )); do
char=${input:$i:1}
if [[ ! "$char" =~ ^[a-zA-Z]$ ]]; then
is_valid=0
break
fi
done
fi
echo $is_valid
}
function hide_cursor() {
echo -n $(tput civis)
}
trap "handle_exit" EXIT
export PICKER_PATTERNS=$PICKER_PATTERNS1
export PICKER_BLACKLIST_PATTERNS=$PICKER_BLACKLIST_PATTERNS
pane_was_zoomed=$(is_pane_zoomed "$current_pane_id")
show_hints_and_swap $current_pane_id $picker_pane_id
[[ $pane_was_zoomed == "1" ]] && zoom_pane "$picker_pane_id"
hide_cursor
input=''
function run_picker_copy_command() {
local result="$1"
local hint="$2"
is_uppercase=$(echo "$input" | grep -E '^[a-z]+$' &> /dev/null; echo $?)
if [[ $is_uppercase == "1" ]] && [ ! -z "$PICKER_COPY_COMMAND_UPPERCASE" ]; then
command_to_run="$PICKER_COPY_COMMAND_UPPERCASE"
elif [ ! -z "$PICKER_COPY_COMMAND" ]; then
command_to_run="$PICKER_COPY_COMMAND"
fi
if [[ ! -z "$command_to_run" ]]; then
tmux run-shell -b "printf '$result' | $command_to_run"
fi
}
while read -rsn1 char; do
if [[ $char == "$BACKSPACE" ]]; then
input=""
fi
# Escape sequence, flush input
if [[ "$char" == $'\x1b' ]]; then
read -rsn1 -t 0.1 next_char
if [[ "$next_char" == "[" ]]; then
read -rsn1 -t 0.1
continue
elif [[ "$next_char" == "" ]]; then
char="<ESC>"
else
continue
fi
fi
if [[ ! $(is_valid_input "$char") == "1" ]]; then
continue
fi
if [[ $char == "$BACKSPACE" ]]; then
input=""
continue
elif [[ $char == "<ESC>" ]]; then
exit
elif [[ $char == "" ]]; then
if [ "$PICKER_PATTERNS" = "$PICKER_PATTERNS1" ]; then
export PICKER_PATTERNS=$PICKER_PATTERNS2;
else
export PICKER_PATTERNS=$PICKER_PATTERNS1;
fi
show_hints_again "$picker_pane_id"
continue
else
input="$input$char"
fi
result=$(lookup_match "$input")
if [[ -z $result ]]; then
continue
fi
exit 0
done < /dev/tty