-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbhist.sh
More file actions
273 lines (253 loc) · 7.17 KB
/
dbhist.sh
File metadata and controls
273 lines (253 loc) · 7.17 KB
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
# The MIT License
# SPDX short identifier: MIT
# Further resources on the MIT License
# Copyright 2017 Denis Gladkikh (https://www.outcoldman.com/en/archive/2017/07/19/dbhist/)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
################################################################################
# To use it, just source the file `source dbhist.sh`
# Verified that works with Bash 3/4
# Configuration
# DBHISTORY=true - use false to disable
# DBHISTORYFILE=$HOME/.dbhist - location of dbhist file
#
# To make it work I keep next configuration for bash, the only important is the
# time format, to make it unixtime
#
# HISTSIZE=$HOME/.bash_history
# HISTFILESIZE=1000
# HISTSIZE=1000
# HISTTIMEFORMAT="%s "
# HISTCONTROL=ignorespace:erasedups
#
# More details in https://www.outcoldman.com/en/archive/2017/07/19/dbhist/
# Or use after you sourced this file
# dbhist --help
################################################################################
if ${DBHISTORY:-true}; then
# Kind of session ID, allows to see only commands executed in this terminal session
__dbhist_salt="${RANDOM}"
# This allows us to verify existence of .dbhist file only once
__dbhist_initialized=false
# Keep the previous folder, this how we keep the PWD for commands like `cd ...`
__dbhist_oldpwd="${OLDPWD}"
# Get location of .dbhist file, default location is under `$HOME/.dbhist`
__dbhist_file() {
echo "${DBHISTORYFILE:-$HOME/.dbhist}"
}
# Execute command on sqlite with .dbhist file
__dbhist_sqlite() {
sqlite3 "$(__dbhist_file)"
}
# Initialize dbhist file, create table
__db_hist_init() {
if [[ ! -f "$(__dbhist_file)" ]]; then
local __sql='CREATE TABLE history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
hist_id INTEGER,
cmd TEXT,
epoch INTEGER,
ppid INTEGER,
pwd TEXT,
salt INTEGER
);'
echo "${__sql}" | __dbhist_sqlite
fi
}
# Execute it with bash PROMPT
__db_hist_prompt() {
if ! ${__dbhist_initialized}; then
__db_hist_init
__dbhist_initialized=true
return
fi
if [[ -n "${COMP_LINE}" ]]; then
return
fi
local __formated_history=
local __num=
local __epoch=
local __pwd="${PWD}"
if [[ "${OLDPWD}" != "${__dbhist_oldpwd}" ]]; then
__pwd="${OLDPWD}"
__dbhist_oldpwd="${OLDPWD}"
fi
# 521 1492964870 history 1
__formated_history="$(history 1 | sed -E 's/[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]](.+)/\1@\2@\3/')"
__num="$(echo "${__formated_history}" | sed -E 's/([^@]+)@([^@]+)@(.+)/\1/')"
__epoch="$(echo "${__formated_history}" | sed -E 's/([^@]+)@([^@]+)@(.+)/\2/')"
__command="$(echo "${__formated_history}" | sed -E 's/([^@]+)@([^@]+)@(.+)/\3/')"
__dbhist_sqlite <<-END
INSERT INTO history(hist_id, epoch, cmd, ppid, pwd, salt)
VALUES("${__num}", "${__epoch}", "${__command//\"/""}", ${PPID}, "${__pwd//\"/""}", ${__dbhist_salt});
END
}
# Use
# dbhist --help
# To learn the tool
dbhist() {
local __limit=100
local __all=false
local __self=false
local __starts=false
local __verbose=false
local __query=''
local __location=''
local __pwd=false
while ! [[ $# -eq 0 || -z "${1}" ]]; do
case "${1}" in
(--shell)
__dbhist_sqlite
return
;;
(--help)
cat<<END
Usage:
dbhist [options] [query]
Options:
--limit {number}
number of latest commands to show. Defaults to 100.
--all
include history of all sessions. Not only current.
--self
include history about dbhist
--starts
match only commands starts with query
--verbose
verbose output (shows sql)
--here
reduce search only for current pwd
--under
reduce search only for current and under pwd
--pwd
show working directory of cmd
--shell
access to shell
END
return
;;
(--limit)
shift
__limit="${1:?option require value}"
if ! [[ "${__limit}" =~ ^[0-9]+$ ]]; then
2>&1 echo "Limit should be a number"
return 1
fi
;;
(--all)
__all=true
;;
(--self)
__self=true
;;
(--starts)
__starts=true
;;
(--verbose)
__verbose=true
;;
(--here)
if [[ -n "${__location}" ]]; then
2>&1 echo "Specifier for pwd already set"
return 1
fi
__location="here"
;;
(--under)
if [[ -n "${__location}" ]]; then
2>&1 echo "Specifier for pwd already set"
return 1
fi
__location="under"
;;
(--pwd)
__pwd=true
;;
(-*)
2>&1 echo "Unknown option ${1}"
return 1
;;
(*)
if [[ -n "${__query}" ]]; then
2>&1 echo "Cannot specify more than one query"
return 1
fi
__query="${1:?requires value}"
;;
esac
shift
done
__pwd_q=''
if ${__pwd}; then
__pwd_q='pwd || " > " ||'
fi
local __sql="
SELECT
substr(' '||max(id),-6) ||
' | ' ||
datetime(max(epoch), 'unixepoch', 'localtime') ||
' | ' ||
substr(' '||count(*),-6) ||
' | ' ||
${__pwd_q}
cmd
FROM history
WHERE 1 "
if ! ${__all}; then
__sql+="AND (salt=${__dbhist_salt} AND ppid=${PPID}) "
fi
if ! ${__self}; then
__sql+="AND (cmd != 'dbhist' AND cmd NOT LIKE 'dbhist %') "
fi
if [[ -n "${__query}" ]]; then
__query=${__query//\"/""}
if ! ${__starts} && [[ "${__query}" != %* ]]; then
__query="%${__query}"
fi
if [[ "${__query}" != *% ]]; then
__query+="%"
fi
__sql+="AND cmd LIKE \"${__query}\" ESCAPE '\' "
fi
__pwd_l="${PWD//\"/""}"
case "${__location}" in
(here)
__sql+="AND (pwd == \"${__pwd_l}\") "
;;
(under)
__pwd_l="${__pwd_l//%/\%}"
__pwd_l="${__pwd_l//_/\_}"
__sql+="AND (pwd LIKE \"${__pwd_l}%\" ESCAPE '\') "
;;
esac
__sql+="GROUP BY cmd "
if ${__pwd}; then
__sql+=", pwd "
fi
__sql+="ORDER BY max(id) DESC LIMIT ${__limit} ;"
if ${__verbose}; then
echo "database:"
__dbhist_file
echo "sql:"
echo "${__sql}"
fi
echo "${__sql}" | __dbhist_sqlite
}
# Inject __db_hist_prompt in `PROMPT_COMMAND`
if ! [[ "${PROMPT_COMMAND}" =~ .*__db_hist_prompt.* ]]; then
PROMPT_COMMAND="__db_hist_prompt${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi
fi