diff options
| author | Deposite Pirate | 2026-02-28 11:05:06 +0100 |
|---|---|---|
| committer | Deposite Pirate | 2026-02-28 11:05:06 +0100 |
| commit | 4b457e9c61fb7d2ee3fff2511dfba9bd3a94fb3e (patch) | |
| tree | f8a37c3ed621e952325272a4b4b35791e39a59c9 /functions/rencase.fish | |
| parent | c695dec4e39811a2ed6321b7a95240c15c964cdc (diff) | |
More generic case tool
new file: functions/rencase.fish
deleted: functions/renlow.fish
Diffstat (limited to 'functions/rencase.fish')
| -rw-r--r-- | functions/rencase.fish | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/functions/rencase.fish b/functions/rencase.fish new file mode 100644 index 0000000..b0f691a --- /dev/null +++ b/functions/rencase.fish @@ -0,0 +1,80 @@ +function rencase --description 'Change the case of all files and directories' + +function _print_option -a opt -a desc + printf "\t%-20s %s\n" $opt $desc +end + +function _display_help + echo "usage: rencase <option> [files...]" + echo + _print_option "-l,--low" "Rename files to lowercase (default)" + _print_option "-h,--high" "Rename files to uppercase" + _print_option "-n,--nodir" "Don't rename directories" + echo +end + +function _low_or_high -a mode -a str + switch $mode + case low + echo (string lower $str) + case high + echo (string upper $str) + end +end + +if not argparse 'l/low' 'h/high' 'n/nodir' -- $argv + _display_help + return 1 +end + +if set -q _flag_low; and set -q _flag_high + echo "--low and --high are mutually exclusive options." + echo + _display_help + return 1 +end + +set mode 'low' + +if set -q _flag_high + set mode 'high' +end + +if test (count $argv) -gt 0 + set targets $argv +else + set targets '.' +end + +for file in (find $targets -type f) + set -l element (string split -r -m1 / $file) + set -l newname (_low_or_high $mode $element[2]) + if test -e $element[1]/$newname + set_color red + echo "File $newname already exists. Skipping." + set_color normal + continue + end + mv -- "$file" "$element[1]/$newname" +end + +if not set -q _flag_nodir + set -f dirs (find $targets -type d) + for dir in $dirs[-1..1] # Rename directories in reverse order + set -l element (string split -r -m1 / $dir) + set -l newname (_low_or_high $mode $element[2]) + if not test -z $element[2]; and not test -e $element[1]/$newname # Also catch . + mv -- "$dir" "$element[1]/$newname" + else + set_color red + echo "Directory $element[2] already exists. Skipping." + set_color normal + end + end +end + +functions -e _print_option +functions -e _display_help +functions -e _low_or_high + +end |
