forked from jcsalterego/historian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhist
executable file
·188 lines (165 loc) · 3.77 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
#!/bin/bash
VERSION="0.0.1"
SRC="${HOME}/.bash_history"
DB="${HOME}/.historian.db"
SQLITE3=$(which sqlite3)
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
SQLITE3=$(which sqlite3)
if [ -z "$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
) | "${SQLITE3}" "${DB}";
}
fail_unless_db_exists() {
if [ ! -f "${DB}" ]; then
echo "db (${DB}) doesn't exist. Aborting";
exit 1;
fi
}
cmd_config() {
echo "version: ${VERSION}"
echo "bash_history: ${SRC}"
echo "db: ${DB}"
echo "sqlite3: ${SQLITE3}"
}
cmd_count() {
local args=$@
preflight_check;
ensure_db_exists;
( cat <<SQL
SELECT COUNT(*) FROM history;
SQL
) | "${SQLITE3}" "${DB}";
}
cmd_import() {
local args=$@
preflight_check;
ensure_db_exists;
( 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 \`
.import ${SRC} history_import
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
) | "${SQLITE3}" "${DB}";
}
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
) | "${SQLITE3}" "${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
) | "${SQLITE3}" "${DB}";
}
cmd_shell() {
local args=$@
preflight_check;
fail_unless_db_exists;
echo "${SQLITE3}" "${DB}" >&2
"${SQLITE3}" "${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 $@