Dotfiles update 2025-12-25 12:04
This commit is contained in:
210
refactor_backup/zsh/functions/btrfs-helpers.zsh
Normal file
210
refactor_backup/zsh/functions/btrfs-helpers.zsh
Normal file
@@ -0,0 +1,210 @@
|
||||
# ============================================================================
|
||||
# Btrfs Helpers for Arch/CachyOS
|
||||
# ============================================================================
|
||||
# Quick commands for btrfs filesystem management
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g BTRFS_DEFAULT_MOUNT="${BTRFS_DEFAULT_MOUNT:-/}"
|
||||
|
||||
_btrfs_check() {
|
||||
df_require_cmd btrfs btrfs-progs || return 1
|
||||
if ! df_is_btrfs; then
|
||||
df_print_warning "Root filesystem is not btrfs"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
btrfs-usage() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Filesystem Usage: ${mount}"
|
||||
sudo btrfs filesystem usage "$mount" -h
|
||||
}
|
||||
|
||||
btrfs-subs() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Subvolumes"
|
||||
df_print_section "Subvolume List"
|
||||
sudo btrfs subvolume list "$mount" | while read -r line; do
|
||||
local path=$(echo "$line" | awk '{print $NF}')
|
||||
local id=$(echo "$line" | awk '{print $2}')
|
||||
df_print_indent "● [$id] $path"
|
||||
done
|
||||
echo ""
|
||||
df_print_section "Default Subvolume"
|
||||
sudo btrfs subvolume get-default "$mount"
|
||||
}
|
||||
|
||||
btrfs-balance() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
local usage="${2:-50}"
|
||||
df_print_func_name "Btrfs Balance"
|
||||
df_confirm_warning "This may take a while and use significant I/O" || return 0
|
||||
echo ""
|
||||
df_print_step "Balancing data chunks with <${usage}% usage..."
|
||||
sudo btrfs balance start -dusage="$usage" -musage="$usage" "$mount" -v
|
||||
[[ $? -eq 0 ]] && df_print_success "Balance completed" || df_print_warning "Balance finished (may have been interrupted)"
|
||||
}
|
||||
|
||||
btrfs-balance-status() {
|
||||
_btrfs_check || return 1
|
||||
df_print_func_name "Btrfs Balance Status"
|
||||
sudo btrfs balance status "${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
}
|
||||
|
||||
btrfs-balance-cancel() {
|
||||
_btrfs_check || return 1
|
||||
df_print_step "Cancelling balance..."
|
||||
sudo btrfs balance cancel "${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
}
|
||||
|
||||
btrfs-scrub() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Scrub"
|
||||
local status=$(sudo btrfs scrub status "$mount" 2>/dev/null)
|
||||
if echo "$status" | grep -q "running"; then
|
||||
df_print_section "Scrub Status (running)"
|
||||
echo "$status" | sed 's/^/ /'
|
||||
return 0
|
||||
fi
|
||||
df_print_warning "Scrub verifies data integrity and may take hours"
|
||||
df_confirm "Start scrub?" || return 0
|
||||
df_print_step "Starting scrub..."
|
||||
sudo btrfs scrub start "$mount"
|
||||
echo ""
|
||||
df_print_section "Scrub Status"
|
||||
sudo btrfs scrub status "$mount"
|
||||
df_print_info "Monitor with: btrfs-scrub-status"
|
||||
}
|
||||
|
||||
btrfs-scrub-status() {
|
||||
_btrfs_check || return 1
|
||||
df_print_func_name "Btrfs Scrub Status"
|
||||
sudo btrfs scrub status "${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
}
|
||||
|
||||
btrfs-scrub-cancel() {
|
||||
_btrfs_check || return 1
|
||||
df_print_step "Cancelling scrub..."
|
||||
sudo btrfs scrub cancel "${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
}
|
||||
|
||||
btrfs-defrag() {
|
||||
_btrfs_check || return 1
|
||||
local target="${1:-.}"
|
||||
[[ ! -e "$target" ]] && { df_print_error "Target not found: $target"; return 1; }
|
||||
df_print_func_name "Btrfs Defragment"
|
||||
if [[ -d "$target" ]]; then
|
||||
df_print_warning "Recursive defrag on directory: $target"
|
||||
df_confirm "Continue?" || return 0
|
||||
sudo btrfs filesystem defragment -r -v "$target"
|
||||
else
|
||||
df_print_step "Defragmenting: $target"
|
||||
sudo btrfs filesystem defragment -v "$target"
|
||||
fi
|
||||
df_print_success "Defragmentation complete"
|
||||
}
|
||||
|
||||
btrfs-compress() {
|
||||
_btrfs_check || return 1
|
||||
df_require_cmd compsize || return 1
|
||||
df_print_func_name "Btrfs Compression Statistics"
|
||||
sudo compsize "${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
}
|
||||
|
||||
btrfs-info() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Filesystem Information"
|
||||
df_print_section "Filesystem Show"
|
||||
sudo btrfs filesystem show "$mount"
|
||||
echo ""
|
||||
df_print_section "Filesystem df"
|
||||
sudo btrfs filesystem df "$mount"
|
||||
echo ""
|
||||
df_print_section "Device Stats"
|
||||
sudo btrfs device stats "$mount"
|
||||
}
|
||||
|
||||
btrfs-health() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Health Check"
|
||||
local issues=0
|
||||
|
||||
df_print_section "Device Errors"
|
||||
local errors=$(sudo btrfs device stats "$mount" 2>/dev/null | grep -v " 0$" | grep -v "^$")
|
||||
[[ -z "$errors" ]] && df_print_indent "✓ No errors" || { df_print_indent "✗ Errors detected:"; echo "$errors" | sed 's/^/ /'; ((issues++)); }
|
||||
|
||||
echo ""
|
||||
df_print_section "Space Allocation"
|
||||
local used_pct=$(sudo btrfs filesystem usage "$mount" -b 2>/dev/null | grep "Used:" | head -1 | awk '{print $2}' | tr -d '%')
|
||||
if [[ -n "$used_pct" ]]; then
|
||||
(( used_pct >= 90 )) && { df_print_indent "✗ ${used_pct}% full - critical!"; ((issues++)); } || \
|
||||
(( used_pct >= 80 )) && df_print_indent "⚠ ${used_pct}% full" || df_print_indent "✓ ${used_pct}% used"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
df_print_section "Last Scrub"
|
||||
local scrub=$(sudo btrfs scrub status "$mount" 2>/dev/null)
|
||||
local scrub_date=$(echo "$scrub" | grep "Scrub started" | awk '{print $3, $4, $5}')
|
||||
[[ -n "$scrub_date" ]] && df_print_indent "Last: $scrub_date" || df_print_indent "⚠ No scrub run yet"
|
||||
|
||||
echo ""
|
||||
(( issues == 0 )) && df_print_success "Filesystem healthy" || df_print_error "Found $issues issue(s)"
|
||||
}
|
||||
|
||||
btrfs-snap-usage() {
|
||||
_btrfs_check || return 1
|
||||
df_print_func_name "Snapshot Disk Space Usage"
|
||||
if [[ -d "/.snapshots" ]]; then
|
||||
df_print_section "Snapshot Directory"
|
||||
local size=$(timeout 10 sudo du -sh /.snapshots 2>/dev/null | cut -f1)
|
||||
df_print_indent "${size:-Unable to calculate}"
|
||||
echo ""
|
||||
df_print_section "Individual Snapshots (top 10)"
|
||||
timeout 30 sudo du -sh /.snapshots/*/ 2>/dev/null | sort -h | tail -10 | sed 's/^/ /'
|
||||
else
|
||||
df_print_warning "No /.snapshots directory found"
|
||||
fi
|
||||
}
|
||||
|
||||
btrfs-maintain() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
df_print_func_name "Btrfs Maintenance Routine"
|
||||
echo "This will: health check, balance, scrub"
|
||||
df_confirm_warning "This may take several hours" || return 0
|
||||
df_print_step "Step 1/3: Health Check"
|
||||
btrfs-health "$mount"
|
||||
df_print_step "Step 2/3: Balance"
|
||||
sudo btrfs balance start -dusage=50 -musage=50 "$mount"
|
||||
df_print_step "Step 3/3: Scrub"
|
||||
sudo btrfs scrub start -B "$mount"
|
||||
df_print_success "Maintenance complete"
|
||||
}
|
||||
|
||||
btrfs-help() {
|
||||
df_print_func_name "Btrfs Helper Commands"
|
||||
cat << 'EOF'
|
||||
btrfs-usage [mount] Filesystem usage
|
||||
btrfs-subs [mount] List subvolumes
|
||||
btrfs-info [mount] Full filesystem info
|
||||
btrfs-health [mount] Quick health check
|
||||
btrfs-compress [path] Compression stats
|
||||
btrfs-balance [mount] Start balance
|
||||
btrfs-scrub [mount] Start scrub
|
||||
btrfs-defrag <path> Defragment
|
||||
btrfs-snap-usage Snapshot space usage
|
||||
btrfs-maintain [mount] Full maintenance
|
||||
EOF
|
||||
}
|
||||
|
||||
alias btru='btrfs-usage' btrs='btrfs-subs' btrh='btrfs-health' btri='btrfs-info' btrc='btrfs-compress'
|
||||
BIN
refactor_backup/zsh/functions/btrfs-helpers.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/btrfs-helpers.zsh.zwc
Normal file
Binary file not shown.
136
refactor_backup/zsh/functions/command-palette.zsh
Normal file
136
refactor_backup/zsh/functions/command-palette.zsh
Normal file
@@ -0,0 +1,136 @@
|
||||
# ============================================================================
|
||||
# Command Palette - Fuzzy Command Launcher for Zsh
|
||||
# ============================================================================
|
||||
# A Raycast/Alfred-style command palette for the terminal
|
||||
# Keybinding: Ctrl+Space (configurable)
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g PALETTE_HOTKEY="${PALETTE_HOTKEY:-^@}"
|
||||
typeset -g PALETTE_HISTORY_SIZE=50
|
||||
typeset -g PALETTE_BOOKMARKS_FILE="$HOME/.dotfiles/.bookmarks"
|
||||
typeset -g DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
|
||||
|
||||
typeset -g ICON_ALIAS="⚡" ICON_FUNC="λ" ICON_HIST="↺" ICON_DIR="📁"
|
||||
typeset -g ICON_SCRIPT="⚙" ICON_ACTION="★" ICON_GIT="⎇"
|
||||
|
||||
_palette_get_aliases() {
|
||||
alias | sed 's/^alias //' | while IFS='=' read -r 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"
|
||||
done
|
||||
}
|
||||
|
||||
_palette_get_functions() {
|
||||
print -l ${(ok)functions} | grep -v "^_" | while read -r name; do
|
||||
printf "%s\t%s\t%s\t%s\n" "$ICON_FUNC" "function" "$name" ""
|
||||
done
|
||||
}
|
||||
|
||||
_palette_get_history() {
|
||||
fc -ln -"$PALETTE_HISTORY_SIZE" 2>/dev/null | awk '!seen[$0]++' | while read -r cmd; do
|
||||
[[ -n "$cmd" ]] && printf "%s\t%s\t%s\t%s\n" "$ICON_HIST" "history" "$cmd" ""
|
||||
done
|
||||
}
|
||||
|
||||
_palette_get_bookmarks() {
|
||||
[[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && return
|
||||
while IFS='|' read -r name path desc; do
|
||||
[[ "$name" =~ ^# || -z "$name" ]] && continue
|
||||
printf "%s\t%s\t%s\t%s\n" "$ICON_DIR" "bookmark" "$name" "$path"
|
||||
done < "$PALETTE_BOOKMARKS_FILE"
|
||||
}
|
||||
|
||||
_palette_get_actions() {
|
||||
cat << 'EOF'
|
||||
★ action reload-shell Reload zsh configuration
|
||||
★ action edit-zshrc Edit ~/.zshrc
|
||||
★ action dotfiles-update Update dotfiles
|
||||
EOF
|
||||
df_in_git_repo && cat << 'EOF'
|
||||
⎇ git git-status Show git status
|
||||
⎇ git git-pull Pull latest
|
||||
⎇ git git-push Push commits
|
||||
EOF
|
||||
}
|
||||
|
||||
_palette_run_action() {
|
||||
case "$1" in
|
||||
reload-shell) source ~/.zshrc; df_print_success "Shell reloaded" ;;
|
||||
edit-zshrc) ${EDITOR:-vim} ~/.zshrc ;;
|
||||
dotfiles-update) cd "$DOTFILES_DIR" && git pull ;;
|
||||
git-status) git status ;;
|
||||
git-pull) git pull ;;
|
||||
git-push) git push ;;
|
||||
*) df_print_error "Unknown action: $1" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
palette() {
|
||||
df_require_cmd fzf || return 1
|
||||
local items=$(_palette_get_actions; _palette_get_aliases; _palette_get_functions; _palette_get_bookmarks; _palette_get_history)
|
||||
local sel=$(echo "$items" | fzf --ansi --delimiter='\t' --with-nth=1,3,4 $(df_fzf_opts) --prompt='> ')
|
||||
[[ -z "$sel" ]] && return
|
||||
local type=$(echo "$sel" | cut -f2) name=$(echo "$sel" | cut -f3) detail=$(echo "$sel" | cut -f4)
|
||||
case "$type" in
|
||||
alias|history) print -z "$name" ;;
|
||||
function) print -z "$name " ;;
|
||||
bookmark) cd "$detail" && pwd ;;
|
||||
action|git) _palette_run_action "$name" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
bookmark() {
|
||||
local cmd="${1:-list}"; shift 2>/dev/null
|
||||
case "$cmd" in
|
||||
add)
|
||||
local name="$1" path="${2:-$(pwd)}" desc="$3"
|
||||
[[ -z "$name" ]] && { echo "Usage: bookmark add <name> [path]"; return 1; }
|
||||
df_ensure_file "$PALETTE_BOOKMARKS_FILE" "# Bookmarks: name|path|description"
|
||||
grep -q "^${name}|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null && {
|
||||
df_confirm "Overwrite '$name'?" || return 1
|
||||
grep -v "^${name}|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp"
|
||||
mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE"
|
||||
}
|
||||
echo "${name}|${path}|${desc}" >> "$PALETTE_BOOKMARKS_FILE"
|
||||
df_print_success "Bookmarked: $name → $path"
|
||||
;;
|
||||
delete|rm)
|
||||
[[ -z "$1" ]] && { echo "Usage: bookmark delete <name>"; return 1; }
|
||||
grep -q "^${1}|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null || { df_print_error "Not found: $1"; return 1; }
|
||||
grep -v "^${1}|" "$PALETTE_BOOKMARKS_FILE" > "${PALETTE_BOOKMARKS_FILE}.tmp"
|
||||
mv "${PALETTE_BOOKMARKS_FILE}.tmp" "$PALETTE_BOOKMARKS_FILE"
|
||||
df_print_success "Deleted: $1"
|
||||
;;
|
||||
list|ls)
|
||||
df_print_func_name "Bookmarks"
|
||||
[[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && { df_print_info "No bookmarks"; return; }
|
||||
while IFS='|' read -r name path desc; do
|
||||
[[ "$name" =~ ^# || -z "$name" ]] && continue
|
||||
df_print_indent "● $name → $path"
|
||||
done < "$PALETTE_BOOKMARKS_FILE"
|
||||
;;
|
||||
go)
|
||||
[[ -z "$1" ]] && { echo "Usage: bookmark go <name>"; return 1; }
|
||||
local path=$(grep "^${1}|" "$PALETTE_BOOKMARKS_FILE" 2>/dev/null | cut -d'|' -f2)
|
||||
[[ -n "$path" ]] && cd "$path" || df_print_error "Not found: $1"
|
||||
;;
|
||||
*) echo "Usage: bookmark <add|delete|list|go>" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
bm() {
|
||||
df_require_cmd fzf || return 1
|
||||
[[ ! -f "$PALETTE_BOOKMARKS_FILE" ]] && { df_print_info "No bookmarks"; return 1; }
|
||||
local sel=$(grep -v "^#" "$PALETTE_BOOKMARKS_FILE" | grep -v "^$" | \
|
||||
fzf $(df_fzf_opts) --delimiter='|' --with-nth=1,2 --prompt='Bookmark > ')
|
||||
[[ -n "$sel" ]] && cd "$(echo "$sel" | cut -d'|' -f2)"
|
||||
}
|
||||
|
||||
_palette_widget() { BUFFER=""; zle redisplay; palette; zle reset-prompt; }
|
||||
zle -N _palette_widget
|
||||
bindkey "$PALETTE_HOTKEY" _palette_widget
|
||||
|
||||
alias p='palette' bml='bookmark list' bma='bookmark add' bmg='bookmark go'
|
||||
BIN
refactor_backup/zsh/functions/command-palette.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/command-palette.zsh.zwc
Normal file
Binary file not shown.
201
refactor_backup/zsh/functions/motd.zsh
Normal file
201
refactor_backup/zsh/functions/motd.zsh
Normal file
@@ -0,0 +1,201 @@
|
||||
#!/usr/bin/env zsh
|
||||
# ============================================================================
|
||||
# MOTD (Message of the Day) - Dynamic System Info
|
||||
# ============================================================================
|
||||
# Displays system information on shell startup
|
||||
# Optimized for Arch/CachyOS with direct /proc access
|
||||
#
|
||||
# Functions:
|
||||
# show_motd - Compact box format
|
||||
# show_motd_mini - Single line format
|
||||
# show_motd_full - Extended info
|
||||
# ============================================================================
|
||||
|
||||
# Only run in interactive shells
|
||||
[[ -o interactive ]] || return 0
|
||||
|
||||
# ============================================================================
|
||||
# Source Configuration
|
||||
# ============================================================================
|
||||
# utils.zsh sources config.zsh which sources dotfiles.conf
|
||||
# This gives us DF_WIDTH, MOTD_STYLE, colors, and all other settings
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null || {
|
||||
# Fallback if utils.zsh not available
|
||||
typeset -g DF_RESET=$'\033[0m' DF_BOLD=$'\033[1m' DF_DIM=$'\033[2m'
|
||||
typeset -g DF_BLUE=$'\033[38;5;39m' DF_CYAN=$'\033[38;5;51m'
|
||||
typeset -g DF_GREEN=$'\033[38;5;82m' DF_YELLOW=$'\033[38;5;220m'
|
||||
typeset -g DF_RED=$'\033[38;5;196m' DF_GREY=$'\033[38;5;242m' DF_NC=$'\033[0m'
|
||||
typeset -g DF_LIGHT_BLUE=$'\033[38;5;39m' DF_LIGHT_GREEN=$'\033[38;5;82m'
|
||||
typeset -g DF_WIDTH="${DF_WIDTH:-66}"
|
||||
typeset -g MOTD_STYLE="${MOTD_STYLE:-compact}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Optimized Info Gathering (using /proc directly)
|
||||
# ============================================================================
|
||||
|
||||
_motd_uptime() {
|
||||
local uptime_seconds=$(cut -d. -f1 /proc/uptime 2>/dev/null)
|
||||
[[ -z "$uptime_seconds" ]] && { echo "?"; return; }
|
||||
|
||||
local days=$((uptime_seconds / 86400))
|
||||
local hours=$(((uptime_seconds % 86400) / 3600))
|
||||
local mins=$(((uptime_seconds % 3600) / 60))
|
||||
|
||||
if (( days > 0 )); then
|
||||
echo "${days}d${hours}h"
|
||||
elif (( hours > 0 )); then
|
||||
echo "${hours}h${mins}m"
|
||||
else
|
||||
echo "${mins}m"
|
||||
fi
|
||||
}
|
||||
|
||||
_motd_load() { cut -d' ' -f1 /proc/loadavg 2>/dev/null || echo "?"; }
|
||||
|
||||
_motd_mem() {
|
||||
awk '/MemTotal/ {total=$2} /MemAvailable/ {avail=$2} END {
|
||||
if (total > 0) {
|
||||
used=(total-avail)/1024/1024
|
||||
total_gb=total/1024/1024
|
||||
printf "%.1fG/%.0fG", used, total_gb
|
||||
} else { print "N/A" }
|
||||
}' /proc/meminfo 2>/dev/null || echo "N/A"
|
||||
}
|
||||
|
||||
_motd_disk() { df -h / 2>/dev/null | awk 'NR==2 {print $3 "/" $2}' || echo "N/A"; }
|
||||
|
||||
_motd_failed_services() {
|
||||
local cache_file="/tmp/.motd-failed-${UID}"
|
||||
local cache_age=300
|
||||
if [[ -f "$cache_file" ]]; then
|
||||
local file_age=$(($(date +%s) - $(stat -c %Y "$cache_file" 2>/dev/null || echo 0)))
|
||||
(( file_age < cache_age )) && { cat "$cache_file"; return; }
|
||||
fi
|
||||
local count=$(systemctl --failed --no-pager --no-legend 2>/dev/null | wc -l)
|
||||
echo "$count" > "$cache_file" 2>/dev/null
|
||||
echo "$count"
|
||||
}
|
||||
|
||||
_motd_updates() { echo "${UPDATE_PKG_COUNT:-0}"; }
|
||||
|
||||
# ============================================================================
|
||||
# Box Drawing - Uses DF_WIDTH from config
|
||||
# ============================================================================
|
||||
|
||||
_motd_line() {
|
||||
local char="$1"
|
||||
local width="${DF_WIDTH:-66}"
|
||||
local line=""
|
||||
for ((i=0; i<width; i++)); do line+="$char"; done
|
||||
echo "$line"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Main Display Function (Compact)
|
||||
# ============================================================================
|
||||
|
||||
show_motd() {
|
||||
[[ -n "$_MOTD_SHOWN" && "$1" != "--force" ]] && return 0
|
||||
typeset -g _MOTD_SHOWN=1
|
||||
|
||||
local width="${DF_WIDTH:-66}"
|
||||
local hostname="${HOST:-$(hostname -s 2>/dev/null)}"
|
||||
local datetime=$(date '+%a %b %d %H:%M')
|
||||
local uptime=$(_motd_uptime)
|
||||
local load=$(_motd_load)
|
||||
local mem=$(_motd_mem)
|
||||
local disk=$(_motd_disk)
|
||||
local hline=$(_motd_line '═')
|
||||
local inner=$((width - 2))
|
||||
|
||||
echo ""
|
||||
echo "${DF_GREY}╒${hline}╕${DF_NC}"
|
||||
|
||||
local h_left="✦ ${hostname}"
|
||||
local h_center=$(hostname -i 2>/dev/null | awk -F" " '{print $1}')
|
||||
local h_right="${datetime}"
|
||||
local h_pad=$(((inner - ${#h_left} - ${#h_center} - ${#h_right}) / 2))
|
||||
local h_spaces=""
|
||||
for ((i=0; i<h_pad; i++)); do h_spaces+=" "; done
|
||||
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_LIGHT_BLUE}${h_left}${DF_NC}${h_spaces}${DF_YELLOW}${h_center}${h_spaces}${DF_NC}${DF_BOLD}${h_right}${DF_NC} ${DF_GREY}│${DF_NC}"
|
||||
else
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_RED}${h_left}${DF_NC}${h_spaces}${DF_YELLOW}${h_center}${h_spaces}${DF_NC}${DF_BOLD}${h_right}${DF_NC} ${DF_GREY}│${DF_NC}"
|
||||
fi
|
||||
|
||||
echo "${DF_GREY}╘${hline}╛${DF_NC}"
|
||||
|
||||
local s1="${DF_GREY}「${DF_YELLOW}▲ ${DF_NC}${uptime}${DF_GREY}」"
|
||||
local s2="${DF_GREY}「${DF_CYAN}◆ ${DF_NC}${load}${DF_GREY}」"
|
||||
local s3="${DF_GREY}「${DF_GREEN}▪ ${DF_NC}${mem}${DF_GREY}」"
|
||||
local s4="${DF_GREY}「${DF_BLUE}● ${DF_NC}${disk}${DF_GREY}」"
|
||||
|
||||
echo " ${s1} ${s2} ${s3} ${s4}"
|
||||
|
||||
# Failed services warning
|
||||
if [[ "${MOTD_SHOW_FAILED_SERVICES:-true}" == "true" ]]; then
|
||||
local failed=$(_motd_failed_services)
|
||||
(( failed > 0 )) && echo " ${DF_RED}⚠${DF_NC} ${failed} failed service(s)"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Mini Format (Single Line)
|
||||
# ============================================================================
|
||||
|
||||
show_motd_mini() {
|
||||
local hostname="${HOST:-$(hostname -s 2>/dev/null)}"
|
||||
local uptime=$(_motd_uptime)
|
||||
local load=$(_motd_load)
|
||||
echo "${DF_LIGHT_BLUE}${hostname}${DF_NC} │ up ${uptime} │ load ${load}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Full Format (Extended Info)
|
||||
# ============================================================================
|
||||
|
||||
show_motd_full() {
|
||||
show_motd --force
|
||||
|
||||
local width="${DF_WIDTH:-66}"
|
||||
|
||||
echo "${DF_CYAN}System Details${DF_NC}"
|
||||
printf "${DF_GREY}─%.0s${DF_NC}" $(seq 1 $width); echo ""
|
||||
|
||||
echo " ${DF_DIM}Kernel:${DF_NC} $(uname -r)"
|
||||
echo " ${DF_DIM}Shell:${DF_NC} ${SHELL##*/} ${ZSH_VERSION:-}"
|
||||
|
||||
if [[ -f /etc/os-release ]]; then
|
||||
local distro=$(grep '^PRETTY_NAME=' /etc/os-release | cut -d'"' -f2)
|
||||
echo " ${DF_DIM}OS:${DF_NC} ${distro}"
|
||||
fi
|
||||
|
||||
if command -v pacman &>/dev/null; then
|
||||
local pkg_count=$(pacman -Q 2>/dev/null | wc -l)
|
||||
echo " ${DF_DIM}Packages:${DF_NC} ${pkg_count}"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Auto-display based on MOTD_STYLE from config
|
||||
# ============================================================================
|
||||
|
||||
_motd_auto() {
|
||||
case "${MOTD_STYLE:-compact}" in
|
||||
full) show_motd_full ;;
|
||||
mini) show_motd_mini ;;
|
||||
none|off|false) ;;
|
||||
*) show_motd ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Run on source if ENABLE_MOTD is true
|
||||
[[ "${ENABLE_MOTD:-true}" == "true" ]] && _motd_auto
|
||||
BIN
refactor_backup/zsh/functions/motd.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/motd.zsh.zwc
Normal file
Binary file not shown.
84
refactor_backup/zsh/functions/password-manager.zsh
Normal file
84
refactor_backup/zsh/functions/password-manager.zsh
Normal file
@@ -0,0 +1,84 @@
|
||||
# ============================================================================
|
||||
# Password Manager Integration (LastPass CLI)
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g PW_CLIP_TIME="${PW_CLIP_TIME:-45}"
|
||||
|
||||
_pw_check() {
|
||||
df_require_cmd lpass lastpass-cli || return 1
|
||||
if ! lpass status -q 2>/dev/null; then
|
||||
df_print_warning "Not logged in"
|
||||
df_print_step "Logging in..."
|
||||
lpass login --trust "${LPASS_USER:-}" || { df_print_error "Login failed"; return 1; }
|
||||
fi
|
||||
}
|
||||
|
||||
_pw_copy() {
|
||||
local text="$1" label="${2:-Password}"
|
||||
if df_cmd_exists wl-copy; then
|
||||
echo -n "$text" | wl-copy
|
||||
elif df_cmd_exists xclip; then
|
||||
echo -n "$text" | xclip -selection clipboard
|
||||
else
|
||||
df_print_error "No clipboard tool (install wl-clipboard or xclip)"
|
||||
return 1
|
||||
fi
|
||||
df_print_success "$label copied (clears in ${PW_CLIP_TIME}s)"
|
||||
(sleep "$PW_CLIP_TIME" && { wl-copy "" 2>/dev/null || xclip -selection clipboard < /dev/null 2>/dev/null; }) &
|
||||
}
|
||||
|
||||
pw() {
|
||||
local cmd="${1:-search}"
|
||||
case "$cmd" in
|
||||
login) lpass login --trust "${LPASS_USER:-}" ;;
|
||||
logout) lpass logout -f; df_print_success "Logged out" ;;
|
||||
sync) _pw_check || return 1; df_print_step "Syncing..."; lpass sync; df_print_success "Synced" ;;
|
||||
show) _pw_check || return 1; [[ -z "$2" ]] && { echo "Usage: pw show <entry>"; return 1; }; lpass show "$2" ;;
|
||||
gen|generate)
|
||||
local len="${2:-20}"
|
||||
local pass=$(tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c "$len")
|
||||
_pw_copy "$pass" "Generated password"
|
||||
;;
|
||||
list|ls) _pw_check || return 1; df_print_func_name "Password Entries"; lpass ls --long ;;
|
||||
search|*)
|
||||
_pw_check || return 1
|
||||
local query="$1"; [[ "$cmd" == "search" ]] && query="$2"
|
||||
if df_cmd_exists fzf && [[ -z "$query" ]]; then
|
||||
local entry=$(lpass ls --format "%an (%au) [%ai]" 2>/dev/null | fzf $(df_fzf_opts) --prompt='Password > ')
|
||||
[[ -z "$entry" ]] && return
|
||||
local id=$(echo "$entry" | grep -oP '\[\K[^\]]+(?=\]$)')
|
||||
local pass=$(lpass show --password "$id" 2>/dev/null)
|
||||
[[ -n "$pass" ]] && _pw_copy "$pass" || df_print_error "Could not retrieve"
|
||||
else
|
||||
[[ -z "$query" ]] && { echo "Usage: pw <search-term>"; return 1; }
|
||||
local results=$(lpass ls --format "%an [%ai]" 2>/dev/null | grep -i "$query")
|
||||
local count=$(echo "$results" | grep -c . 2>/dev/null || echo 0)
|
||||
if (( count == 0 )); then
|
||||
df_print_warning "No entries for: $query"
|
||||
elif (( count == 1 )); then
|
||||
local id=$(echo "$results" | grep -oP '\[\K[^\]]+(?=\]$)')
|
||||
_pw_copy "$(lpass show --password "$id" 2>/dev/null)"
|
||||
else
|
||||
df_print_warning "Multiple entries:"
|
||||
echo "$results" | while read -r l; do df_print_indent "$l"; done
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
help|--help|-h)
|
||||
df_print_func_name "Password Manager"
|
||||
cat << 'EOF'
|
||||
pw <search> Search and copy password
|
||||
pw show <n> Show entry details
|
||||
pw list List all entries
|
||||
pw gen [len] Generate password (default: 20)
|
||||
pw sync Sync vault
|
||||
pw login/logout Auth commands
|
||||
EOF
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
alias pwc='pw' pws='pw show' pwg='pw gen' pwl='pw list'
|
||||
BIN
refactor_backup/zsh/functions/password-manager.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/password-manager.zsh.zwc
Normal file
Binary file not shown.
173
refactor_backup/zsh/functions/python-templates.zsh
Normal file
173
refactor_backup/zsh/functions/python-templates.zsh
Normal file
@@ -0,0 +1,173 @@
|
||||
# ============================================================================
|
||||
# Python Project Template Functions
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g PY_PYTHON="${PY_PYTHON:-python3}"
|
||||
typeset -g PY_VENV="${PY_VENV:-venv}"
|
||||
typeset -g PY_GIT_INIT="${PY_GIT_INIT:-true}"
|
||||
|
||||
_py_check_name() {
|
||||
[[ -z "$1" ]] && { df_print_warning "Project name required"; return 1; }
|
||||
[[ -d "$1" ]] && { df_print_warning "Directory '$1' exists"; return 1; }
|
||||
return 0
|
||||
}
|
||||
|
||||
_py_venv() {
|
||||
df_print_step "Creating virtual environment"
|
||||
"$PY_PYTHON" -m venv "$1/$PY_VENV"
|
||||
df_print_success "Created: $PY_VENV"
|
||||
}
|
||||
|
||||
_py_gitignore() {
|
||||
cat > "$1/.gitignore" << 'EOF'
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*.so
|
||||
build/
|
||||
dist/
|
||||
*.egg-info/
|
||||
venv/
|
||||
.venv/
|
||||
.env
|
||||
*.log
|
||||
.pytest_cache/
|
||||
.mypy_cache/
|
||||
EOF
|
||||
df_print_success "Created .gitignore"
|
||||
}
|
||||
|
||||
_py_git() {
|
||||
[[ "$PY_GIT_INIT" == "true" ]] && { cd "$1"; git init; git add .; git commit -m "Initial commit"; df_print_success "Git initialized"; }
|
||||
}
|
||||
|
||||
_py_next() {
|
||||
echo ""
|
||||
df_print_section "Next steps"
|
||||
df_print_indent "cd $1"
|
||||
df_print_indent "source $PY_VENV/bin/activate"
|
||||
}
|
||||
|
||||
py-new() {
|
||||
_py_check_name "$1" || return 1
|
||||
df_print_func_name "Python Project: $1"
|
||||
mkdir -p "$1"/{src,tests}
|
||||
touch "$1/src/__init__.py" "$1/tests/__init__.py"
|
||||
cat > "$1/src/main.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
def main():
|
||||
print("Hello!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
EOF
|
||||
echo "# Dependencies" > "$1/requirements.txt"
|
||||
_py_venv "$1"; _py_gitignore "$1"; _py_git "$1"
|
||||
df_print_success "Created: $1"
|
||||
_py_next "$1"
|
||||
}
|
||||
|
||||
py-flask() {
|
||||
_py_check_name "$1" || return 1
|
||||
df_print_func_name "Flask Project: $1"
|
||||
mkdir -p "$1"/{app/{templates,static},tests}
|
||||
_py_venv "$1"
|
||||
df_print_step "Installing Flask"
|
||||
"$1/$PY_VENV/bin/pip" install flask -q
|
||||
cat > "$1/app/__init__.py" << 'EOF'
|
||||
from flask import Flask
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
from app.routes import main
|
||||
app.register_blueprint(main)
|
||||
return app
|
||||
EOF
|
||||
cat > "$1/app/routes.py" << 'EOF'
|
||||
from flask import Blueprint, render_template
|
||||
main = Blueprint('main', __name__)
|
||||
@main.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
EOF
|
||||
echo '<!DOCTYPE html><html><body><h1>Flask</h1></body></html>' > "$1/app/templates/index.html"
|
||||
cat > "$1/app.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
from app import create_app
|
||||
app = create_app()
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
EOF
|
||||
echo "Flask>=3.0.0" > "$1/requirements.txt"
|
||||
_py_gitignore "$1"; _py_git "$1"
|
||||
df_print_success "Created: $1"
|
||||
_py_next "$1"
|
||||
}
|
||||
|
||||
py-fastapi() {
|
||||
_py_check_name "$1" || return 1
|
||||
df_print_func_name "FastAPI Project: $1"
|
||||
mkdir -p "$1"/{app,tests}
|
||||
_py_venv "$1"
|
||||
df_print_step "Installing FastAPI"
|
||||
"$1/$PY_VENV/bin/pip" install fastapi uvicorn -q
|
||||
cat > "$1/app/main.py" << 'EOF'
|
||||
from fastapi import FastAPI
|
||||
app = FastAPI()
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {"message": "Hello"}
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
EOF
|
||||
cat > "$1/run.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
import uvicorn
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True)
|
||||
EOF
|
||||
echo -e "fastapi>=0.104.0\nuvicorn>=0.24.0" > "$1/requirements.txt"
|
||||
_py_gitignore "$1"; _py_git "$1"
|
||||
df_print_success "Created: $1"
|
||||
df_print_info "Docs: http://localhost:8000/docs"
|
||||
_py_next "$1"
|
||||
}
|
||||
|
||||
py-cli() {
|
||||
_py_check_name "$1" || return 1
|
||||
df_print_func_name "CLI Project: $1"
|
||||
mkdir -p "$1"/{src/$1,tests}
|
||||
_py_venv "$1"
|
||||
df_print_step "Installing click"
|
||||
"$1/$PY_VENV/bin/pip" install click -q
|
||||
echo '__version__ = "0.1.0"' > "$1/src/$1/__init__.py"
|
||||
cat > "$1/src/$1/cli.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
import click
|
||||
@click.group()
|
||||
@click.version_option()
|
||||
def cli():
|
||||
pass
|
||||
@cli.command()
|
||||
@click.argument('name', default='World')
|
||||
def greet(name):
|
||||
click.echo(f"Hello, {name}!")
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
EOF
|
||||
echo "click>=8.1.0" > "$1/requirements.txt"
|
||||
_py_gitignore "$1"; _py_git "$1"
|
||||
df_print_success "Created: $1"
|
||||
df_print_info "Install: pip install -e $1"
|
||||
_py_next "$1"
|
||||
}
|
||||
|
||||
venv() {
|
||||
[[ -d "venv" ]] && source venv/bin/activate && return
|
||||
[[ -d ".venv" ]] && source .venv/bin/activate && return
|
||||
df_print_error "No venv found"
|
||||
}
|
||||
|
||||
alias pynew='py-new' pyflask='py-flask' pyfast='py-fastapi' pycli='py-cli'
|
||||
BIN
refactor_backup/zsh/functions/python-templates.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/python-templates.zsh.zwc
Normal file
Binary file not shown.
82
refactor_backup/zsh/functions/smart-suggest.zsh
Normal file
82
refactor_backup/zsh/functions/smart-suggest.zsh
Normal file
@@ -0,0 +1,82 @@
|
||||
# ============================================================================
|
||||
# Smart Command Suggestions for Zsh
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g SMART_SUGGEST_ENABLED=true
|
||||
typeset -g SMART_SUGGEST_TRACK_FILE="$HOME/.cache/smart-suggest-track"
|
||||
|
||||
typeset -gA TYPO_CORRECTIONS=(
|
||||
[gti]="git" [gitt]="git" [got]="git" [gi]="git"
|
||||
[gitst]="git st" [gits]="git s" [gitp]="git p"
|
||||
[psuh]="push" [psull]="pull" [pul]="pull"
|
||||
[stauts]="status" [comit]="commit" [commti]="commit"
|
||||
[chekcout]="checkout" [branhc]="branch" [marge]="merge"
|
||||
[dokcer]="docker" [doker]="docker" [dcoker]="docker"
|
||||
[sl]="ls" [sls]="ls" [cta]="cat" [grpe]="grep" [gerp]="grep"
|
||||
[mkdri]="mkdir" [chmdo]="chmod" [suod]="sudo" [sduo]="sudo"
|
||||
[pytohn]="python" [pyhton]="python" [ndoe]="node"
|
||||
[vmi]="vim" [cde]="code" [clera]="clear" [exti]="exit"
|
||||
)
|
||||
|
||||
typeset -gA COMMAND_PACKAGES=(
|
||||
[htop]="htop" [tree]="tree" [jq]="jq" [fd]="fd" [rg]="ripgrep"
|
||||
[bat]="bat" [eza]="eza" [fzf]="fzf" [tldr]="tldr" [ncdu]="ncdu"
|
||||
[lazygit]="lazygit" [neofetch]="neofetch" [delta]="git-delta"
|
||||
)
|
||||
|
||||
_ss_track() {
|
||||
local cmd="$1"
|
||||
[[ ${#cmd} -lt 8 ]] && return
|
||||
df_ensure_dir "$(dirname "$SMART_SUGGEST_TRACK_FILE")"
|
||||
echo "$cmd" >> "$SMART_SUGGEST_TRACK_FILE"
|
||||
local count=$(grep -Fc "$cmd" "$SMART_SUGGEST_TRACK_FILE" 2>/dev/null || echo 0)
|
||||
if (( count >= 10 && count % 10 == 0 )); then
|
||||
local existing=$(alias | grep -F "='$cmd'" | head -1 | cut -d= -f1)
|
||||
[[ -n "$existing" ]] && df_print_info "You have alias: $existing" || \
|
||||
df_print_info "Consider: alias xyz='$cmd'"
|
||||
fi
|
||||
}
|
||||
|
||||
command_not_found_handler() {
|
||||
local cmd="$1"; shift
|
||||
[[ "$SMART_SUGGEST_ENABLED" != true ]] && { echo "zsh: command not found: $cmd"; return 127; }
|
||||
|
||||
df_print_error "Command not found: $cmd"
|
||||
|
||||
local correction="${TYPO_CORRECTIONS[$cmd]}"
|
||||
[[ -n "$correction" ]] && { df_print_info "Did you mean: $correction?"; df_print_indent "Run: $correction $@"; }
|
||||
|
||||
local pkg="${COMMAND_PACKAGES[$cmd]}"
|
||||
[[ -n "$pkg" ]] && df_print_info "Install: sudo pacman -S $pkg"
|
||||
|
||||
return 127
|
||||
}
|
||||
|
||||
fuck() {
|
||||
local last=$(fc -ln -1 2>/dev/null | sed 's/^[[:space:]]*//')
|
||||
local first="${last%% *}"
|
||||
local fix="${TYPO_CORRECTIONS[$first]}"
|
||||
[[ -n "$fix" ]] && { df_print_step "Running: ${last/$first/$fix}"; eval "${last/$first/$fix}"; } || df_print_warning "No fix for: $last"
|
||||
}
|
||||
|
||||
_ss_preexec() { _ss_track "$1"; }
|
||||
_ss_precmd() {
|
||||
local exit=$?; (( exit == 0 )) && return
|
||||
local last=$(fc -ln -1 2>/dev/null | sed 's/^[[:space:]]*//')
|
||||
[[ "${last%% *}" == "git" ]] && {
|
||||
local sub=$(echo "$last" | awk '{print $2}')
|
||||
local fix="${TYPO_CORRECTIONS[$sub]}"
|
||||
[[ -n "$fix" ]] && df_print_info "Did you mean: git $fix?"
|
||||
}
|
||||
}
|
||||
|
||||
_ss_setup() {
|
||||
autoload -Uz add-zsh-hook
|
||||
add-zsh-hook preexec _ss_preexec
|
||||
add-zsh-hook precmd _ss_precmd
|
||||
}
|
||||
|
||||
[[ "$SMART_SUGGEST_ENABLED" == true ]] && _ss_setup
|
||||
BIN
refactor_backup/zsh/functions/smart-suggest.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/smart-suggest.zsh.zwc
Normal file
Binary file not shown.
98
refactor_backup/zsh/functions/snapper.zsh
Normal file
98
refactor_backup/zsh/functions/snapper.zsh
Normal file
@@ -0,0 +1,98 @@
|
||||
# ============================================================================
|
||||
# Snapper Snapshot Functions for CachyOS/Arch with limine-snapper-sync
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
snap-create() {
|
||||
local desc="$*"
|
||||
local limine="/boot/limine.conf"
|
||||
|
||||
df_print_func_name "Snapper Snapshot Creation"
|
||||
|
||||
if [[ -z "$desc" ]]; then
|
||||
df_print_warning "No description"
|
||||
echo -n "Description: "; read desc
|
||||
[[ -z "$desc" ]] && { df_print_error "Required"; return 1; }
|
||||
fi
|
||||
|
||||
[[ ! -f "$limine" ]] && { df_print_error "Limine not found: $limine"; return 1; }
|
||||
|
||||
df_print_step "Checking limine.conf before snapshot"
|
||||
local before=$(sudo grep -cP "^\\s*///\\d+\\s*│" "$limine" || echo "0")
|
||||
df_print_success "Before: $before entries"
|
||||
|
||||
df_print_step "Creating snapshot: \"$desc\""
|
||||
local num=$(sudo snapper -c root create --description "$desc" --print-number)
|
||||
[[ -z "$num" ]] && { df_print_error "Failed"; return 1; }
|
||||
df_print_success "Created: #$num"
|
||||
|
||||
df_print_step "Triggering limine-snapper-sync..."
|
||||
sudo systemctl start limine-snapper-sync.service && df_print_success "Triggered" || df_print_warning "May run automatically"
|
||||
sleep 2
|
||||
|
||||
df_print_step "Validating"
|
||||
local after=$(sudo grep -cP "^\\s*///\\d+\\s*│" "$limine" || echo "0")
|
||||
|
||||
if sudo grep -qP "^\\s*///$num\\s*│" "$limine"; then
|
||||
df_print_success "Snapshot #$num in limine.conf"
|
||||
(( after > before )) && df_print_success "Added $((after - before)) entry"
|
||||
else
|
||||
df_print_error "Snapshot #$num NOT in limine.conf"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
df_print_section "Summary"
|
||||
df_print_indent "Number: #$num"
|
||||
df_print_indent "Description: $desc"
|
||||
}
|
||||
|
||||
snap-list() {
|
||||
local count="${1:-10}"
|
||||
df_print_func_name "Snapper Snapshots (last $count)"
|
||||
sudo snapper -c root list | tail -n "$((count + 1))"
|
||||
}
|
||||
|
||||
snap-show() {
|
||||
[[ -z "$1" ]] && { echo "Usage: snap-show <num>"; return 1; }
|
||||
df_print_func_name "Snapshot #$1"
|
||||
sudo snapper -c root list | grep "^\s*$1\s"
|
||||
echo ""
|
||||
df_print_section "In limine.conf"
|
||||
sudo grep -qP "^\\s*///$1\\s*│" /boot/limine.conf && \
|
||||
sudo grep -P "^\\s*///$1\\s*│" /boot/limine.conf || df_print_warning "Not found"
|
||||
}
|
||||
|
||||
snap-delete() {
|
||||
[[ -z "$1" ]] && { echo "Usage: snap-delete <num>"; return 1; }
|
||||
df_print_func_name "Delete Snapshot #$1"
|
||||
|
||||
local before=$(sudo grep -cP "^\\s*///\\d+\\s*│" /boot/limine.conf || echo "0")
|
||||
sudo snapper -c root delete "$1" && df_print_success "Deleted #$1" || { df_print_error "Failed"; return 1; }
|
||||
|
||||
df_print_step "Syncing limine..."
|
||||
sudo systemctl start limine-snapper-sync.service; sleep 2
|
||||
|
||||
sudo grep -qP "^\\s*///$1\\s*│" /boot/limine.conf && df_print_error "Still in limine!" || df_print_success "Removed from limine"
|
||||
}
|
||||
|
||||
snap-sync() {
|
||||
df_print_func_name "Limine-Snapper-Sync"
|
||||
df_print_step "Triggering sync..."
|
||||
sudo systemctl start limine-snapper-sync.service && { sleep 2; df_print_success "Done"; } || df_print_error "Failed"
|
||||
}
|
||||
|
||||
snap-check() {
|
||||
df_print_func_name "Limine Snapshot Entries"
|
||||
local latest=$(sudo snapper -c root list | tail -n +3 | grep -v "^\s*0\s" | tail -1 | awk '{print $1}')
|
||||
[[ -z "$latest" ]] && { df_print_warning "No snapshots"; return 1; }
|
||||
df_print_info "Latest: #$latest"
|
||||
sudo grep -qP "^\\s*///$latest\\s*│" /boot/limine.conf && \
|
||||
df_print_success "Latest in limine.conf" || df_print_error "Latest NOT in limine.conf"
|
||||
local count=$(sudo grep -cP "^\\s*///\\d+\\s*│" /boot/limine.conf || echo "0")
|
||||
df_print_info "Total entries: $count"
|
||||
}
|
||||
|
||||
alias snap='snap-create' snapls='snap-list' snaprm='snap-delete' snapcheck='snap-check'
|
||||
BIN
refactor_backup/zsh/functions/snapper.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/snapper.zsh.zwc
Normal file
Binary file not shown.
96
refactor_backup/zsh/functions/ssh-manager.zsh
Normal file
96
refactor_backup/zsh/functions/ssh-manager.zsh
Normal file
@@ -0,0 +1,96 @@
|
||||
# ============================================================================
|
||||
# SSH Session Manager with Tmux Integration
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g SSH_PROFILES_FILE="${SSH_PROFILES_FILE:-$HOME/.dotfiles/.ssh-profiles}"
|
||||
typeset -g SSH_AUTO_TMUX="${SSH_AUTO_TMUX:-true}"
|
||||
typeset -g SSH_TMUX_PREFIX="${SSH_TMUX_PREFIX:-ssh}"
|
||||
|
||||
_ssh_init() {
|
||||
df_ensure_file "$SSH_PROFILES_FILE" "# SSH Profiles: name|user@host|port|key|options|description"
|
||||
}
|
||||
|
||||
_ssh_parse() {
|
||||
local line=$(grep "^${1}|" "$SSH_PROFILES_FILE" 2>/dev/null | head -1)
|
||||
[[ -z "$line" ]] && return 1
|
||||
echo "$line" | cut -d'|' -f2-
|
||||
}
|
||||
|
||||
ssh-save() {
|
||||
local name="$1" conn="$2" port="${3:-22}" key="${4:-}" opts="${5:-}" desc="${6:-}"
|
||||
[[ -z "$name" || -z "$conn" ]] && { echo "Usage: ssh-save <n> <user@host> [port] [key] [opts] [desc]"; return 1; }
|
||||
_ssh_init
|
||||
grep -q "^${name}|" "$SSH_PROFILES_FILE" 2>/dev/null && {
|
||||
df_confirm "Overwrite '$name'?" || return 1
|
||||
grep -v "^${name}|" "$SSH_PROFILES_FILE" > "${SSH_PROFILES_FILE}.tmp"
|
||||
mv "${SSH_PROFILES_FILE}.tmp" "$SSH_PROFILES_FILE"
|
||||
}
|
||||
echo "${name}|${conn}|${port}|${key}|${opts}|${desc}" >> "$SSH_PROFILES_FILE"
|
||||
df_print_success "Saved: $name → $conn"
|
||||
}
|
||||
|
||||
ssh-list() {
|
||||
_ssh_init
|
||||
df_print_func_name "SSH Profiles"
|
||||
local found=false
|
||||
while IFS='|' read -r name conn port key opts desc; do
|
||||
[[ "$name" =~ ^# || -z "$name" ]] && continue
|
||||
found=true
|
||||
df_print_indent "● $name → $conn"
|
||||
[[ "$port" != "22" && -n "$port" ]] && df_print_indent " Port: $port"
|
||||
[[ -n "$desc" ]] && df_print_indent " $desc"
|
||||
done < "$SSH_PROFILES_FILE"
|
||||
[[ "$found" != true ]] && df_print_info "No profiles. Use: ssh-save name user@host"
|
||||
}
|
||||
|
||||
ssh-delete() {
|
||||
[[ -z "$1" ]] && { echo "Usage: ssh-delete <n>"; return 1; }
|
||||
grep -q "^${1}|" "$SSH_PROFILES_FILE" 2>/dev/null || { df_print_error "Not found: $1"; return 1; }
|
||||
grep -v "^${1}|" "$SSH_PROFILES_FILE" > "${SSH_PROFILES_FILE}.tmp"
|
||||
mv "${SSH_PROFILES_FILE}.tmp" "$SSH_PROFILES_FILE"
|
||||
df_print_success "Deleted: $1"
|
||||
}
|
||||
|
||||
ssh-connect() {
|
||||
local name="$1" session="${2:-${SSH_TMUX_PREFIX}-${1}}"
|
||||
[[ -z "$name" ]] && { ssh-list; return 1; }
|
||||
_ssh_init
|
||||
local data=$(_ssh_parse "$name")
|
||||
[[ -z "$data" ]] && { df_print_error "Not found: $name"; return 1; }
|
||||
|
||||
IFS='|' read -r conn port key opts desc <<< "$data"
|
||||
df_print_step "Connecting: $name"
|
||||
[[ -n "$desc" ]] && df_print_indent "$desc"
|
||||
|
||||
local cmd="ssh"
|
||||
[[ -n "$port" && "$port" != "22" ]] && cmd="$cmd -p $port"
|
||||
[[ -n "$key" ]] && cmd="$cmd -i $key"
|
||||
[[ -n "$opts" ]] && cmd="$cmd $opts"
|
||||
cmd="$cmd $conn"
|
||||
|
||||
if [[ "$SSH_AUTO_TMUX" == "true" ]]; then
|
||||
df_print_info "Tmux session: $session"
|
||||
eval "$cmd -t 'tmux attach -t $session 2>/dev/null || tmux new -s $session'"
|
||||
else
|
||||
eval "$cmd"
|
||||
fi
|
||||
}
|
||||
|
||||
sshf() {
|
||||
df_require_cmd fzf || return 1
|
||||
_ssh_init
|
||||
local profiles=()
|
||||
while IFS='|' read -r name conn port key opts desc; do
|
||||
[[ "$name" =~ ^# || -z "$name" ]] && continue
|
||||
profiles+=("$name|$name → $conn")
|
||||
done < "$SSH_PROFILES_FILE"
|
||||
[[ ${#profiles[@]} -eq 0 ]] && { df_print_info "No profiles"; return 1; }
|
||||
local sel=$(printf '%s\n' "${profiles[@]}" | fzf $(df_fzf_opts) --delimiter='|' --with-nth=2 --prompt='SSH > ')
|
||||
[[ -n "$sel" ]] && ssh-connect "${sel%%|*}"
|
||||
}
|
||||
|
||||
alias sshl='ssh-list' sshs='ssh-save' sshc='ssh-connect' sshd='ssh-delete'
|
||||
_ssh_init
|
||||
BIN
refactor_backup/zsh/functions/ssh-manager.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/ssh-manager.zsh.zwc
Normal file
Binary file not shown.
124
refactor_backup/zsh/functions/systemd-helpers.zsh
Normal file
124
refactor_backup/zsh/functions/systemd-helpers.zsh
Normal file
@@ -0,0 +1,124 @@
|
||||
# ============================================================================
|
||||
# Systemd Integration for Arch/CachyOS
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
# Core shortcuts
|
||||
sc() { sudo systemctl "$@"; }
|
||||
scu() { systemctl --user "$@"; }
|
||||
|
||||
scr() {
|
||||
[[ -z "$1" ]] && { echo "Usage: scr <service>"; return 1; }
|
||||
df_print_step "Restarting $1..."
|
||||
sudo systemctl restart "$1" && { df_print_success "Restarted"; sudo systemctl status "$1" --no-pager -l; } || df_print_error "Failed"
|
||||
}
|
||||
|
||||
sce() {
|
||||
[[ -z "$1" ]] && { echo "Usage: sce <service>"; return 1; }
|
||||
df_print_step "Enabling $1..."
|
||||
sudo systemctl enable --now "$1" && { df_print_success "Enabled"; sudo systemctl status "$1" --no-pager -l | head -15; } || df_print_error "Failed"
|
||||
}
|
||||
|
||||
scd() {
|
||||
[[ -z "$1" ]] && { echo "Usage: scd <service>"; return 1; }
|
||||
df_print_step "Disabling $1..."
|
||||
sudo systemctl disable --now "$1" && df_print_success "Disabled" || df_print_error "Failed"
|
||||
}
|
||||
|
||||
sclog() {
|
||||
[[ -z "$1" ]] && { echo "Usage: sclog <service>"; return 1; }
|
||||
df_print_step "Following logs for $1 (Ctrl+C to exit)..."
|
||||
sudo journalctl -xeu "$1" -f -n "${2:-50}"
|
||||
}
|
||||
|
||||
sclogs() {
|
||||
[[ -z "$1" ]] && { echo "Usage: sclogs <service>"; return 1; }
|
||||
sudo journalctl -xeu "$1" -n "${2:-50}" --no-pager
|
||||
}
|
||||
|
||||
sc-failed() {
|
||||
df_print_func_name "Failed Services"
|
||||
df_print_section "System"
|
||||
local sys=$(systemctl --failed --no-pager --no-legend 2>/dev/null)
|
||||
[[ -z "$sys" ]] && df_print_indent "✓ None" || echo "$sys" | sed 's/^/ /'
|
||||
echo ""
|
||||
df_print_section "User"
|
||||
local usr=$(systemctl --user --failed --no-pager --no-legend 2>/dev/null)
|
||||
[[ -z "$usr" ]] && df_print_indent "✓ None" || echo "$usr" | sed 's/^/ /'
|
||||
}
|
||||
|
||||
sc-timers() {
|
||||
df_print_func_name "Active Timers"
|
||||
df_print_section "System"
|
||||
systemctl list-timers --no-pager | head -15
|
||||
echo ""
|
||||
df_print_section "User"
|
||||
systemctl --user list-timers --no-pager 2>/dev/null | head -10
|
||||
}
|
||||
|
||||
sc-boot() {
|
||||
df_print_func_name "Boot Analysis"
|
||||
df_print_section "Summary"
|
||||
systemd-analyze
|
||||
echo ""
|
||||
df_print_section "Slowest (top 10)"
|
||||
systemd-analyze blame --no-pager | head -10 | sed 's/^/ /'
|
||||
}
|
||||
|
||||
sc-search() {
|
||||
[[ -z "$1" ]] && { echo "Usage: sc-search <query>"; return 1; }
|
||||
df_print_func_name "Service Search: $1"
|
||||
systemctl list-unit-files --type=service --no-pager | grep -i "$1"
|
||||
}
|
||||
|
||||
sc-info() {
|
||||
[[ -z "$1" ]] && { echo "Usage: sc-info <service>"; return 1; }
|
||||
df_print_func_name "Service: $1"
|
||||
systemctl status "$1" --no-pager -l 2>/dev/null || sudo systemctl status "$1" --no-pager -l
|
||||
echo ""
|
||||
df_print_section "Unit File"
|
||||
systemctl cat "$1" 2>/dev/null | head -30
|
||||
}
|
||||
|
||||
# fzf interactive
|
||||
if df_cmd_exists fzf; then
|
||||
scf() {
|
||||
local svc=$(systemctl list-units --type=service --no-pager --no-legend | \
|
||||
fzf $(df_fzf_opts) --prompt='Service > ' --preview='systemctl status {1} --no-pager' | awk '{print $1}')
|
||||
[[ -z "$svc" ]] && return
|
||||
df_print_info "Selected: $svc"
|
||||
echo "[s]tatus [r]estart [l]ogs [e]nable [d]isable"
|
||||
read -k 1 "act?Action: "; echo
|
||||
case "$act" in
|
||||
s) sudo systemctl status "$svc" --no-pager -l ;;
|
||||
r) scr "$svc" ;;
|
||||
l) sclog "$svc" ;;
|
||||
e) sce "$svc" ;;
|
||||
d) scd "$svc" ;;
|
||||
esac
|
||||
}
|
||||
fi
|
||||
|
||||
sc-help() {
|
||||
df_print_func_name "Systemd Commands"
|
||||
cat << 'EOF'
|
||||
sc <args> sudo systemctl
|
||||
scu <args> systemctl --user
|
||||
scr <svc> Restart + status
|
||||
sce <svc> Enable + start
|
||||
scd <svc> Disable + stop
|
||||
sclog <svc> Follow logs
|
||||
sclogs <svc> Recent logs
|
||||
sc-failed Failed services
|
||||
sc-timers Active timers
|
||||
sc-boot Boot analysis
|
||||
sc-search <q> Search services
|
||||
sc-info <svc> Service details
|
||||
scf Interactive (fzf)
|
||||
EOF
|
||||
}
|
||||
|
||||
alias scs='sc status' scstart='sc start' scstop='sc stop' screload='sc daemon-reload'
|
||||
alias jctl='journalctl' jctlf='journalctl -f' jctlb='journalctl -b'
|
||||
BIN
refactor_backup/zsh/functions/systemd-helpers.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/systemd-helpers.zsh.zwc
Normal file
Binary file not shown.
116
refactor_backup/zsh/functions/tmux-workspaces.zsh
Normal file
116
refactor_backup/zsh/functions/tmux-workspaces.zsh
Normal file
@@ -0,0 +1,116 @@
|
||||
# ============================================================================
|
||||
# Tmux Workspace Manager - Project Templates & Layouts
|
||||
# ============================================================================
|
||||
|
||||
source "${0:A:h}/../lib/utils.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/utils.zsh" 2>/dev/null
|
||||
|
||||
typeset -g TW_TEMPLATES="${TW_TEMPLATES:-$HOME/.dotfiles/.tmux-templates}"
|
||||
typeset -g TW_PREFIX="${TW_PREFIX:-work}"
|
||||
typeset -g TW_DEFAULT="${TW_DEFAULT:-dev}"
|
||||
|
||||
_tw_check() { df_require_cmd tmux || return 1; }
|
||||
|
||||
_tw_init() {
|
||||
df_ensure_dir "$TW_TEMPLATES"
|
||||
[[ ! -f "$TW_TEMPLATES/dev.tmux" ]] && {
|
||||
echo -e "# Dev layout\nsplit-window -h -p 50\nsplit-window -v -p 50\nselect-pane -t 0" > "$TW_TEMPLATES/dev.tmux"
|
||||
echo -e "# Ops layout\nsplit-window -h\nsplit-window -v\nselect-pane -t 0\nsplit-window -v\nselect-pane -t 0" > "$TW_TEMPLATES/ops.tmux"
|
||||
echo "# Full\n" > "$TW_TEMPLATES/full.tmux"
|
||||
df_print_success "Created default templates"
|
||||
}
|
||||
}
|
||||
|
||||
tw-templates() {
|
||||
_tw_init
|
||||
df_print_func_name "Tmux Templates"
|
||||
for t in "$TW_TEMPLATES"/*.tmux; do
|
||||
[[ -f "$t" ]] && df_print_indent "● $(basename "$t" .tmux)"
|
||||
done
|
||||
echo ""
|
||||
df_print_info "Create: tw-create <name> <template>"
|
||||
}
|
||||
|
||||
tw-create() {
|
||||
local name="$1" tmpl="${2:-$TW_DEFAULT}"
|
||||
[[ -z "$name" ]] && { tw-templates; return 1; }
|
||||
_tw_check || return 1
|
||||
_tw_init
|
||||
|
||||
local session="${TW_PREFIX}-${name}"
|
||||
tmux has-session -t "$session" 2>/dev/null && { df_print_error "'$name' exists"; return 1; }
|
||||
|
||||
local tfile="$TW_TEMPLATES/${tmpl}.tmux"
|
||||
[[ ! -f "$tfile" ]] && { df_print_error "Template '$tmpl' not found"; tw-templates; return 1; }
|
||||
|
||||
df_print_step "Creating: $name (template: $tmpl)"
|
||||
tmux new-session -d -s "$session"
|
||||
tmux source-file "$tfile" -t "$session"
|
||||
|
||||
df_in_git_repo && {
|
||||
local root=$(df_git_root)
|
||||
df_print_info "Git root: $root"
|
||||
tmux send-keys -t "$session:0" "cd $root" C-m
|
||||
}
|
||||
|
||||
df_print_success "Created: $name"
|
||||
[[ -z "$TMUX" ]] && tmux attach -t "$session" || df_print_info "Switch: tmux switch -t $session"
|
||||
}
|
||||
|
||||
tw-list() {
|
||||
_tw_check || return 1
|
||||
df_print_func_name "Tmux Workspaces"
|
||||
local found=false
|
||||
tmux list-sessions 2>/dev/null | while IFS=: read -r sess rest; do
|
||||
[[ "$sess" == ${TW_PREFIX}-* ]] && { found=true; df_print_indent "● ${sess#${TW_PREFIX}-}"; }
|
||||
done
|
||||
[[ "$found" != true ]] && df_print_info "No workspaces. Use: tw-create <name>"
|
||||
}
|
||||
|
||||
tw-attach() {
|
||||
local name="$1"
|
||||
[[ -z "$name" ]] && { tw-list; return 1; }
|
||||
_tw_check || return 1
|
||||
local session="${TW_PREFIX}-${name}"
|
||||
tmux has-session -t "$session" 2>/dev/null || { df_print_error "'$name' not found"; return 1; }
|
||||
[[ -z "$TMUX" ]] && tmux attach -t "$session" || tmux switch -t "$session"
|
||||
}
|
||||
|
||||
tw-delete() {
|
||||
[[ -z "$1" ]] && { tw-list; return 1; }
|
||||
_tw_check || return 1
|
||||
local session="${TW_PREFIX}-${1}"
|
||||
tmux has-session -t "$session" 2>/dev/null || { df_print_error "'$1' not found"; return 1; }
|
||||
tmux kill-session -t "$session"
|
||||
df_print_success "Deleted: $1"
|
||||
}
|
||||
|
||||
tw() {
|
||||
local name="$1" tmpl="${2:-$TW_DEFAULT}"
|
||||
[[ -z "$name" ]] && { tw-list; return; }
|
||||
_tw_check || return 1
|
||||
local session="${TW_PREFIX}-${name}"
|
||||
tmux has-session -t "$session" 2>/dev/null && tw-attach "$name" || tw-create "$name" "$tmpl"
|
||||
}
|
||||
|
||||
twf() {
|
||||
df_require_cmd fzf || return 1
|
||||
_tw_check || return 1
|
||||
local sessions=()
|
||||
tmux list-sessions 2>/dev/null | while IFS=: read -r sess rest; do
|
||||
[[ "$sess" == ${TW_PREFIX}-* ]] && sessions+=("${sess#${TW_PREFIX}-}")
|
||||
done
|
||||
[[ ${#sessions[@]} -eq 0 ]] && { df_print_info "No workspaces"; return 1; }
|
||||
local sel=$(printf '%s\n' "${sessions[@]}" | fzf $(df_fzf_opts) --prompt='Workspace > ')
|
||||
[[ -n "$sel" ]] && tw-attach "$sel"
|
||||
}
|
||||
|
||||
tw-sync() {
|
||||
[[ -z "$TMUX" ]] && { df_print_error "Must be in tmux"; return 1; }
|
||||
local cur=$(tmux show-window-option -v synchronize-panes 2>/dev/null)
|
||||
[[ "$cur" == "on" ]] && { tmux set-window-option synchronize-panes off; df_print_info "Sync: OFF"; } || \
|
||||
{ tmux set-window-option synchronize-panes on; df_print_info "Sync: ON"; }
|
||||
}
|
||||
|
||||
alias twl='tw-list' twc='tw-create' twa='tw-attach' twd='tw-delete' twt='tw-templates'
|
||||
_tw_init
|
||||
BIN
refactor_backup/zsh/functions/tmux-workspaces.zsh.zwc
Normal file
BIN
refactor_backup/zsh/functions/tmux-workspaces.zsh.zwc
Normal file
Binary file not shown.
Reference in New Issue
Block a user