2022-01-10 01:39:13 +01:00
|
|
|
# This script should be sourced inside of .bashrc to integrate bash with hishtory
|
|
|
|
|
2022-11-04 05:01:57 +01:00
|
|
|
# Include guard. This file is sourced in multiple places, but we want it to only execute once.
|
|
|
|
# This trick is from https://stackoverflow.com/questions/7518584/is-there-any-mechanism-in-shell-script-alike-include-guard-in-c
|
|
|
|
if [ -n "$__hishtory_bash_config_sourced" ]; then return; fi
|
|
|
|
__hishtory_bash_config_sourced=`date`
|
|
|
|
|
2022-09-28 06:10:45 +02:00
|
|
|
# Implementation of running before/after every command based on https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/
|
|
|
|
function __hishtory_precommand() {
|
2022-01-10 01:39:13 +01:00
|
|
|
if [ -z "$HISHTORY_AT_PROMPT" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
unset HISHTORY_AT_PROMPT
|
|
|
|
|
|
|
|
# Run before every command
|
2022-04-19 07:36:57 +02:00
|
|
|
HISHTORY_START_TIME=`date +%s`
|
2023-08-27 23:24:59 +02:00
|
|
|
if ! [ -z "BASH_COMMAND " ] && [ "$BASH_COMMAND" != "__hishtory_postcommand" ]; then
|
2023-09-17 23:11:44 +02:00
|
|
|
(hishtory presaveHistoryEntry bash "$BASH_COMMAND" $HISHTORY_START_TIME &) # Background Run
|
|
|
|
# hishtory presaveHistoryEntry bash "$BASH_COMMAND" $HISHTORY_START_TIME # Foreground Run
|
2023-08-27 23:24:59 +02:00
|
|
|
fi
|
2022-01-10 01:39:13 +01:00
|
|
|
}
|
2022-09-28 06:10:45 +02:00
|
|
|
trap "__hishtory_precommand" DEBUG
|
2022-01-10 01:39:13 +01:00
|
|
|
|
|
|
|
HISHTORY_FIRST_PROMPT=1
|
2022-09-28 06:10:45 +02:00
|
|
|
function __hishtory_postcommand() {
|
2022-04-08 07:53:39 +02:00
|
|
|
EXIT_CODE=$?
|
2022-01-10 01:39:13 +01:00
|
|
|
HISHTORY_AT_PROMPT=1
|
|
|
|
|
|
|
|
if [ -n "$HISHTORY_FIRST_PROMPT" ]; then
|
|
|
|
unset HISHTORY_FIRST_PROMPT
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run after every prompt
|
2022-10-24 02:35:02 +02:00
|
|
|
(hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME &) # Background Run
|
2022-11-03 03:46:02 +01:00
|
|
|
# hishtory saveHistoryEntry bash $EXIT_CODE "`history 1`" $HISHTORY_START_TIME # Foreground Run
|
2022-01-10 01:39:13 +01:00
|
|
|
}
|
2022-09-28 06:10:45 +02:00
|
|
|
PROMPT_COMMAND="__hishtory_postcommand; $PROMPT_COMMAND"
|
2022-09-28 07:25:57 +02:00
|
|
|
export HISTTIMEFORMAT=$HISTTIMEFORMAT
|
2022-10-18 05:18:28 +02:00
|
|
|
|
|
|
|
__history_control_r() {
|
2022-11-20 07:27:08 +01:00
|
|
|
READLINE_LINE=$(HISHTORY_TERM_INTEGRATION=1 hishtory tquery "$READLINE_LINE")
|
2022-10-18 05:18:28 +02:00
|
|
|
READLINE_POINT=0x7FFFFFFF
|
|
|
|
}
|
|
|
|
|
|
|
|
__hishtory_bind_control_r() {
|
|
|
|
bind -x '"\C-r": __history_control_r'
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "$(hishtory config-get enable-control-r)" = true ] && __hishtory_bind_control_r
|