Skip to content

Conversation

khaneliman
Copy link
Collaborator

@khaneliman khaneliman commented Sep 20, 2025

Description

Just doing some loops for adding to path and sourcing to clean up the generated code.

Plugin path:

path+="/Users/khaneliman/.config/zsh/plugins/fzf-tab"
fpath+="/Users/khaneliman/.config/zsh/plugins/fzf-tab"

path+="/Users/khaneliman/.config/zsh/plugins/zsh-nix-shell"
fpath+="/Users/khaneliman/.config/zsh/plugins/zsh-nix-shell"

path+="/Users/khaneliman/.config/zsh/plugins/zsh-vi-mode"
fpath+="/Users/khaneliman/.config/zsh/plugins/zsh-vi-mode"

path+="/Users/khaneliman/.config/zsh/plugins/fast-syntax-highlighting"
fpath+="/Users/khaneliman/.config/zsh/plugins/fast-syntax-highlighting"

path+="/Users/khaneliman/.config/zsh/plugins/zsh-autosuggestions"
fpath+="/Users/khaneliman/.config/zsh/plugins/zsh-autosuggestions"

path+="/Users/khaneliman/.config/zsh/plugins/zsh-better-npm-completion"
fpath+="/Users/khaneliman/.config/zsh/plugins/zsh-better-npm-completion"

path+="/Users/khaneliman/.config/zsh/plugins/zsh-you-should-use"
fpath+="/Users/khaneliman/.config/zsh/plugins/zsh-you-should-use"

->

# Add plugin directories to PATH and fpath
plugin_dirs=(
  "fzf-tab" "zsh-nix-shell" "zsh-vi-mode" "fast-syntax-highlighting"
  "zsh-autosuggestions" "zsh-better-npm-completion" "zsh-you-should-use"
)
for plugin_dir in "${plugin_dirs[@]}"; do
  path+="/Users/khaneliman/.config/zsh/plugins/$plugin_dir"
  fpath+="/Users/khaneliman/.config/zsh/plugins/$plugin_dir"
done

Plugin sourcing:

if [[ -f "/Users/khaneliman/.config/zsh/plugins/fzf-tab/share/fzf-tab/fzf-tab.plugin.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/fzf-tab/share/fzf-tab/fzf-tab.plugin.zsh"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/zsh-nix-shell/share/zsh-nix-shell/nix-shell.plugin.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/zsh-nix-shell/share/zsh-nix-shell/nix-shell.plugin.zsh"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/zsh-vi-mode/share/zsh-vi-mode/zsh-vi-mode.plugin.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/zsh-vi-mode/share/zsh-vi-mode/zsh-vi-mode.plugin.zsh"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/fast-syntax-highlighting/share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/fast-syntax-highlighting/share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/zsh-autosuggestions/share/zsh-autosuggestions/zsh-autosuggestions.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/zsh-autosuggestions/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/zsh-better-npm-completion/share/zsh-better-npm-completion" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/zsh-better-npm-completion/share/zsh-better-npm-completion"
fi
if [[ -f "/Users/khaneliman/.config/zsh/plugins/zsh-you-should-use/share/zsh/plugins/you-should-use/you-should-use.plugin.zsh" ]]; then
  source "/Users/khaneliman/.config/zsh/plugins/zsh-you-should-use/share/zsh/plugins/you-should-use/you-should-use.plugin.zsh"
fi

->

# Source plugins
plugins=(
  "fzf-tab/share/fzf-tab/fzf-tab.plugin.zsh"
  "zsh-nix-shell/share/zsh-nix-shell/nix-shell.plugin.zsh"
  "zsh-vi-mode/share/zsh-vi-mode/zsh-vi-mode.plugin.zsh"
  "fast-syntax-highlighting/share/zsh/site-functions/fast-syntax-highlighting.plugin.zsh"
  "zsh-autosuggestions/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
  "zsh-better-npm-completion/share/zsh-better-npm-completion"
  "zsh-you-should-use/share/zsh/plugins/you-should-use/you-should-use.plugin.zsh"
)
for plugin in "${plugins[@]}"; do
  [[ -f "/Users/khaneliman/.config/zsh/plugins/$plugin" ]] && source "/Users/khaneliman/.config/zsh/plugins/$plugin"
d

Hist:

setopt HIST_FCNTL_LOCK
unsetopt APPEND_HISTORY
setopt HIST_IGNORE_DUPS
unsetopt HIST_IGNORE_ALL_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_EXPIRE_DUPS_FIRST
setopt SHARE_HISTORY
setopt EXTENDED_HISTORY
setopt autocd

source '/nix/store/i1q7kjxqmfwi4h2kdl18wyidqsxx3yp0-catppuccin-zsh-syntax-highlighting-0-unstable-2024-07-20/catppuccin_macchiato-zsh-syntax-highlighting.zsh'

setopt AUTO_LIST
setopt AUTO_PARAM_SLASH
setopt AUTO_PUSHD
setopt ALWAYS_TO_END
setopt CORRECT
setopt HIST_FCNTL_LOCK
setopt HIST_VERIFY
setopt INTERACTIVE_COMMENTS
setopt MENU_COMPLETE
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_TO_HOME
setopt PUSHD_SILENT
setopt NOTIFY
setopt PROMPT_SUBST
setopt MULTIOS
setopt NOFLOWCONTROL
setopt NO_CORRECT_ALL
setopt NO_HIST_BEEP
setopt NO_NOMATCH

->

setopt HIST_FCNTL_LOCK

# Enabled history options
enabled_opts=(
  "EXTENDED_HISTORY" "HIST_EXPIRE_DUPS_FIRST" "HIST_FIND_NO_DUPS"
  "HIST_IGNORE_DUPS" "HIST_IGNORE_SPACE" "HIST_SAVE_NO_DUPS" "SHARE_HISTORY"
  "autocd"
)
for opt in "${enabled_opts[@]}"; do
  setopt "$opt"
done

# Disabled history options
disabled_opts=("APPEND_HISTORY" "HIST_IGNORE_ALL_DUPS")
for opt in "${disabled_opts[@]}"; do
  unsetopt "$opt"
done

Checklist

  • Change is backwards compatible.

  • Code formatted with nix fmt or
    nix-shell -p treefmt nixfmt deadnix keep-sorted --run treefmt.

  • Code tested through nix run .#tests -- test-all or
    nix-shell --pure tests -A run.all.

  • Test cases updated/added. See example.

  • Commit messages are formatted like

    {component}: {description}
    
    {long description}
    

    See CONTRIBUTING for more information and recent commit messages for examples.

  • If this PR adds a new module

    • Added myself as module maintainer. See example.
    • Generate a news entry. See News
    • Basic tests added. See Tests
  • If this PR adds an exciting new feature or contains a breaking change.

    • Generate a news entry. See News

Replace individual conditional checks with a single loop over a plugin
array. This generates cleaner shell code and follows common bash patterns
while maintaining the same functionality.

Signed-off-by: Austin Horstman <[email protected]>
Add test assertions to verify the new array-based plugin loading
structure generates the expected shell code patterns.

Signed-off-by: Austin Horstman <[email protected]>
Replace individual plugin PATH and fpath statements with efficient
array-based loops, reducing generated shell code and improving
consistency with existing plugin sourcing logic.

Signed-off-by: Austin Horstman <[email protected]>
Update test assertions to match the new array and loop structure
for plugin directory and completion path setup, replacing checks
for individual statements with loop-based pattern verification.

Signed-off-by: Austin Horstman <[email protected]>
Use arrays and loops for bindkey statements instead of individual
statements, improving consistency with other zsh optimizations and
reducing generated shell code.

Signed-off-by: Austin Horstman <[email protected]>
Update test expectations to match the new array and loop structure
for key bindings instead of checking for individual bindkey statements.

Signed-off-by: Austin Horstman <[email protected]>
Replace repetitive setopt/unsetopt conditional statements with
efficient array-based loops, reducing generated shell code and
improving consistency with other zsh module optimizations.

Signed-off-by: Austin Horstman <[email protected]>
Replace individual setopt statements with array-based loops for
cfg.setOptions, improving consistency with other zsh optimizations
and reducing generated shell code.

Signed-off-by: Austin Horstman <[email protected]>
…zation

Add shared utility function that formats shell arrays with smart width optimization.

Signed-off-by: Austin Horstman <[email protected]>
Improve array formatting to pack multiple items per line based on
available width (~76 characters), making better use of terminal
space while maintaining readability.

Examples:
- Before: Each item on separate line
- After: Multiple items per line within width limit

disabled_opts=(
  "APPEND_HISTORY" "EXTENDED_HISTORY" "HIST_EXPIRE_DUPS_FIRST"
  "HIST_FIND_NO_DUPS" "HIST_IGNORE_ALL_DUPS" "HIST_SAVE_NO_DUPS"
)

This provides optimal balance of compactness and readability.

Signed-off-by: Austin Horstman <[email protected]>
Copy link
Contributor

@ambroisie ambroisie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the change, I read it commit-by-commit so suggestions might be slightly repetitive.

Comment on lines 128 to 130
plugins=(
${lib.concatMapStringsSep "\n " (path: ''"${path}"'') pluginPaths}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have lib.hm.zsh.define to do this. Arguably the formatting won't be quite as pretty (no newlines -- this could be fixed in define itself though)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note, I think both toZshValue and your own version of it fail to account for escaping caracters in a string.

This line should probably read lib.escapeShellArg v. Your mapped function should probably be lib.escapeShellArg instead of a naive quote.

)
for plugin in "''${plugins[@]}"; do
[[ -f "${pluginsDir}/$plugin" ]] && source "${pluginsDir}/$plugin"
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should unset plugin plugins after the loop.

Comment on lines 113 to 128
plugin_dirs=(
${lib.concatMapStringsSep "\n " (name: ''"${name}"'') pluginNames}
)
for plugin_dir in "''${plugin_dirs[@]}"; do
path+="${pluginsDir}/$plugin_dir"
fpath+="${pluginsDir}/$plugin_dir"
done
${lib.optionalString (completionPaths != [ ]) ''
# Add completion paths to fpath
completion_paths=(
${lib.concatMapStringsSep "\n " (path: ''"${path}"'') completionPaths}
)
for completion_path in "''${completion_paths[@]}"; do
fpath+="${pluginsDir}/$completion_path"
done
''}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestions.

Comment on lines 227 to 237
# Bind search up keys
search_up_keys=(${lib.concatStringsSep " " (map (key: ''"${key}"'') upKeys)})
for key in "''${search_up_keys[@]}"; do
bindkey "$key" history-substring-search-up
done
# Bind search down keys
search_down_keys=(${lib.concatStringsSep " " (map (key: ''"${key}"'') downKeys)})
for key in "''${search_down_keys[@]}"; do
bindkey "$key" history-substring-search-down
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestions.

Comment on lines 224 to 239
lib.filter (s: s != "") [
(lib.optionalString (enabledOpts != { }) ''
# Enabled history options
enabled_opts=(${lib.concatStringsSep " " (lib.mapAttrsToList (name: _: ''"${name}"'') enabledOpts)})
for opt in "''${enabled_opts[@]}"; do
setopt "$opt"
done'')
(lib.optionalString (disabledOpts != { }) ''
# Disabled history options
disabled_opts=(${
lib.concatStringsSep " " (lib.mapAttrsToList (name: _: ''"${name}"'') disabledOpts)
})
for opt in "''${disabled_opts[@]}"; do
unsetopt "$opt"
done'')
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestions.

Comment on lines 504 to 508
# Set shell options
set_opts=(${concatStringsSep " " (map (opt: ''"${opt}"'') cfg.setOptions)})
for opt in "''${set_opts[@]}"; do
setopt "$opt"
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestions.

Also: should the history options be deprecated to use cfg.setOptions instead? That'd be a big change so definitely not suggesting it for this MR, just floating the idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking about that, too. I mentioned the idea #7333 (comment) just wasn't 100% sure, yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use this in lib.hm.zsh.define directly, I guess.

mkOrder 950 ''
# Set shell options
set_opts=(${concatStringsSep " " (map (opt: ''"${opt}"'') cfg.setOptions)})
set_opts=(${lib.hm.shell.formatShellArray cfg.setOptions})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... Because I think this is slightly weird: formatShellArray does not define an array (i.e: I have to call it inside the (/)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants