From e5091f0b12a628d704fb3bef08b6c2a385862027 Mon Sep 17 00:00:00 2001 From: "Aaron D. Lee" Date: Mon, 22 Dec 2025 12:43:04 -0500 Subject: [PATCH] Dotfiles update 2025-12-22 12:43 --- zsh/functions/command-palette.zsh | 118 ++++++++++++++++-------------- 1 file changed, 65 insertions(+), 53 deletions(-) diff --git a/zsh/functions/command-palette.zsh b/zsh/functions/command-palette.zsh index 71f8384..4d710f3 100644 --- a/zsh/functions/command-palette.zsh +++ b/zsh/functions/command-palette.zsh @@ -12,11 +12,15 @@ # Keybinding: Ctrl+Space (configurable) # # Requirements: fzf -# -# Add to .zshrc: -# source ~/.dotfiles/zsh/functions/command-palette.zsh # ============================================================================ +# Source shared colors (with fallback) +source "${0:A:h}/../lib/colors.zsh" 2>/dev/null || \ +source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || { + typeset -g DF_GREEN=$'\033[0;32m' DF_BLUE=$'\033[0;34m' + typeset -g DF_CYAN=$'\033[0;36m' DF_NC=$'\033[0m' +} + # ============================================================================ # Configuration # ============================================================================ @@ -56,19 +60,18 @@ _palette_check_deps() { # ============================================================================ _palette_get_aliases() { - alias | sed 's/^alias //' | while IFS='=' read -r name cmd; do + alias | sed 's/^alias //' | while IFS='=' read -r alias_name cmd; do cmd="${cmd#\'}" cmd="${cmd%\'}" cmd="${cmd#\"}" cmd="${cmd%\"}" - printf "%s\t%s\t%s\t%s\n" "$ICON_ALIAS" "alias" "$name" "$cmd" + printf "%s\t%s\t%s\t%s\n" "$ICON_ALIAS" "alias" "$alias_name" "$cmd" done } _palette_get_functions() { - # Get user-defined functions (not starting with _) - print -l ${(ok)functions} | grep -v '^_' | while read -r name; do - printf "%s\t%s\t%s\t%s\n" "$ICON_FUNC" "func" "$name" "function" + print -l ${(ok)functions} | grep -v '^_' | while read -r func_name; do + printf "%s\t%s\t%s\t%s\n" "$ICON_FUNC" "func" "$func_name" "function" done } @@ -81,8 +84,8 @@ _palette_get_history() { _palette_get_bookmarks() { [[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && return - while IFS='|' read -r name path; do - [[ -n "$name" && -n "$path" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "bookmark" "$name" "cd $path" + while IFS='|' read -r bm_name bm_path; do + [[ -n "$bm_name" && -n "$bm_path" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "bookmark" "$bm_name" "cd $bm_path" done < "$PALETTE_BOOKMARKS_FILE" } @@ -91,13 +94,12 @@ _palette_get_scripts() { for script in "$DOTFILES_DIR/bin"/*.sh; do [[ -f "$script" ]] || continue - local name=$(basename "$script" .sh) - printf "%s\t%s\t%s\t%s\n" "$ICON_SCRIPT" "script" "$name" "$script" + local script_name=$(basename "$script" .sh) + printf "%s\t%s\t%s\t%s\n" "$ICON_SCRIPT" "script" "$script_name" "$script" done } _palette_get_git_commands() { - # Only show if in git repo git rev-parse --git-dir &>/dev/null || return local branch=$(git branch --show-current 2>/dev/null) @@ -139,7 +141,6 @@ _palette_get_actions() { } _palette_get_directories() { - # Recent directories from dirstack dirs -v 2>/dev/null | tail -n +2 | head -10 | while read -r num dir; do [[ -n "$dir" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "recent" "$dir" "cd $dir" done @@ -190,16 +191,13 @@ command_palette() { case "$key" in ctrl-e) - # Edit mode - put command on line without executing print -z "$cmd" ;; ctrl-y) - # Yank - copy to clipboard echo -n "$cmd" | pbcopy 2>/dev/null || echo -n "$cmd" | xclip -selection clipboard 2>/dev/null echo "Copied: $cmd" ;; *) - # Default - execute echo "❯ $cmd" eval "$cmd" ;; @@ -215,22 +213,28 @@ p() { command_palette; } # ============================================================================ bookmark() { - local name="$1" - local path="${2:-$(pwd)}" + local bm_name="$1" + local bm_path="${2:-$(pwd)}" - if [[ -z "$name" ]]; then + # Ensure bookmarks file parent directory exists + mkdir -p "$(dirname "$PALETTE_BOOKMARKS_FILE")" 2>/dev/null + + # Create bookmarks file if it doesn't exist + [[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && touch "$PALETTE_BOOKMARKS_FILE" + + if [[ -z "$bm_name" ]]; then echo "Usage: bookmark [path]" echo " bookmark list" echo " bookmark delete " return 1 fi - case "$name" in + case "$bm_name" in list|ls) - if [[ -f "$PALETTE_BOOKMARKS_FILE" ]]; then + if [[ -s "$PALETTE_BOOKMARKS_FILE" ]]; then echo "Bookmarks:" - while IFS='|' read -r n p; do - echo " $n → $p" + while IFS='|' read -r stored_name stored_path || [[ -n "$stored_name" ]]; do + [[ -n "$stored_name" ]] && echo " $stored_name → $stored_path" done < "$PALETTE_BOOKMARKS_FILE" else echo "No bookmarks yet" @@ -238,46 +242,61 @@ bookmark() { ;; delete|rm) local to_delete="$2" - [[ -z "$to_delete" ]] && { echo "Specify bookmark to delete"; return 1; } - [[ -f "$PALETTE_BOOKMARKS_FILE" ]] && { - grep -v "^$to_delete|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp" - mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE" + if [[ -z "$to_delete" ]]; then + echo "Specify bookmark to delete" + return 1 + fi + if [[ -s "$PALETTE_BOOKMARKS_FILE" ]]; then + # Use a temp file approach that won't hang + local temp_file="${PALETTE_BOOKMARKS_FILE}.tmp.$$" + grep -v "^${to_delete}|" "$PALETTE_BOOKMARKS_FILE" > "$temp_file" 2>/dev/null || true + mv -f "$temp_file" "$PALETTE_BOOKMARKS_FILE" echo "Deleted: $to_delete" - } + else + echo "No bookmarks to delete" + fi ;; *) - mkdir -p "$(dirname "$PALETTE_BOOKMARKS_FILE")" - # Remove existing bookmark with same name - [[ -f "$PALETTE_BOOKMARKS_FILE" ]] && { - grep -v "^$name|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp" - mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE" - } - echo "$name|$path" >> "$PALETTE_BOOKMARKS_FILE" - echo "Bookmarked: $name → $path" + # Remove existing bookmark with same name (if file has content) + if [[ -s "$PALETTE_BOOKMARKS_FILE" ]]; then + local temp_file="${PALETTE_BOOKMARKS_FILE}.tmp.$$" + grep -v "^${bm_name}|" "$PALETTE_BOOKMARKS_FILE" > "$temp_file" 2>/dev/null || true + mv -f "$temp_file" "$PALETTE_BOOKMARKS_FILE" + fi + # Add new bookmark + echo "${bm_name}|${bm_path}" >> "$PALETTE_BOOKMARKS_FILE" + echo "Bookmarked: $bm_name → $bm_path" ;; esac } # Quick jump to bookmark jump() { - local name="$1" + local bm_name="$1" - if [[ -z "$name" ]]; then + if [[ -z "$bm_name" ]]; then # Fuzzy select bookmark - [[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && { echo "No bookmarks"; return 1; } + if [[ ! -s "$PALETTE_BOOKMARKS_FILE" ]]; then + echo "No bookmarks" + return 1 + fi local selection=$(cat "$PALETTE_BOOKMARKS_FILE" | \ fzf --height=40% --layout=reverse --delimiter='|' --with-nth=1 \ --preview='echo "Path: $(echo {} | cut -d"|" -f2)"') - [[ -n "$selection" ]] && { - local path=$(echo "$selection" | cut -d'|' -f2) - cd "$path" && echo "→ $path" - } + if [[ -n "$selection" ]]; then + local jump_path=$(echo "$selection" | cut -d'|' -f2) + cd "$jump_path" && echo "→ $jump_path" + fi else # Direct jump - local path=$(grep "^$name|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null | cut -d'|' -f2) - [[ -n "$path" ]] && cd "$path" || echo "Bookmark not found: $name" + local jump_path=$(grep "^${bm_name}|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null | cut -d'|' -f2) + if [[ -n "$jump_path" ]]; then + cd "$jump_path" && echo "→ $jump_path" + else + echo "Bookmark not found: $bm_name" + fi fi } @@ -302,10 +321,3 @@ bindkey "$PALETTE_HOTKEY" _palette_widget # Alternative binding: Ctrl+P bindkey '^P' _palette_widget - -# ============================================================================ -# Initialization Message -# ============================================================================ - -# Uncomment to show on load: -# echo "Command palette loaded. Press Ctrl+Space or Ctrl+P to open."