diff --git a/fish/completions/ff.fish b/fish/completions/ff.fish index 1befd7d..cd7b61f 100644 --- a/fish/completions/ff.fish +++ b/fish/completions/ff.fish @@ -1,6 +1,6 @@ # Completions for ff command complete -c ff -f -set -l ff_subcommands reload edit fish dotfile pon poff pst make script s update help region ssh init copy help region +set -l ff_subcommands reload edit fish dotfile pon poff pst make script s update help region ssh init copy dirsize help region complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "reload" -d "Reload fish configuration" complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "edit" -d "Edit custom fish configuration" complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "fish" -d "Change to fish config directory" @@ -14,6 +14,14 @@ complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "help" -d complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "region" -d "Toggle or set CN_REGION (mirror/proxy region)" complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "ssh" -d "SSH init (keys + config.d) or copy config to remote" +complete -c ff -n "not __fish_seen_subcommand_from $ff_subcommands" -a "dirsize" -d "Show directory sizes" + +complete -c ff -n "__fish_seen_subcommand_from dirsize" -s a -l all -d "Recursively show all subdirectories" +complete -c ff -n "__fish_seen_subcommand_from dirsize" -s d -l depth -d "Recursion depth (default 1)" -r +complete -c ff -n "__fish_seen_subcommand_from dirsize" -s n -l top -d "Show top N only" -r +complete -c ff -n "__fish_seen_subcommand_from dirsize" -s r -l reverse -d "Sort smallest first" +complete -c ff -n "__fish_seen_subcommand_from dirsize" -s h -l help -d "Show help" +complete -c ff -n "__fish_seen_subcommand_from dirsize" -a "(__fish_complete_directories)" -d "Directory" complete -c ff -n "__fish_seen_subcommand_from ssh; and not __fish_seen_subcommand_from init copy help" -a init -d "Ensure SSH key and config.d Include" complete -c ff -n "__fish_seen_subcommand_from ssh; and not __fish_seen_subcommand_from init copy help" -a copy -d "Copy specified SSH config file(s) to remote" diff --git a/fish/functions/ff.fish b/fish/functions/ff.fish index a6b5353..2eef722 100644 --- a/fish/functions/ff.fish +++ b/fish/functions/ff.fish @@ -14,6 +14,7 @@ function ff "update|Update dotfile" \ "region|Toggle or set CN_REGION (mirror/proxy region)" \ "ssh|SSH init (keys + config.d) or copy config to remote" \ + "dirsize|Show directory sizes (du wrapper)" \ "help|Show this help message" # 确保 execute-script 可用(在 fish 自动加载前手动引入一次) @@ -89,6 +90,14 @@ function ff source ~/.dotfile/fish/functions/ff-ssh.fish end ff_ssh $argv[2..-1] + case dirsize + set -l script_path ~/.dotfile/script/dirsize.sh + if test -f $script_path + bash $script_path $argv[2..-1] + else + echo "Error: dirsize script not found at $script_path" + return 1 + end case region set -l sub toggle if test (count $argv) -ge 2 diff --git a/script/dirsize.sh b/script/dirsize.sh index 73a59c1..c5ecc78 100755 --- a/script/dirsize.sh +++ b/script/dirsize.sh @@ -1,6 +1,6 @@ #!/bin/bash -set -euo pipefail +set -eu usage() { cat <<'EOF' @@ -33,13 +33,31 @@ TARGET_DIR="." while [[ $# -gt 0 ]]; do case "$1" in - -a|--all) ALL=true; shift ;; - -d|--depth) DEPTH="$2"; shift 2 ;; - -n|--top) TOP="$2"; shift 2 ;; - -r|--reverse) REVERSE=true; shift ;; - -h|--help) usage ;; - -*) echo "未知选项: $1" >&2; usage ;; - *) TARGET_DIR="$1"; shift ;; + -a | --all) + ALL=true + shift + ;; + -d | --depth) + DEPTH="$2" + shift 2 + ;; + -n | --top) + TOP="$2" + shift 2 + ;; + -r | --reverse) + REVERSE=true + shift + ;; + -h | --help) usage ;; + -*) + echo "未知选项: $1" >&2 + usage + ;; + *) + TARGET_DIR="$1" + shift + ;; esac done @@ -48,7 +66,7 @@ if [[ ! -d "$TARGET_DIR" ]]; then exit 1 fi -TARGET_DIR="${TARGET_DIR%/}" +TARGET_DIR="$(cd "$TARGET_DIR" && pwd -P)" if $ALL; then DEPTH=999 @@ -60,9 +78,9 @@ if $REVERSE; then fi if [[ "$OSTYPE" == "darwin"* ]]; then - result=$(du -h -d "$DEPTH" "$TARGET_DIR" 2>/dev/null | grep -v "^.*${TARGET_DIR}$" | sort $sort_flag) + result=$(sudo du -h -d "$DEPTH" "$TARGET_DIR" 2>/dev/null | awk -v dir="$TARGET_DIR" '$2 != dir {print}' | sort $sort_flag) else - result=$(du -h --max-depth="$DEPTH" "$TARGET_DIR" 2>/dev/null | grep -v "^.*${TARGET_DIR}$" | sort $sort_flag) + result=$(sudo du -h --max-depth="$DEPTH" "$TARGET_DIR" 2>/dev/null | awk -v dir="$TARGET_DIR" '$2 != dir {print}' | sort $sort_flag) fi if [[ "$TOP" -gt 0 ]]; then