Name : Add name_parse that accepts custom parser.

This commit is contained in:
Dmitry Voronin 2024-01-03 22:18:18 +03:00
parent df5e4edde8
commit d3047d5dcc
4 changed files with 42 additions and 10 deletions

View file

@ -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'

View file

@ -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 <PARSER> [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

View file

@ -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 <STRING>
function parse_snake_uppercase() {
local parts=($(_get_parts ${*}))

View file

@ -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 <PARSER> [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 <STRING>`|Parse to snake_case.
`parse_kebab <STRING>`|Parse to kebab-case.
`parse_camel <STRING>`|Parse to camelCase.
`parse_snake_uppercase <STRING>`|Parse to SNAKE_CASE_UPPERCASE.
`parse_snake_uppercase <STRING>`|Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.**
`parse_alnum <STRING>`|Parse data keeping only alphanumeric characters.
`parse_ints`|Parse integers from mixed string.
## Permissions.