#!/bin/zsh # Trunkate given files (or create them if the don't exist) function purge { print "Warning, the following files will be destroyed: $@" read '?Continue? ' for f in $@; do echo -n "" > $f done } # Pretty color printing function clr { cl=$1 shift print -- "${fg[$cl]}$@$reset_color" } function bclr { cl=$1 shift print -- "${fg_bold[$cl]}$@$reset_color" } function red { clr red $@ } function bred { bclr red $@ } function green { clr green $@ } # Replace . with spaces function fnorm { if [[ $# -eq 0 ]]; then set * ; fi for f in $@; do dir=$(dirname $f) file=$(basename $f) base=${file:r:l} sufx=${file:e:l} new=${base//./ } if [[ -d $f ]]; then new=${new// /_} else new=${new//_/ } fi if [[ -n $sufx ]]; then new=$new.$sufx fi #print "$file -> $(green $new)" nf=$dir/$new if [[ -e $nf ]]; then print -- "[$(bred file exists)] $nf" else mv -i "$f" "$nf" fi done } function simple_replace { case $1 in 'underscores'|'u') mode=1;; 'spaces'|'s') mode=2;; 'lower'|'l') mode=3;; *) return 1;; esac shift if [[ $# -eq 0 ]]; then set * ; fi for f in $@; do dir=$(dirname $f) file=$(basename $f) case $mode in 1) file=${file// /_} ;; 2) file=${file//_/ } ;; 3) file=${file:l} ;; esac new="$dir/$file" if [[ ! -e $new ]]; then if mv "$f" "$new" fi done } function underscores { simple_replace u $@ } function spaces { simple_replace s $@ } function lower { simple_replace l $@ }