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
|
2024-02-19 03:13:38 +01:00
|
|
|
__hishtory_bash_config_sourced=true
|
2022-11-04 05:01:57 +01:00
|
|
|
|
2023-12-31 22:00:56 +01:00
|
|
|
# For detecting color rendering support for this terminal, see #134
|
|
|
|
hishtory getColorSupport
|
|
|
|
export _hishtory_tui_color=$?
|
|
|
|
|
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() {
|
2023-12-10 18:14:12 +01:00
|
|
|
if [ -z "${HISHTORY_AT_PROMPT:-}" ]; then
|
2022-01-10 01:39:13 +01:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
unset HISHTORY_AT_PROMPT
|
|
|
|
|
|
|
|
# Run before every command
|
2023-11-18 21:19:19 +01:00
|
|
|
HISHTORY_START_TIME=`hishtory getTimestamp`
|
2023-09-30 03:21:23 +02:00
|
|
|
CMD=`history 1`
|
|
|
|
if ! [ -z "CMD " ] ; then
|
2024-06-09 05:50:45 +02:00
|
|
|
# This code with $LAST_PRESAVED_COMMAND and $LAST_SAVED_COMMAND is necessary to work around a quirk of
|
|
|
|
# bash. With bash, if you run the command `foo` and then press `Control+R`, it will trigger the DEBUG
|
|
|
|
# signal. And `history 1` will still return `foo` so this will lead to duplicate pre-saves causing entries
|
|
|
|
# to show up twice. This works around this issue by skipping presaving if the last commadn we presaved
|
|
|
|
# was identical.
|
|
|
|
#
|
|
|
|
# This does lead to a potential correctness bug since it means if someone runs the same non-terminating
|
|
|
|
# command twice in a row, we won't pre-save the second entry. But this seems reasonably unlikely
|
|
|
|
# such that it is worth accepting this issue to mitigate the duplicate entries observed in
|
|
|
|
# https://github.com/ddworken/hishtory/issues/215.
|
|
|
|
if [[ "$CMD" != "$LAST_PRESAVED_COMMAND" ]] && [[ "$CMD" != "$LAST_SAVED_COMMAND" ]]; then
|
2024-10-20 23:06:54 +02:00
|
|
|
(hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME &) 2>&1 >/dev/null # Background Run
|
|
|
|
# hishtory presaveHistoryEntry bash "$CMD" $HISHTORY_START_TIME 2>&1 >/dev/null # Foreground Run
|
2024-06-03 05:07:19 +02:00
|
|
|
fi
|
2023-08-27 23:24:59 +02:00
|
|
|
fi
|
2024-06-03 05:07:19 +02:00
|
|
|
LAST_PRESAVED_COMMAND=$CMD
|
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
|
|
|
|
|
2023-12-10 18:14:12 +01:00
|
|
|
if [ -n "${HISHTORY_FIRST_PROMPT:-}" ]; then
|
2022-01-10 01:39:13 +01:00
|
|
|
unset HISHTORY_FIRST_PROMPT
|
2024-10-03 15:56:15 +02:00
|
|
|
return $EXIT_CODE
|
2022-01-10 01:39:13 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Run after every prompt
|
2024-06-09 05:50:45 +02:00
|
|
|
CMD=`history 1`
|
2024-10-20 23:06:54 +02:00
|
|
|
(hishtory saveHistoryEntry bash $EXIT_CODE "$CMD" $HISHTORY_START_TIME &) 2>&1 >/dev/null # Background Run
|
|
|
|
# hishtory saveHistoryEntry bash $EXIT_CODE "$CMD" $HISHTORY_START_TIME 2>&1 >/dev/null # Foreground Run
|
2024-06-09 05:50:45 +02:00
|
|
|
|
|
|
|
LAST_SAVED_COMMAND=$CMD
|
2023-10-15 21:29:50 +02:00
|
|
|
|
|
|
|
(hishtory updateLocalDbFromRemote &)
|
2024-10-03 15:56:15 +02:00
|
|
|
|
|
|
|
return $EXIT_CODE
|
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() {
|
2024-02-19 21:12:04 +01:00
|
|
|
READLINE_LINE=$(HISHTORY_TERM_INTEGRATION=1 HISHTORY_SHELL_NAME=bash 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
|