-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashrc-timestamp-sqliteonly.txt
More file actions
56 lines (49 loc) · 1.98 KB
/
bashrc-timestamp-sqliteonly.txt
File metadata and controls
56 lines (49 loc) · 1.98 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
PS1='`RETURN=$?; sqliteaddstop "$RETURN" 2>/dev/null ; if [ $RETURN != 0 ]; then \
printf "\e[3m\e[2m\e[31m#!%03d\e[39m\e[49m" $RETURN ; else \
printf "\e[3m\e[2m\e[39m# \e[49m" ; fi ; \
printf "\e[39m\e[12m[\D{%Y-%m-%d_%H:%M:%S}\e7\e[20C]\e[1m (\u@\h:\w)\e[22m\n\[\e[10m\e[3m\e[2m\]#\[\e[0m\] "`'
# WARNING: for multiline prompts, \n must be outside the \[ \] non-printable indicator
## SQLite logging
function sqliteaddstart {
[[ $BASH_COMMAND =~ "^logout$" ]] && return
# get the command from history then strip the command number
local numandwhat="$(history 1)"
# remove leading spaces
numandwhat="${numandwhat#"${numandwhat%%[![:space:]]*}"}"
# read the sequence number
local num="${numandwhat%%' '*}"
# to avoid having to unset the trap in PROMPT_COMMAND and to deal with pipes
[[ $SEEN -eq $num ]] && return
# remove the number and the leading spaces
numandwhat="${numandwhat#*' '}"
what="${numandwhat#"${numandwhat%%[![:space:]]*}"}"
# avoid adding empty commands
[[ -z $what ]] && return
# IGNORE to avoid failing the UNIQUE constaint on pipes
sqlite3 "$SQLITEFILE" "
INSERT OR IGNORE INTO commands (ssid, seq, what, path) VALUES (
'${SID//\'/''}', '${num//\'/''}', '${what//\'/''}', '${PWD//\'/''}'
);"
# PROMPT_COMMAND contains several commands, only run once to optimize
export SEEN=$num
}
# upon starting a command, log it
trap sqliteaddstart DEBUG
# once done, complete with the error code and stop timestamp through PROMPT_COMMAND
function sqliteaddstop {
local ERR=$1
[[ -z $ERR ]] && ERR=0
# get the command from history then strip the command number
local numandwhat="$(history 1)"
# remove leading spaces
numandwhat="${numandwhat#"${numandwhat%%[![:space:]]*}"}"
# read the sequence number
local num="${numandwhat%%' '*}"
# don't do anything right after login
[[ $num <1 ]] && return
sqlite3 "$SQLITEFILE" "
UPDATE commands
SET err='${ERR//\'/''}', stop=current_timestamp
WHERE seq =${num//\'/''} AND ssid =${SID//\'/''}
;"
}