Files
dotfiles/shell/functions.zsh
T
David F GliddenandClaude 389febb161 🚀 Complete machine migration setup with encrypted backups
- Added comprehensive configuration files for seamless migration
- SSH, GPG, Karabiner, iTerm2, Neofetch, SwiftBar configs
- Pass license management system with templates and documentation
- Enhanced shell functions with MAS updates and moon phase tracking
- Comprehensive encrypted backup system (GPG AES256)
- Included encrypted backups of all sensitive data
- BBEdit as default editor with proper configuration
- Fixed shell compatibility issues
- Merged existing .zsh configs with improvements

Security:
- All sensitive data is GPG encrypted (.gpg files)
- Private keys excluded from version control
- Only configs and encrypted backups are tracked

Following prime directive: durable, thoughtful solutions

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-28 23:10:36 +02:00

229 lines
8.2 KiB
Bash

# ░█▀▀░█░█░█▀█░█▀▀░▀█▀░▀█▀░█▀█░█▀█░█▀▀
# ░█▀▀░█░█░█░█░█░░░░█░░░█░░█░█░█░█░▀▀█
# ░▀░░░▀▀▀░▀░▀░▀▀▀░░▀░░▀▀▀░▀▀▀░▀░▀░▀▀▀
## I. PDF Compression #######################
pdfcompress ()
{
gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=$1.compressed.pdf $1;
}
## II. Weather #######################
wttr() {
# Usage: wttr [location]
# Examples: wttr -> your IP-based location
# wttr 75012 -> Paris 12e
# wttr 23190 -> Crozant
local location=${1:-}
curl -s "wttr.in/${location}?n"
}
## III. Updates (system, brew, etc) #######################
## Description: Performs system updates, Homebrew cleanup, fzf refresh, moon phase logging, and dotfile sync ###########################
function sysupdate() {
local start_time=$(date +%s)
echo "🔧 Starting full system update at $(date)"
# Ensure sudo credentials are fresh
if ! sudo -vn 2>/dev/null; then
echo "🔒 sudo authentication required..."
sudo -v
fi
echo "🍎 Updating macOS Software..."
sudo softwareupdate -i -a
echo "🍺 Updating Homebrew packages..."
brew update
brew upgrade
brew cleanup
brew autoremove
# Mac App Store updates
echo "🍎 Updating Mac App Store apps..."
if command -v mas >/dev/null; then
local mas_outdated=$(mas outdated)
if [[ -n "$mas_outdated" ]]; then
echo "📱 Updating MAS apps:"
echo "$mas_outdated" | while read -r line; do
echo " $line"
done
mas upgrade || echo "⚠️ Some MAS updates may require manual intervention"
else
echo "✅ All MAS apps are up to date"
fi
else
echo "⚠️ mas not installed - install with: brew install mas"
fi
echo "🔄 Refreshing fzf..."
if [ -d ~/.fzf ]; then
cd ~/.fzf && git pull && ./install --all && cd -
fi
# Fix Zsh completion security warnings
echo "🔒 Checking Zsh completion security..."
if [[ -d "/opt/homebrew/share/zsh" ]]; then
# Fix common Homebrew Zsh permission issues
chmod 755 /opt/homebrew/share/zsh /opt/homebrew/share/zsh/site-functions 2>/dev/null || true
fi
echo "🧹 Checking for broken symlinks..."
find /usr/local/bin "$HOME/bin" "$HOME/.local/bin" -xtype l 2>/dev/null
echo "🌓 Moon phase at update time:"
if command -v curl &>/dev/null && command -v jq >/dev/null; then
# Use API key from SwiftBar script
local api_key="6ec71a8170214d828c08ba8bf2ca29e9"
moon_json=$(curl -s "https://api.ipgeolocation.io/astronomy?apiKey=$api_key" 2>/dev/null)
if [[ $? -eq 0 ]] && [[ -n "$moon_json" ]]; then
moon_phase=$(echo "$moon_json" | jq -r '.moon_phase // "Unknown"' | tr '_' ' ')
illum=$(echo "$moon_json" | jq -r '.moon_illumination_percentage // "Unknown"')
# Add moon emoji based on phase
local moon_emoji="🌙"
case "$moon_phase" in
"new moon") moon_emoji="🌑" ;;
"waxing crescent") moon_emoji="🌒" ;;
"first quarter") moon_emoji="🌓" ;;
"waxing gibbous") moon_emoji="🌔" ;;
"full moon") moon_emoji="🌕" ;;
"waning gibbous") moon_emoji="🌖" ;;
"last quarter") moon_emoji="🌗" ;;
"waning crescent") moon_emoji="🌘" ;;
esac
echo "$moon_emoji $moon_phase (${illum}%)"
else
echo "🌙 Moon phase unavailable"
fi
else
echo "🌙 Moon phase tracking disabled (missing curl or jq)"
fi
if [ -d "$HOME/.dotfiles" ]; then
echo "📝 Committing dotfile changes..."
cd ~/.dotfiles || return
git add . && git commit -m "🔧 Auto-commit from sysupdate on $(date '+%Y-%m-%d %H:%M')" && git push
brew bundle dump --force --file="$HOME/.dotfiles/Brewfile"
cd - >/dev/null || return
fi
echo "📊 System Info:"
echo "💻 $(uname -a)"
echo "📅 macOS: $(sw_vers | grep ProductVersion)"
echo "🧠 Memory: $(top -l 1 -s 0 | grep PhysMem)"
# Optional post-update hook
if [ -x "$HOME/.zsh/hooks/post-update" ]; then
"$HOME/.zsh/hooks/post-update"
fi
echo "🕓 Duration: $(($(date +%s) - $start_time))s"
osascript -e 'display notification "Update complete!" with title "System Maintenance" subtitle "All systems go 🚀"'
}
# 💤 Quiet version (no output, same functionality)
function quietupdate() {
local start_time=$(date +%s)
sudo -vn 2>/dev/null || sudo -v
sudo softwareupdate -i -a >/dev/null
brew update >/dev/null && brew upgrade >/dev/null && brew cleanup >/dev/null && brew autoremove >/dev/null
[ -d ~/.fzf ] && cd ~/.fzf && git pull >/dev/null && ./install --all >/dev/null && cd - >/dev/null
[ -d "$HOME/.dotfiles" ] && cd ~/.dotfiles && git add . && git commit -m "auto sysupdate" && git push && brew bundle dump --force --file="$HOME/.dotfiles/Brewfile" && cd - >/dev/null
if [ -x "$HOME/.zsh/hooks/post-update" ]; then "$HOME/.zsh/hooks/post-update"; fi
osascript -e 'display notification "Quiet update complete!" with title "System Maintenance"'
}
## IV. Enhanced Man Page Viewer #######################
# Detect current terminal for optimal display
detect_terminal() {
if [[ -n "${KITTY_WINDOW_ID-}" ]]; then
echo "kitty"
elif [[ -n "${ITERM_SESSION_ID-}" ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
echo "iterm"
elif [[ "$TERM_PROGRAM" == "kitty" ]]; then
echo "kitty"
else
echo "unknown"
fi
}
# Enhanced man function with beautiful formatting in new window
man() {
# If no arguments, show usage
if [[ $# -eq 0 ]]; then
echo "Usage: man <command> [section]"
echo "Opens beautifully formatted man page in new terminal window"
return 1
fi
local cmd="$1"
local section="${2:-}"
local terminal_type=$(detect_terminal)
# Check if man page exists
if ! command man -w ${section:+$section} "$cmd" >/dev/null 2>&1; then
echo "❌ No manual entry for '$cmd'${section:+ in section $section}"
return 1
fi
# Build the command to run in new window
local man_cmd
if command -v bat >/dev/null; then
# Use bat for beautiful syntax highlighting with Nord theme
man_cmd="MANPAGER='bat --language=man --style=grid --color=always --theme=Nord' man ${section:+$section} '$cmd'"
else
# Fallback to enhanced less
man_cmd="MANPAGER='less -R' man ${section:+$section} '$cmd'"
fi
# Terminal-specific window creation
case "$terminal_type" in
"kitty")
# Kitty: New window with optimal size for reading
kitty @ new-window --title "📖 man $cmd${section:+ ($section)}" --cwd "$PWD" \
zsh -c "$man_cmd; echo; echo '📖 Press any key to close...'; read -k1"
;;
"iterm")
# iTerm2: New window with AppleScript
osascript -e "
tell application \"iTerm\"
create window with default profile
tell current session of current window
write text \"$man_cmd; echo; echo '📖 Press any key to close...'; read -k1\"
set name to \"📖 man $cmd${section:+ ($section)}\"
end tell
end tell
" >/dev/null 2>&1
;;
*)
# Fallback: run in current terminal with nice formatting
echo "📖 Displaying man page for '$cmd'${section:+ (section $section)}:"
echo ""
eval "$man_cmd"
;;
esac
}
# Quick man page search function
mans() {
if [[ $# -eq 0 ]]; then
echo "Usage: mans <search_term>"
echo "Search for man pages containing the term"
return 1
fi
echo "🔍 Searching man pages for: $*"
echo ""
# Use apropos to search, format nicely
if command -v bat >/dev/null; then
apropos "$*" | bat --language=man --style=plain --theme=Nord
else
apropos "$*"
fi
}