Name : Add name_parse that accepts custom parser.
This commit is contained in:
parent
df5e4edde8
commit
d3047d5dcc
|
@ -4,7 +4,7 @@
|
||||||
function _autocomplete() {
|
function _autocomplete() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local commands="${*}"
|
local commands="${*}"
|
||||||
|
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
|
|
||||||
local cur="${COMP_WORDS[COMP_CWORD]}"
|
local cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
|
@ -32,6 +32,26 @@ function _autocomplete_first() {
|
||||||
fi
|
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.
|
# Autocomplete by grepping file names.
|
||||||
function _autocomplete_grep() {
|
function _autocomplete_grep() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
|
|
|
@ -32,14 +32,20 @@ function name() {
|
||||||
_iterate_targets process ${targets[@]}
|
_iterate_targets process ${targets[@]}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Rename files to strip all special characters.
|
# Rename files with provided parser, i.e. `parse_simple`.
|
||||||
# All files by default.
|
# All files by default.
|
||||||
# Usage: name_simple [FILES]
|
# Usage: name_parse <PARSER> [FILES]
|
||||||
function name_simple() {
|
function name_parse() {
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
local targets=(${@})
|
local parser=${1}
|
||||||
|
local targets=(${@:2})
|
||||||
[[ "${targets}" = "" ]] && targets=([^.]*)
|
[[ "${targets}" = "" ]] && targets=([^.]*)
|
||||||
|
|
||||||
|
if [[ "${parser}" = "" ]]; then
|
||||||
|
help name_parse
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
|
||||||
process() {
|
process() {
|
||||||
# Skip archive.
|
# Skip archive.
|
||||||
if $(_is_archive ${target}); then
|
if $(_is_archive ${target}); then
|
||||||
|
@ -61,7 +67,7 @@ function name_simple() {
|
||||||
[[ "${ext#.}" = "${name}" ]] && ext=""
|
[[ "${ext#.}" = "${name}" ]] && ext=""
|
||||||
|
|
||||||
# Get new name.
|
# Get new name.
|
||||||
local new_name=$(parse_simplify "${name}")${ext,,}
|
local new_name=$(${parser} "${name}")${ext,,}
|
||||||
|
|
||||||
# check if same name.
|
# check if same name.
|
||||||
[[ "${target}" = "${new_name}" ]] && return 0
|
[[ "${target}" = "${new_name}" ]] && return 0
|
||||||
|
@ -73,7 +79,7 @@ function name_simple() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# rename target.
|
# rename target.
|
||||||
mv -- "${target}" "${new_name}"
|
mv -- "${target}" "${new_name}" && echo "${new_name}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_iterate_targets process ${targets[@]}
|
_iterate_targets process ${targets[@]}
|
||||||
|
@ -354,3 +360,8 @@ function name_fix_numbering() {
|
||||||
_iterate_targets process ${targets[@]}
|
_iterate_targets process ${targets[@]}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _comp_name_parse() {
|
||||||
|
_autocomplete_first_ls $(find_function | grep ^parse)
|
||||||
|
}
|
||||||
|
|
||||||
|
complete -o filenames -F _comp_name_parse name_parse
|
||||||
|
|
|
@ -71,7 +71,7 @@ function parse_camel() {
|
||||||
echo "${result,}"
|
echo "${result,}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Parse to SNAKE_CASE_UPPERCASE.
|
# Parse to SNAKE_CASE_UPPERCASE. **NOT STABLE! Repeating results in different output.**
|
||||||
# Usage: parse_snake_uppercase <STRING>
|
# Usage: parse_snake_uppercase <STRING>
|
||||||
function parse_snake_uppercase() {
|
function parse_snake_uppercase() {
|
||||||
local parts=($(_get_parts ${*}))
|
local parts=($(_get_parts ${*}))
|
||||||
|
|
|
@ -225,7 +225,7 @@ Command|Description
|
||||||
Command|Description
|
Command|Description
|
||||||
---|---
|
---|---
|
||||||
`name [FILES]`|Rename dirs to `snake_case` and files to `PascalCase`. Careful with structured file names like archives!
|
`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 [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_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.
|
`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_snake <STRING>`|Parse to snake_case.
|
||||||
`parse_kebab <STRING>`|Parse to kebab-case.
|
`parse_kebab <STRING>`|Parse to kebab-case.
|
||||||
`parse_camel <STRING>`|Parse to camelCase.
|
`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_alnum <STRING>`|Parse data keeping only alphanumeric characters.
|
||||||
|
`parse_ints`|Parse integers from mixed string.
|
||||||
|
|
||||||
## Permissions.
|
## Permissions.
|
||||||
|
|
||||||
|
|
Reference in a new issue