-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhist
executable file
·243 lines (215 loc) · 5.88 KB
/
hist
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
#!/usr/bin/env bash
VERSION="0.0.2"
HISTORIAN_SRC=${HISTORIAN_SRC-"${HOME}/.bash_history"}
HISTORIAN_DB=${HISTORIAN_DB-"${HOME}/.historian.db"}
HISTORIAN_SQLITE3=${HISTORIAN_SQLITE3-"$(which sqlite3)"}
MAGIC="$(echo -e "\x10\x83\xB9\x9F\x34\xB5\x96\x45")" # 0118 999 881 999 119 725 3
MAGIC_ENUM_QUOTE=1
SEPARATOR=$(echo -e "\x01")
# Other ENV parameters:
#
# - ZSH_EXTENDED_HISTORY: if set, parses HISTORIAN_SRC using zsh's
# EXTENDED_HISTORY format
usage() {
echo "Usage: hist <subcommand>" >&2
echo "subcommands:" >&2
echo " config show config" >&2
echo " count count items in history" >&2
echo " import import to db" >&2
echo " shell launch sqlite3 shell with db" >&2
echo " search <term> search for <term>" >&2
echo " /term search for <term>" >&2
echo " version show the version" >&2
}
preflight_check() {
if [ -z "$HOME" ]; then
echo "need \$HOME" >&2
exit 1
fi
if [ -z "${HISTORIAN_SQLITE3}" ]; then
echo "need sqlite3" >&2
exit 1
fi
}
ensure_db_exists() {
( cat <<SQL
CREATE TABLE IF NOT EXISTS history (
id INTEGER PRIMARY KEY ASC,
command TEXT NOT NULL,
timestamp INTEGER
);
CREATE UNIQUE INDEX IF NOT EXISTS
history_command_timestamp ON history(command);
CREATE VIRTUAL TABLE IF NOT EXISTS
history_search USING fts4(id, history, command);
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
}
get_magic() {
local idx=$1
let offset_idx=idx+11
suffix=$(printf "%02d" ${offset_idx})
echo -n "${MAGIC}$(echo -ne "\x${suffix}")"
}
fail_unless_db_exists() {
if [ ! -f "${HISTORIAN_DB}" ]; then
echo "db (${HISTORIAN_DB}) doesn't exist. Aborting";
exit 1;
fi
}
cmd_config() {
echo "version: ${VERSION}"
echo "source_history: ${HISTORIAN_SRC}"
echo "db: ${HISTORIAN_DB}"
echo "sqlite3: ${HISTORIAN_SQLITE3}"
}
cmd_count() {
local args=$@
preflight_check;
ensure_db_exists;
( cat <<SQL
SELECT COUNT(*) FROM history;
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
}
cmd_import() {
local args=$@
preflight_check;
ensure_db_exists;
sanitized_src=$(mktemp)
cat ${HISTORIAN_SRC} \
| sed -e 's/"/'$(get_magic ${MAGIC_ENUM_QUOTE})'/g' \
> "${sanitized_src}" \
;
if [ -n "${ZSH_EXTENDED_HISTORY}" ]; then
_import_zsh_extended_history;
else
_import_default;
fi
}
_import_default() {
( cat <<SQL
CREATE TEMPORARY TABLE variables
(key TEXT, value INTEGER);
INSERT INTO variables(key, value)
SELECT 'items', COUNT(*) FROM history;
CREATE TEMPORARY TABLE history_import (line TEXT);
.separator ${SEPARATOR}
.import ${sanitized_src} history_import
UPDATE history_import
SET line = REPLACE(line, '$(get_magic ${MAGIC_ENUM_QUOTE})', '"');
INSERT OR IGNORE INTO history(command, timestamp)
SELECT line, NULL FROM history_import;
UPDATE variables
SET value = -1 * value + (SELECT COUNT(*) FROM history); -- lol subtraction
SELECT 'Imported ' || value || ' item(s).' FROM variables WHERE key = 'items';
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
rm -f ${sanitized_src};
}
# http://zsh.sourceforge.net/Doc/Release/Options.html#History
_import_zsh_extended_history() {
local tmpdir=$(mktemp -d)
tmp_src=${tmpdir}/$(basename $HISTORIAN_SRC)
sed -e 's/^: \([0-9][0-9]*\):\([0-9][0-9]*\);/\1'${SEPARATOR}'/g' ${HISTORIAN_SRC} \
> ${tmp_src}
( cat <<SQL
CREATE TEMPORARY TABLE variables
(key TEXT, value INTEGER);
INSERT INTO variables(key, value)
SELECT 'items', COUNT(*) FROM history;
CREATE TEMPORARY TABLE history_import (timestamp INTEGER, command TEXT);
.separator ${SEPARATOR}
.import ${tmp_src} history_import
INSERT OR IGNORE INTO history(timestamp, command)
SELECT timestamp, command FROM history_import;
UPDATE variables
SET value = -1 * value + (SELECT COUNT(*) FROM history); -- lol subtraction
SELECT 'Imported ' || value || ' item(s).' FROM variables WHERE key = 'items';
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
[ -n "$tmpdir" ] && rm -rf $tmpdir
}
cmd_log() {
local args=$@
preflight_check;
fail_unless_db_exists;
( cat <<SQL
.separator "\\n\\t"
SELECT id, command FROM history ORDER BY id DESC;
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}" | less;
}
cmd_search() {
preflight_check;
ensure_db_exists;
local args=$@
_search "$args"
}
cmd_search_slash() {
preflight_check;
ensure_db_exists;
local args=$@
term="$(echo "$args" | sed -e 's/^.//g')";
_search "$term"
}
_search() {
local args=$@
( cat <<SQL
.separator "\\n\\t"
SELECT id, command
FROM history
WHERE command LIKE '%${args}%'
ORDER BY id DESC;
SQL
) | "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
}
cmd_shell() {
local args=$@
preflight_check;
fail_unless_db_exists;
echo "${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}" >&2
"${HISTORIAN_SQLITE3}" "${HISTORIAN_DB}";
}
cmd_version() {
echo "historian version: ${VERSION}"
}
main() {
local cmd=$1
shift
case $cmd in
config)
cmd_config $@
;;
count)
cmd_count $@
;;
import)
cmd_import $@
;;
log)
cmd_log $@
;;
search)
cmd_search $@
;;
shell)
cmd_shell $@
;;
version)
cmd_version $@
;;
"")
usage
;;
*)
if [ -n "$(echo "$cmd" | grep -E '^/')" ]; then
cmd_search_slash $cmd $@
else
usage
exit 1
fi
;;
esac
}
main $@