diff --git a/.config/bash/module/archive.sh b/.config/bash/module/archive.sh index 72d7d9a..b40c917 100644 --- a/.config/bash/module/archive.sh +++ b/.config/bash/module/archive.sh @@ -13,7 +13,7 @@ function archive() { local date=$(_archive_date) # Parse name. - local name=$(parse_camel ${target}) + local name=$(parse_pascal ${target}) # create archive. local hash=$(tar -c ${target} | pv -s $(/usr/bin/du -sb ${target} | awk '{print $1}') | xz -9e | tee ${name}.txz | sha1sum | cut -d\ -f1) @@ -39,7 +39,7 @@ function archive_fast() { local date=$(_archive_date) # Parse name. - local name=$(parse_camel "${target}") + local name=$(parse_pascal "${target}") # create archive. local hash=$(tar -c "${target}" | pv -s $(/usr/bin/du -sb "${target}" | awk '{print $1}') | gzip -1 | tee "${name}".tgz | sha1sum | cut -d\ -f1) @@ -159,7 +159,7 @@ function archive_name() { # simplify name by default. if [[ "${name}" = "" || ${count} -gt 1 ]]; then name="${target%_*}" - name="$(parse_camel ${name})" + name="$(parse_pascal ${name})" fi # remove old name. diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 1d961bd..93f0e05 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -313,3 +313,31 @@ function name_fix_numbering() { _iterate_targets process ${targets[@]} } + +# Rename dirs to `snake_case` and files to `PascalCase`. Careful with structured file names like archives! +# Usage: name_convention [FILES] +function name_convention() { + local IFS=$'\n' + local targets=(${@}) + [[ "${targets}" = "" ]] && targets=($(ls)) + + process() { + if [[ -d "${target}" ]]; then + local new_name=$(parse_snake ${target}) + [[ -e "${new_name}" ]] && return 0 + + mv -- ${target} ${new_name} && echo ${new_name} + else + local ext=".${target##*.}" + local name=${target%.*} + [[ "${ext}" = ".${target}" ]] && ext="" + + local new_name="$(parse_pascal ${name})${ext}" + [[ -e "${new_name}" ]] && return 0 + + mv -- ${target} ${new_name} && echo ${new_name} + fi + } + + _iterate_targets process ${targets[@]} +} diff --git a/.config/bash/module/parse.sh b/.config/bash/module/parse.sh index 5e3bef8..d0d63a1 100644 --- a/.config/bash/module/parse.sh +++ b/.config/bash/module/parse.sh @@ -17,6 +17,12 @@ function parse_pascal() { local parts=($(parse_simplify ${1})) local result + # If already in Pascal. + if [[ "${#parts[@]}" = 1 ]]; then + echo "${parts[*]^}" + return 0 + fi + for part in "${parts[@]}"; do local word="${part,,}" word="${word^}" @@ -63,6 +69,12 @@ function parse_camel() { local parts=($(parse_simplify ${1})) local result + # If already in camel. + if [[ "${#parts[@]}" = 1 ]]; then + echo "${parts[*],}" + return 0 + fi + for part in "${parts[@]}"; do local word="${part,,}" word="${word^}"