ohmyzsh/lib/history.zsh
Nick Glenn 8908e6d720
fix(history): fix logic error in prompt for history -c (#12500)
Logic error in `history -c` when prompting for confirmation caused history
to be deleted when typing anything but explicitly `n`, `N`, or sending `\n`.
New logic prevents deletion by pressing wrong key and only deletes
history when sending `y` or `Y`.

Co-authored-by: Sargates <nicholas.g.glenn@gmail.com>
2024-06-13 09:26:17 +02:00

49 lines
1.7 KiB
Bash

## History wrapper
function omz_history {
# parse arguments and remove from $@
local clear list stamp REPLY
zparseopts -E -D c=clear l=list f=stamp E=stamp i=stamp t:=stamp
if [[ -n "$clear" ]]; then
# if -c provided, clobber the history file
# confirm action before deleting history
print -nu2 "This action will irreversibly delete your command history. Are you sure? [y/N] "
builtin read -k1
[[ "$REPLY" = $'\n' ]] || print -u2
[[ "$REPLY" != ([yY]) ]] && return 0
print -nu2 >| "$HISTFILE"
fc -p "$HISTFILE"
print -u2 History file deleted.
elif [[ $# -eq 0 ]]; then
# if no arguments provided, show full history starting from 1
builtin fc $stamp -l 1
else
# otherwise, run `fc -l` with a custom format
builtin fc $stamp -l "$@"
fi
}
# Timestamp format
case ${HIST_STAMPS-} in
"mm/dd/yyyy") alias history='omz_history -f' ;;
"dd.mm.yyyy") alias history='omz_history -E' ;;
"yyyy-mm-dd") alias history='omz_history -i' ;;
"") alias history='omz_history' ;;
*) alias history="omz_history -t '$HIST_STAMPS'" ;;
esac
## History file configuration
[ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
[ "$HISTSIZE" -lt 50000 ] && HISTSIZE=50000
[ "$SAVEHIST" -lt 10000 ] && SAVEHIST=10000
## History command configuration
setopt extended_history # record timestamp of command in HISTFILE
setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt hist_ignore_dups # ignore duplicated commands history list
setopt hist_ignore_space # ignore commands that start with space
setopt hist_verify # show command with history expansion to user before running it
setopt share_history # share command history data