Dotfiles update 2025-12-22 12:43

This commit is contained in:
Aaron D. Lee
2025-12-22 12:43:04 -05:00
parent 64bc7623a7
commit e5091f0b12

View File

@@ -12,11 +12,15 @@
# Keybinding: Ctrl+Space (configurable) # Keybinding: Ctrl+Space (configurable)
# #
# Requirements: fzf # 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 # Configuration
# ============================================================================ # ============================================================================
@@ -56,19 +60,18 @@ _palette_check_deps() {
# ============================================================================ # ============================================================================
_palette_get_aliases() { _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%\'}"
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 done
} }
_palette_get_functions() { _palette_get_functions() {
# Get user-defined functions (not starting with _) print -l ${(ok)functions} | grep -v '^_' | while read -r func_name; do
print -l ${(ok)functions} | grep -v '^_' | while read -r name; do printf "%s\t%s\t%s\t%s\n" "$ICON_FUNC" "func" "$func_name" "function"
printf "%s\t%s\t%s\t%s\n" "$ICON_FUNC" "func" "$name" "function"
done done
} }
@@ -81,8 +84,8 @@ _palette_get_history() {
_palette_get_bookmarks() { _palette_get_bookmarks() {
[[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && return [[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && return
while IFS='|' read -r name path; do while IFS='|' read -r bm_name bm_path; do
[[ -n "$name" && -n "$path" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "bookmark" "$name" "cd $path" [[ -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" done < "$PALETTE_BOOKMARKS_FILE"
} }
@@ -91,13 +94,12 @@ _palette_get_scripts() {
for script in "$DOTFILES_DIR/bin"/*.sh; do for script in "$DOTFILES_DIR/bin"/*.sh; do
[[ -f "$script" ]] || continue [[ -f "$script" ]] || continue
local name=$(basename "$script" .sh) local script_name=$(basename "$script" .sh)
printf "%s\t%s\t%s\t%s\n" "$ICON_SCRIPT" "script" "$name" "$script" printf "%s\t%s\t%s\t%s\n" "$ICON_SCRIPT" "script" "$script_name" "$script"
done done
} }
_palette_get_git_commands() { _palette_get_git_commands() {
# Only show if in git repo
git rev-parse --git-dir &>/dev/null || return git rev-parse --git-dir &>/dev/null || return
local branch=$(git branch --show-current 2>/dev/null) local branch=$(git branch --show-current 2>/dev/null)
@@ -139,7 +141,6 @@ _palette_get_actions() {
} }
_palette_get_directories() { _palette_get_directories() {
# Recent directories from dirstack
dirs -v 2>/dev/null | tail -n +2 | head -10 | while read -r num dir; do 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" [[ -n "$dir" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "recent" "$dir" "cd $dir"
done done
@@ -190,16 +191,13 @@ command_palette() {
case "$key" in case "$key" in
ctrl-e) ctrl-e)
# Edit mode - put command on line without executing
print -z "$cmd" print -z "$cmd"
;; ;;
ctrl-y) ctrl-y)
# Yank - copy to clipboard
echo -n "$cmd" | pbcopy 2>/dev/null || echo -n "$cmd" | xclip -selection clipboard 2>/dev/null echo -n "$cmd" | pbcopy 2>/dev/null || echo -n "$cmd" | xclip -selection clipboard 2>/dev/null
echo "Copied: $cmd" echo "Copied: $cmd"
;; ;;
*) *)
# Default - execute
echo " $cmd" echo " $cmd"
eval "$cmd" eval "$cmd"
;; ;;
@@ -215,22 +213,28 @@ p() { command_palette; }
# ============================================================================ # ============================================================================
bookmark() { bookmark() {
local name="$1" local bm_name="$1"
local path="${2:-$(pwd)}" 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 <name> [path]" echo "Usage: bookmark <name> [path]"
echo " bookmark list" echo " bookmark list"
echo " bookmark delete <name>" echo " bookmark delete <name>"
return 1 return 1
fi fi
case "$name" in case "$bm_name" in
list|ls) list|ls)
if [[ -f "$PALETTE_BOOKMARKS_FILE" ]]; then if [[ -s "$PALETTE_BOOKMARKS_FILE" ]]; then
echo "Bookmarks:" echo "Bookmarks:"
while IFS='|' read -r n p; do while IFS='|' read -r stored_name stored_path || [[ -n "$stored_name" ]]; do
echo " $n$p" [[ -n "$stored_name" ]] && echo " $stored_name$stored_path"
done < "$PALETTE_BOOKMARKS_FILE" done < "$PALETTE_BOOKMARKS_FILE"
else else
echo "No bookmarks yet" echo "No bookmarks yet"
@@ -238,46 +242,61 @@ bookmark() {
;; ;;
delete|rm) delete|rm)
local to_delete="$2" local to_delete="$2"
[[ -z "$to_delete" ]] && { echo "Specify bookmark to delete"; return 1; } if [[ -z "$to_delete" ]]; then
[[ -f "$PALETTE_BOOKMARKS_FILE" ]] && { echo "Specify bookmark to delete"
grep -v "^$to_delete|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp" return 1
mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE" 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" echo "Deleted: $to_delete"
} else
echo "No bookmarks to delete"
fi
;; ;;
*) *)
mkdir -p "$(dirname "$PALETTE_BOOKMARKS_FILE")" # Remove existing bookmark with same name (if file has content)
# Remove existing bookmark with same name if [[ -s "$PALETTE_BOOKMARKS_FILE" ]]; then
[[ -f "$PALETTE_BOOKMARKS_FILE" ]] && { local temp_file="${PALETTE_BOOKMARKS_FILE}.tmp.$$"
grep -v "^$name|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp" grep -v "^${bm_name}|" "$PALETTE_BOOKMARKS_FILE" > "$temp_file" 2>/dev/null || true
mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE" mv -f "$temp_file" "$PALETTE_BOOKMARKS_FILE"
} fi
echo "$name|$path" >> "$PALETTE_BOOKMARKS_FILE" # Add new bookmark
echo "Bookmarked: $name$path" echo "${bm_name}|${bm_path}" >> "$PALETTE_BOOKMARKS_FILE"
echo "Bookmarked: $bm_name$bm_path"
;; ;;
esac esac
} }
# Quick jump to bookmark # Quick jump to bookmark
jump() { jump() {
local name="$1" local bm_name="$1"
if [[ -z "$name" ]]; then if [[ -z "$bm_name" ]]; then
# Fuzzy select bookmark # 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" | \ local selection=$(cat "$PALETTE_BOOKMARKS_FILE" | \
fzf --height=40% --layout=reverse --delimiter='|' --with-nth=1 \ fzf --height=40% --layout=reverse --delimiter='|' --with-nth=1 \
--preview='echo "Path: $(echo {} | cut -d"|" -f2)"') --preview='echo "Path: $(echo {} | cut -d"|" -f2)"')
[[ -n "$selection" ]] && { if [[ -n "$selection" ]]; then
local path=$(echo "$selection" | cut -d'|' -f2) local jump_path=$(echo "$selection" | cut -d'|' -f2)
cd "$path" && echo "$path" cd "$jump_path" && echo "$jump_path"
} fi
else else
# Direct jump # Direct jump
local path=$(grep "^$name|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null | cut -d'|' -f2) local jump_path=$(grep "^${bm_name}|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null | cut -d'|' -f2)
[[ -n "$path" ]] && cd "$path" || echo "Bookmark not found: $name" if [[ -n "$jump_path" ]]; then
cd "$jump_path" && echo "$jump_path"
else
echo "Bookmark not found: $bm_name"
fi
fi fi
} }
@@ -302,10 +321,3 @@ bindkey "$PALETTE_HOTKEY" _palette_widget
# Alternative binding: Ctrl+P # Alternative binding: Ctrl+P
bindkey '^P' _palette_widget bindkey '^P' _palette_widget
# ============================================================================
# Initialization Message
# ============================================================================
# Uncomment to show on load:
# echo "Command palette loaded. Press Ctrl+Space or Ctrl+P to open."