-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfzf-histdb.zsh
executable file
·318 lines (286 loc) · 10.4 KB
/
fzf-histdb.zsh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
FZF_HISTDB_FILE="${(%):-%N}"
HISTDB_FZF_CMD=${HISTDB_FZF_COMMAND:-fzf}
# use gdate if available (will provide nanoseconds on mac)
if command -v gdate >> /dev/null; then
datecmd='gdate'
else
datecmd='date'
fi
get_date_format() (
local date_format
date_format="$(awk '{ print tolower($1) }' <<< "${HISTDB_FZF_FORCE_DATE_FORMAT}")"
if [[ "${date_format}" != "us" && "${date_format}" != "non-us" ]]; then
eval "$(locale)"
local lc_time_lang="$(awk -F'.' '{ print tolower($1) }' <<< "${LC_TIME}")"
if [[ "${lc_time_lang}" == "en_us" || "${lc_time_lang}" == "c" ]]; then
date_format="us"
else
date_format="non-us"
fi
fi
if [[ "${date_format}" == "us" ]]; then
echo "%m/%d"
else
echo "%d/%m"
fi
)
# variables for substitution in log
NL="
"
NLT=$(printf "\n\t\t")
autoload -U colors && colors
histdb-fzf-log() {
if [[ ! -z ${HISTDB_FZF_LOGFILE} ]]; then
if [[ ! -f ${HISTDB_FZF_LOGFILE} ]]; then
touch ${HISTDB_FZF_LOGFILE}
fi
printf "%s %s\n" $(${datecmd} +'%s.%N') ${*//$NL/$NLT} >> ${HISTDB_FZF_LOGFILE}
fi
}
histdb-fzf-query(){
# A wrapper for histb-query with fzf specific options and query
_histdb_init
local -a opts
zparseopts -E -D -a opts \
s d t a
local where=""
local everywhere=0
local cols="history.id as id, commands.argv as argv, max(start_time) as max_start, exit_status"
local groupby="group by history.command_id, history.place_id"
local date_format="$(get_date_format)"
local mst="datetime(max_start, 'unixepoch')"
local dst="datetime('now', 'start of day')"
local yst="datetime('now', 'start of year')"
local timecol="strftime(
case when $mst > $dst then
'%H:%M'
else (
case when $mst > $yst then
'${date_format}'
else
'${date_format}/%Y'
end)
end,
max_start,
'unixepoch',
'localtime') as time"
for opt ($opts); do
case $opt in
-s)
where="${where:+$where and} session in (${HISTDB_SESSION})"
;;
-d)
where="${where:+$where and} (places.dir like '$(sql_escape $PWD)%')"
;;
-t)
everywhere=1
;;
-a)
histdb-fzf-log "Grouping disabled"
cols="history.id as id, commands.argv as argv, start_time as max_start, exit_status"
timecol="strftime( '${date_format} %H:%M', max_start, 'unixepoch', 'localtime') as time"
groupby=""
;;
esac
done
if [[ $everywhere -eq 0 ]];then
where="${where:+$where and} places.host=${HISTDB_HOST}"
fi
local query="
select
id,
${timecol},
CASE exit_status WHEN 0 THEN '' ELSE '${fg[red]}' END || replace(argv, '$NL', ' ') as cmd,
CASE exit_status WHEN 0 THEN '' ELSE '${reset_color}' END
from
( select
${cols}
from
history
left join
commands on history.command_id = commands.id
left join
places on history.place_id = places.id
${where:+where ${where}}
${groupby}
order
by max_start desc
)
order by max_start desc"
histdb-fzf-log "query for log '${(Q)query}'"
# use Figure Space U+2007 as separator
_histdb_query -separator ' ' "$query"
histdb-fzf-log "query completed"
}
histdb-detail(){
HISTDB_FILE=$1
local where="(history.id == '$(sed -e "s/'/''/g" <<< "$2" | tr -d '\000')')"
local date_format="$(get_date_format)"
local cols="
history.id as id,
commands.argv as argv,
max(start_time) as max_start,
exit_status,
duration as secs,
count() as runcount,
history.session as session,
places.host as host,
places.dir as dir"
local query="
select
strftime('${date_format}/%Y %H:%M', max_start, 'unixepoch', 'localtime') as time,
ifnull(exit_status, 'NONE') as exit_status,
ifnull(secs, '-----') as secs,
ifnull(host, '<somewhere>') as host,
ifnull(dir, '<somedir>') as dir,
session,
id,
argv as cmd
from
(select ${cols}
from
history
left join commands on history.command_id = commands.id
left join places on history.place_id = places.id
where ${where})
"
array_str=("${$(sqlite3 -cmd ".timeout 1000" "${HISTDB_FILE}" -separator " " "$query" )}")
array=(${(@s: :)array_str})
histdb-fzf-log "DETAIL: ${array_str}"
# Add some color
if [[ "${array[2]}" == "NONE" ]];then
#Color exitcode magento if not available
array[2]=$(echo "\033[35m${array[2]}\033[0m")
elif [[ ! ${array[2]} ]];then
#Color exitcode red if not 0
array[2]=$(echo "\033[31m${array[2]}\033[0m")
fi
if [[ "${array[3]}" == "-----" ]];then
#Color duration magento if not available
array[3]=$(echo "\033[35m${array[3]}\033[0m")
elif [[ "${array[3]}" -gt 300 ]];then
# Duration red if > 5 min
array[3]=$(echo "\033[31m${array[3]}\033[0m")
elif [[ "${array[3]}" -gt 60 ]];then
# Duration yellow if > 1 min
array[3]=$(echo "\033[33m${array[3]}\033[0m")
fi
printf "\033[1mLast run\033[0m\n\nTime: %s\nStatus: %s\nDuration: %s sec.\nHost: %s\nDirectory: %s\nSessionid: %s\nCommand id: %s\nCommand:\n\n" ${array[0]} ${array[1]} ${array[2]} ${array[3]} ${array[4]} ${array[5]} ${array[6]} ${array[7]}
echo "${array[8,-1]}"
}
histdb-get-command(){
HISTDB_FILE=$1
CMD_ID=$2
local query="
select
argv as cmd
from
history
left join commands on history.command_id = commands.id
where
history.id='${CMD_ID}'
"
printf "%s" "$(sqlite3 -cmd ".timeout 1000" "${HISTDB_FILE}" "$query")"
}
histdb-fzf-widget() {
local selected num mode exitkey typ cmd_opts cmd_opts_extra
ORIG_FZF_DEFAULT_OPTS=$FZF_DEFAULT_OPTS
query=${BUFFER}
origquery=${BUFFER}
histdb-fzf-log "================== START ==================="
histdb-fzf-log "original buffers: -:$BUFFER l:$LBUFFER r:$RBUFFER"
histdb-fzf-log "original query $query"
histdb_fzf_modes=('session' 'loc' 'global' 'everywhere')
if [[ -n ${HISTDB_FZF_DEFAULT_MODE} ]]; then
mode=${HISTDB_FZF_DEFAULT_MODE}
elif [[ -z ${HISTDB_SESSION} ]];then
mode=2
else
mode=1
fi
histdb-fzf-log "Start mode ${histdb_fzf_modes[$mode]} ($mode)"
exitkey='ctrl-r'
cmd_opts_extra=''
setopt localoptions noglobsubst noposixbuiltins pipefail 2> /dev/null
# Here it is getting a bit tricky, fzf does not support dynamic updating so we have to close and reopen fzf when changing the focus (session, dir, global)
# so we check the exitkey and decide what to do
while [[ "$exitkey" != "" && "$exitkey" != "esc" ]]; do
histdb-fzf-log "------------------- TURN -------------------"
histdb-fzf-log "Exitkey $exitkey"
# the f keys are a shortcut to select a certain mode
if [[ $exitkey == "f5" ]]; then
mode=$((($mode - 1) % $#histdb_fzf_modes))
if [[ $cmd_opts_extra == '' ]]; then
cmd_opts_extra='-a'
else
cmd_opts_extra=''
fi
elif [[ $exitkey =~ "f." ]]; then
mode=${exitkey[$(($MBEGIN+1)),$MEND]}
histdb-fzf-log "mode changed to ${histdb_fzf_modes[$mode]} ($mode)"
fi
histdb-fzf-log "Extra Opts $cmd_opts_extra"
# based on the mode, we use the options for histdb options
case "$histdb_fzf_modes[$mode]" in
'session')
cmd_opts="-s"
typ="Session local history ${fg[blue]}${HISTDB_SESSION}${reset_color}"
switchhints="${fg[blue]}F1: session${reset_color} ${bold_color}F2: directory${reset_color} ${bold_color}F3: global${reset_color} ${bold_color}F4: everywhere${reset_color} -- F5: toggle grouping"
;;
'loc')
cmd_opts="-d"
typ="Directory local history ${fg[blue]}$(pwd)${reset_color}"
switchhints="${bold_color}F1: session${reset_color} ${fg[blue]}F2: directory${reset_color} ${bold_color}F3: global${reset_color} ${bold_color}F4: everywhere${reset_color} -- F5: toggle grouping"
;;
'global')
cmd_opts=""
typ="global history ${fg[blue]}$(hostname)${reset_color}"
switchhints="${bold_color}F1: session${reset_color} ${bold_color}F2: directory${reset_color} ${fg[blue]}F3: global${reset_color} ${bold_color}F4: everywhere${reset_color} -- F5: toggle grouping"
;;
'everywhere')
cmd_opts="-t"
typ='everywhere'
switchhints="${bold_color}F1: session${reset_color} ${bold_color}F2: directory${reset_color} ${bold_color}F3: global${reset_color} ${fg[blue]}F4: everywhere${reset_color} -- F5: toggle grouping"
;;
esac
mode=$(((($mode + 1) % $#histdb_fzf_modes)))
histdb-fzf-log "mode changed to ${histdb_fzf_modes[$mode]} ($mode)"
# log the FZF arguments
OPTIONS="$ORIG_FZF_DEFAULT_OPTS
--ansi
--header='${typ}${NL}${switchhints}${NL}―――――――――――――――――――――――――' --delimiter=' '
-n2.. --with-nth=2..
--tiebreak=index --expect='esc,ctrl-r,f1,f2,f3,f4,f5'
--bind 'ctrl-d:page-down,ctrl-u:page-up'
--print-query
--preview='source ${FZF_HISTDB_FILE}; histdb-detail ${HISTDB_FILE} {1}' --preview-window=right:50%:wrap
--no-hscroll
--query='${query}' +m"
histdb-fzf-log "$OPTIONS"
result=( "${(@f)$( histdb-fzf-query ${cmd_opts} ${cmd_opts_extra} |
FZF_DEFAULT_OPTS="${OPTIONS}" ${HISTDB_FZF_CMD})}" )
# here we got a result from fzf, containing all the information, now we must handle it, split it and use the correct elements
histdb-fzf-log "returncode was $?"
query=$result[1]
exitkey=${result[2]}
fzf_selected="${(@s: :)result[3]}"
fzf_selected="${${(@s: :)result[3]}[1]}"
histdb-fzf-log "Query was ${query:-<nothing>}"
histdb-fzf-log "Exitkey was ${exitkey:-<NONE>}"
histdb-fzf-log "fzf_selected = $fzf_selected"
done
if [[ "$exitkey" == "esc" ]]; then
BUFFER=$origquery
else
histdb-fzf-log "histdb-get-command ${HISTDB_FILE} ${fzf_selected}"
selected=$(histdb-get-command ${HISTDB_FILE} ${fzf_selected})
histdb-fzf-log "selected = $selected"
BUFFER=$selected
fi
CURSOR=$#BUFFER
zle redisplay
histdb-fzf-log "new buffers: -:$BUFFER l:$LBUFFER r:$RBUFFER"
histdb-fzf-log "=================== DONE ==================="
}
zle -N histdb-fzf-widget
bindkey '^R' histdb-fzf-widget