aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeposite Pirate2026-02-28 11:05:06 +0100
committerDeposite Pirate2026-02-28 11:05:06 +0100
commit4b457e9c61fb7d2ee3fff2511dfba9bd3a94fb3e (patch)
treef8a37c3ed621e952325272a4b4b35791e39a59c9
parentc695dec4e39811a2ed6321b7a95240c15c964cdc (diff)
More generic case tool
new file: functions/rencase.fish deleted: functions/renlow.fish
-rw-r--r--functions/rencase.fish80
-rw-r--r--functions/renlow.fish16
2 files changed, 80 insertions, 16 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
diff --git a/functions/renlow.fish b/functions/renlow.fish
deleted file mode 100644
index 1581550..0000000
--- a/functions/renlow.fish
+++ /dev/null
@@ -1,16 +0,0 @@
-function renlow --description 'Rename all files and directories with lowercase characters'
-
-for file in (find . -type f)
- set -l element (string split -r -m1 / $file)
- mv $file $element[1]/(string lower $element[2])
-end
-
-set -f dirs (find . -type d)
-for dir in $dirs[-1..1] # Rename directories in reverse order
- set -l element (string split -r -m1 / $dir)
- if not test -z $element[2] # Catch .
- mv $dir $element[1]/(string lower $element[2])
- end
-end
-
-end