1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 11:36:05 +01:00

zsh/history: optimize history options with array-based loops

Replace individual setopt/unsetopt statements for history options with efficient
array-based loops. Also optimize history substring search key bindings using
the same pattern. Use lib.hm.zsh.define for consistent array formatting and
add unset statements to clean up temporary variables.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman 2025-09-26 16:47:30 -05:00
parent c26a2ac2e4
commit 04f672b5db
4 changed files with 116 additions and 43 deletions

View file

@ -199,16 +199,46 @@ in
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK
${if cfg.history.append then "setopt" else "unsetopt"} APPEND_HISTORY
${if cfg.history.ignoreDups then "setopt" else "unsetopt"} HIST_IGNORE_DUPS
${if cfg.history.ignoreAllDups then "setopt" else "unsetopt"} HIST_IGNORE_ALL_DUPS
${if cfg.history.saveNoDups then "setopt" else "unsetopt"} HIST_SAVE_NO_DUPS
${if cfg.history.findNoDups then "setopt" else "unsetopt"} HIST_FIND_NO_DUPS
${if cfg.history.ignoreSpace then "setopt" else "unsetopt"} HIST_IGNORE_SPACE
${if cfg.history.expireDuplicatesFirst then "setopt" else "unsetopt"} HIST_EXPIRE_DUPS_FIRST
${if cfg.history.share then "setopt" else "unsetopt"} SHARE_HISTORY
${if cfg.history.extended then "setopt" else "unsetopt"} EXTENDED_HISTORY
${if cfg.autocd != null then "${if cfg.autocd then "setopt" else "unsetopt"} autocd" else ""}
${
let
historyOptions = {
APPEND_HISTORY = cfg.history.append;
HIST_IGNORE_DUPS = cfg.history.ignoreDups;
HIST_IGNORE_ALL_DUPS = cfg.history.ignoreAllDups;
HIST_SAVE_NO_DUPS = cfg.history.saveNoDups;
HIST_FIND_NO_DUPS = cfg.history.findNoDups;
HIST_IGNORE_SPACE = cfg.history.ignoreSpace;
HIST_EXPIRE_DUPS_FIRST = cfg.history.expireDuplicatesFirst;
SHARE_HISTORY = cfg.history.share;
EXTENDED_HISTORY = cfg.history.extended;
}
// lib.optionalAttrs (cfg.autocd != null) {
inherit (cfg) autocd;
};
enabledOpts = lib.filterAttrs (_: enabled: enabled) historyOptions;
disabledOpts = lib.filterAttrs (_: enabled: !enabled) historyOptions;
in
lib.concatStringsSep "\n\n" (
lib.filter (s: s != "") [
(lib.optionalString (enabledOpts != { }) ''
# Enabled history options
${lib.hm.zsh.define "enabled_opts" (lib.mapAttrsToList (name: _: name) enabledOpts)}
for opt in "''${enabled_opts[@]}"; do
setopt "$opt"
done
unset opt enabled_opts'')
(lib.optionalString (disabledOpts != { }) ''
# Disabled history options
${lib.hm.zsh.define "disabled_opts" (lib.mapAttrsToList (name: _: name) disabledOpts)}
for opt in "''${disabled_opts[@]}"; do
unsetopt "$opt"
done
unset opt disabled_opts'')
]
)
}
'')
(lib.mkIf (cfg.historySubstringSearch.enable or false) (
@ -217,12 +247,28 @@ in
# https://github.com/zsh-users/zsh-history-substring-search#usage
''
source ${pkgs.zsh-history-substring-search}/share/zsh-history-substring-search/zsh-history-substring-search.zsh
${lib.concatMapStringsSep "\n" (upKey: ''bindkey "${upKey}" history-substring-search-up'') (
lib.toList cfg.historySubstringSearch.searchUpKey
)}
${lib.concatMapStringsSep "\n" (downKey: ''bindkey "${downKey}" history-substring-search-down'') (
lib.toList cfg.historySubstringSearch.searchDownKey
)}
${
let
upKeys = lib.toList cfg.historySubstringSearch.searchUpKey;
downKeys = lib.toList cfg.historySubstringSearch.searchDownKey;
in
''
# Bind search up keys
${lib.hm.zsh.define "search_up_keys" upKeys}
for key in "''${search_up_keys[@]}"; do
bindkey "$key" history-substring-search-up
done
unset key search_up_keys
# Bind search down keys
${lib.hm.zsh.define "search_down_keys" downKeys}
for key in "''${search_down_keys[@]}"; do
bindkey "$key" history-substring-search-down
done
unset key search_down_keys
''
}
''
))
];

View file

@ -15,16 +15,25 @@ HISTFILE="/home/hm-user/.zsh_history"
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK
unsetopt APPEND_HISTORY
setopt HIST_IGNORE_DUPS
unsetopt HIST_IGNORE_ALL_DUPS
unsetopt HIST_SAVE_NO_DUPS
unsetopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
unsetopt HIST_EXPIRE_DUPS_FIRST
setopt SHARE_HISTORY
unsetopt EXTENDED_HISTORY
# Enabled history options
enabled_opts=(
HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY
)
for opt in "${enabled_opts[@]}"; do
setopt "$opt"
done
unset opt enabled_opts
# Disabled history options
disabled_opts=(
APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS
HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS
)
for opt in "${disabled_opts[@]}"; do
unsetopt "$opt"
done
unset opt disabled_opts

View file

@ -32,16 +32,25 @@
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK
unsetopt APPEND_HISTORY
setopt HIST_IGNORE_DUPS
unsetopt HIST_IGNORE_ALL_DUPS
unsetopt HIST_SAVE_NO_DUPS
unsetopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
unsetopt HIST_EXPIRE_DUPS_FIRST
setopt SHARE_HISTORY
unsetopt EXTENDED_HISTORY
# Enabled history options
enabled_opts=(
HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY
)
for opt in "''${enabled_opts[@]}"; do
setopt "$opt"
done
unset opt enabled_opts
# Disabled history options
disabled_opts=(
APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS
HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS
)
for opt in "''${disabled_opts[@]}"; do
unsetopt "$opt"
done
unset opt disabled_opts
alias -- test1=alias
alias -- test2=alias2

View file

@ -50,16 +50,25 @@
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK
unsetopt APPEND_HISTORY
setopt HIST_IGNORE_DUPS
unsetopt HIST_IGNORE_ALL_DUPS
unsetopt HIST_SAVE_NO_DUPS
unsetopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
unsetopt HIST_EXPIRE_DUPS_FIRST
setopt SHARE_HISTORY
unsetopt EXTENDED_HISTORY
# Enabled history options
enabled_opts=(
HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY
)
for opt in "''${enabled_opts[@]}"; do
setopt "$opt"
done
unset opt enabled_opts
# Disabled history options
disabled_opts=(
APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS
HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS
)
for opt in "''${disabled_opts[@]}"; do
unsetopt "$opt"
done
unset opt disabled_opts
# Default priority
echo "Default priority content"