#!/usr/bin/env bash # ============================================================================ # Dotfiles Shell Analytics (Arch/CachyOS) # ============================================================================ set -e readonly DOTFILES_HOME="${DOTFILES_HOME:-$HOME/.dotfiles}" # Source shared colors source "$DOTFILES_HOME/zsh/lib/colors.zsh" 2>/dev/null || { DF_RED=$'\033[0;31m' DF_GREEN=$'\033[0;32m' DF_YELLOW=$'\033[1;33m' DF_BLUE=$'\033[0;34m' DF_CYAN=$'\033[0;36m' DF_MAGENTA=$'\033[0;35m' DF_NC=$'\033[0m' DF_GREY=$'\033[38;5;242m' DF_LIGHT_BLUE=$'\033[38;5;39m' DF_BOLD=$'\033[1m' DF_DIM=$'\033[2m' } # Source utils.zsh source "$DOTFILES_HOME/zsh/lib/utils.zsh" 2>/dev/null # ============================================================================ # MOTD-style header # ============================================================================ print_header() { if declare -f df_print_header &>/dev/null; then df_print_header "dotfiles-stats " else local user="${USER:-root}" local hostname="${HOSTNAME:-$(hostname -s 2>/dev/null)}" local datetime=$(date '+%a %b %d %H:%M') local width=66 local hline="" && for ((i=0; i/dev/null | sort | uniq -c | sort -k2n | while read count hour; do local bar_length=$((count / 5)) local bar=$(printf '█%.0s' $(seq 1 $bar_length)) printf " ${DF_CYAN}%02d:00${DF_NC} ${DF_MAGENTA}%5d${DF_NC} ${DF_GREEN}${bar}${DF_NC}\n" "$hour" "$count" done else echo " ${DF_YELLOW}⚠${DF_NC} Zsh history file required for hourly breakdown" fi echo "" } show_dirs() { print_section "Most Visited Directories" echo "" if [[ -f "$HOME/.zsh_history" ]]; then grep -I "cd " "$HOME/.zsh_history" | awk '{print $NF}' | sort | uniq -c | \ sort -rn | head -15 | while read count dir; do printf " ${DF_CYAN}%4d${DF_NC} ${DF_YELLOW}%s${DF_NC}\n" "$count" "$dir" done else echo " ${DF_YELLOW}⚠${DF_NC} Zsh history file required" fi echo "" } show_git_breakdown() { print_section "Git Command Breakdown" echo "" local total=$(get_history | grep -I "^git" | wc -l) if [[ $total -eq 0 ]]; then echo " ${DF_YELLOW}No git commands found${DF_NC}" return fi get_history | grep -I "^git " | awk '{print $2}' | sort | uniq -c | sort -rn | \ head -10 | while read count subcmd; do local percent=$((count * 100 / total)) printf " ${DF_YELLOW}git %-15s${DF_NC} ${DF_CYAN}%4d${DF_NC} (${DF_MAGENTA}%3d%%${DF_NC})\n" \ "$subcmd" "$count" "$percent" done echo "" } # ============================================================================ # Main # ============================================================================ main() { print_header case "${1:-dashboard}" in dashboard) show_dashboard ;; top) show_top_n "${2:-20}" ;; suggest) show_suggestions ;; breakdown) show_breakdown ;; heatmap) show_heatmap ;; dirs) show_dirs ;; git) show_git_breakdown ;; export) echo "{" echo " \"total_commands\": $(get_history | wc -l)," echo " \"unique_commands\": $(get_history | sort | uniq | wc -l)," echo " \"timestamp\": \"$(date -Iseconds)\"" echo "}" ;; *) echo "Usage: $0 {dashboard|top [n]|suggest|breakdown|heatmap|dirs|git|export}" echo "" echo "Commands:" echo " dashboard Show full dashboard (default)" echo " top [n] Show top N commands (default: 20)" echo " suggest Suggest aliases" echo " breakdown Command category breakdown" echo " heatmap Activity by hour" echo " dirs Most visited directories" echo " git Git command breakdown" echo " export Export as JSON" exit 1 ;; esac } main "$@"