diff --git a/.config/bash/module/Autocomplete.sh b/.config/bash/module/Autocomplete.sh index dee9dfe..a1bc2b9 100644 --- a/.config/bash/module/Autocomplete.sh +++ b/.config/bash/module/Autocomplete.sh @@ -4,7 +4,7 @@ function _autocomplete() { local IFS=$'\n' local commands="${*}" - + COMPREPLY=() local cur="${COMP_WORDS[COMP_CWORD]}" @@ -32,6 +32,26 @@ function _autocomplete_first() { fi } +# Autocomplete only first argument and the rest with files. +function _autocomplete_first_ls() { + local IFS=$'\n' + local commands="${*}" + + COMPREPLY=() + + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + local command="${COMP_WORDS[0]}" + + if [[ "${prev}" = "${command}" ]]; then + COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) ) + return 0 + else + COMPREPLY=( $(compgen -W "$(ls)" -- ${cur}) ) + return 0 + fi +} + # Autocomplete by grepping file names. function _autocomplete_grep() { local IFS=$'\n' diff --git a/.config/bash/module/Name.sh b/.config/bash/module/Name.sh index 37e8465..f0abe4f 100644 --- a/.config/bash/module/Name.sh +++ b/.config/bash/module/Name.sh @@ -32,14 +32,20 @@ function name() { _iterate_targets process ${targets[@]} } -# Rename files to strip all special characters. +# Rename files with provided parser, i.e. `parse_simple`. # All files by default. -# Usage: name_simple [FILES] -function name_simple() { +# Usage: name_parse [FILES] +function name_parse() { local IFS=$'\n' - local targets=(${@}) + local parser=${1} + local targets=(${@:2}) [[ "${targets}" = "" ]] && targets=([^.]*) + if [[ "${parser}" = "" ]]; then + help name_parse + return 2 + fi + process() { # Skip archive. if $(_is_archive ${target}); then @@ -61,7 +67,7 @@ function name_simple() { [[ "${ext#.}" = "${name}" ]] && ext="" # Get new name. - local new_name=$(parse_simplify "${name}")${ext,,} + local new_name=$(${parser} "${name}")${ext,,} # check if same name. [[ "${target}" = "${new_name}" ]] && return 0 @@ -73,7 +79,7 @@ function name_simple() { fi # rename target. - mv -- "${target}" "${new_name}" + mv -- "${target}" "${new_name}" && echo "${new_name}" } _iterate_targets process ${targets[@]} @@ -354,3 +360,8 @@ function name_fix_numbering() { _iterate_targets process ${targets[@]} } +function _comp_name_parse() { + _autocomplete_first_ls $(find_function | grep ^parse) +} + +complete -o filenames -F _comp_name_parse name_parse diff --git a/.config/bash/module/Parse.sh b/.config/bash/module/Parse.sh index d4bd7f1..a8ade51 100644 --- a/.config/bash/module/Parse.sh +++ b/.config/bash/module/Parse.sh @@ -71,7 +71,7 @@ function parse_camel() { echo "${result,}" } -# Parse to SNAKE_CASE_UPPERCASE. +# Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.** # Usage: parse_snake_uppercase function parse_snake_uppercase() { local parts=($(_get_parts ${*})) diff --git a/.doc/Bash.md b/.doc/Bash.md index ee0257c..965263c 100644 --- a/.doc/Bash.md +++ b/.doc/Bash.md @@ -225,7 +225,7 @@ Command|Description Command|Description ---|--- `name [FILES]`|Rename dirs to `snake_case` and files to `PascalCase`. Careful with structured file names like archives! -`name_simple [FILES]`|Rename files to strip all special characters. All files by default. +`name_parse [FILES]`|Rename files with provided parser, i.e. `parse_simple`. All files by default. `name_hash [FILES]`|Rename all files to their hashes while keeping extensions. All files by default. `name_hash_check [FILES]`|Check hashes for previously renamed files. All files by default. `name_series [FILES]`|Rename files for Jellyfin series, i.e. `Episode S01E01.mkv` All files by default. @@ -271,8 +271,9 @@ Command|Description `parse_snake `|Parse to snake_case. `parse_kebab `|Parse to kebab-case. `parse_camel `|Parse to camelCase. -`parse_snake_uppercase `|Parse to SNAKE_CASE_UPPERCASE. +`parse_snake_uppercase `|Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.** `parse_alnum `|Parse data keeping only alphanumeric characters. +`parse_ints`|Parse integers from mixed string. ## Permissions.