-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.zsh.histdb
51 lines (44 loc) · 1.81 KB
/
.zsh.histdb
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
#!/usr/bin/env bash
# catches original bash, writes them to mariadb or sqlite3 file
# place it to the end of ~/.bashrc
# need to find original author of preexec_invoke_exec, google it :)
# source: https://github.com/digitalist/bash_database_history
#-------------------------------------------------------------------
# Create db and schema
#-------------------------------------------------------------------
# create database if not exists bash ;
# use bash ;
# -- drop table `history`;
# CREATE TABLE `history` (
# `oid` bigint(20) NOT NULL AUTO_INCREMENT,
# `command` TEXT,
# `arguments` TEXT,
# `cwd` TEXT,
# `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
# `tag` TEXT,
# PRIMARY KEY (`oid`),
# KEY `created` (`created`)
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ;
preexec() {
BASH_COMMAND=$1
if [[ -z "$HISTORY_TAG" ]]; then
HISTORY_TAG=''
else
echo TAG: $HISTORY_TAG
fi
[ -n "$COMP_LINE" ] && return # do nothing if completing
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
# get current command
local cur_cmd=$(echo $1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")
cwd=$(pwd)
# optional: ignore alias for mariadb history search
[[ "$BASH_COMMAND" =~ historyMysql* ]] && return
# optional: ignore pyenv
[[ "$BASH_COMMAND" =~ _pyenv_virtualenv_hook* ]] && return
# TODO
# ARGS=$@
# place ESCAPED $BASH_COMMAND into $BASH_COMMAND_ESCAPE variable
printf -v BASH_COMMAND_ESCAPE "%q" "$BASH_COMMAND" # from https://stackoverflow.com/a/4383994/5006740
# trap and write to db
mariadb -ucli-history -e "INSERT INTO sh.history (oid, command, arguments, cwd, created, tag) values (0, '${BASH_COMMAND_ESCAPE}', '', '$cwd', NOW(), '$HISTORY_TAG' )"
}