Intégration de la branche better-fndate
This commit is contained in:
commit
208f30ddad
218
fndate
218
fndate
|
@ -1,5 +1,16 @@
|
|||
#!/bin/bash
|
||||
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||
|
||||
TOOLS=(ddir dfile dcopy dmove dcmd)
|
||||
if [ "$#" -eq 1 -a "$1" == --nutools-makelinks ]; then
|
||||
# créer les liens
|
||||
scriptname="$(basename "$0")"
|
||||
for tool in "${TOOLS[@]}"; do
|
||||
ln -s "$scriptname" "$tool"
|
||||
done
|
||||
exit 0
|
||||
fi
|
||||
|
||||
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
||||
urequire DEFAULTS
|
||||
|
||||
|
@ -41,10 +52,17 @@ OPTIONS
|
|||
arguments, alors ajouter la date à la fin de la commande. Par exemple,
|
||||
voici comment émuler l'option -k
|
||||
$scriptname -c cp src dest/~~
|
||||
--string
|
||||
Remplacer dans chaque argument le placeholder par la date et afficher le
|
||||
résultat.
|
||||
-P, --placeholder PLACEHOLDER
|
||||
Dans le nom spécifié, remplacer PLACEHOLDER par la date. Par défaut,
|
||||
PLACEHOLDER vaut ~~. Si le nom spécifié ne contient pas le PLACEHOLDER,
|
||||
il est placé au début.
|
||||
-@, --force-date DATE
|
||||
Dans le nom spécifié, si la date est déjà présente, forcer son
|
||||
remplacement par la valeur spécifiée. Par défaut, le fichier n'est pas
|
||||
modifié si la date est déjà présente.
|
||||
-F, --format FORMAT
|
||||
Spécifier le format de la date à insérer dans le nom du fichier. Par
|
||||
défaut, FORMAT vaut 'YYMMDD-'
|
||||
|
@ -53,7 +71,13 @@ OPTIONS
|
|||
YY année sur 2 chiffres
|
||||
MM mois sur 2 chiffres
|
||||
DD jour sur 2 chiffres
|
||||
Tous les autres caractères sont pris tels-quels"
|
||||
Tous les autres caractères sont pris tels-quels
|
||||
-s, --short
|
||||
Equivalent à -F YYMMDD --autof
|
||||
-l, --long
|
||||
Equivalent à -F YYYMMDD --autof
|
||||
--autof
|
||||
Option non (pas encore) documentée"
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
@ -66,13 +90,43 @@ function have_ph() {
|
|||
[[ "$1" == *"$placeholder"* ]]
|
||||
}
|
||||
function replace_ph() {
|
||||
awkrun ph="$placeholder" format="$format" autof:int="$autof" '
|
||||
local fy fm fd
|
||||
if [ -n "$force_date" ]; then
|
||||
fd="${force_date:0:2}"
|
||||
fm="${force_date:3:2}"
|
||||
fy="${force_date:6:4}"
|
||||
fi
|
||||
awkrun ph="$placeholder" \
|
||||
force_date="$force_date" fy:str="$fy" fm:str="$fm" fd:str="$fd" \
|
||||
format="$format" autof:int="$autof" '
|
||||
function short2long(s, cy, py, sy, ly) {
|
||||
cy = strftime("%Y") + 0
|
||||
cy = cy - cy % 100
|
||||
py = cy - 100
|
||||
sy = substr(s, 1, 2) + 0
|
||||
if (sy >= 80) ly = py + sy
|
||||
else ly = cy + sy
|
||||
return ly substr(s, 3)
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
gsub(/%/, "%%", format)
|
||||
gsub(/YYYY/, "%Y", format)
|
||||
gsub(/YY/, "%y", format)
|
||||
gsub(/MM/, "%m", format)
|
||||
gsub(/DD/, "%d", format)
|
||||
if (format ~ /YYYYMMDD/) type = "long"
|
||||
else if (format ~ /YYMMDD/) type = "short"
|
||||
else type = "custom"
|
||||
|
||||
if (force_date != "") {
|
||||
gsub(/%/, "%%", format)
|
||||
gsub(/YYYY/, fy, format)
|
||||
gsub(/YY/, substr(fy, 3, 2), format)
|
||||
gsub(/MM/, fm, format)
|
||||
gsub(/DD/, fd, format)
|
||||
} else {
|
||||
gsub(/%/, "%%", format)
|
||||
gsub(/YYYY/, "%Y", format)
|
||||
gsub(/YY/, "%y", format)
|
||||
gsub(/MM/, "%m", format)
|
||||
gsub(/DD/, "%d", format)
|
||||
}
|
||||
date = strftime(format)
|
||||
}
|
||||
{
|
||||
|
@ -88,7 +142,43 @@ BEGIN {
|
|||
if (autof) {
|
||||
pos = index(name, ph)
|
||||
if (pos == 0) {
|
||||
print dir date "-" name
|
||||
if (force_date != "") {
|
||||
if (type == "long") {
|
||||
if (name ~ /^[0-9]{8}/) {
|
||||
name = substr(name, 9)
|
||||
print dir date name
|
||||
} else if (name ~ /^[0-9]{6}/) {
|
||||
name = substr(name, 7)
|
||||
print dir date name
|
||||
} else {
|
||||
print dir date "-" name
|
||||
}
|
||||
} else if (type == "short") {
|
||||
if (name ~ /^[0-9]{8}/) {
|
||||
name = substr(name, 9)
|
||||
print dir date name
|
||||
} else if (name ~ /^[0-9]{6}/) {
|
||||
name = substr(name, 7)
|
||||
print dir date name
|
||||
} else {
|
||||
print dir date "-" name
|
||||
}
|
||||
} else if (type == "custom") {
|
||||
print dir date "-" name
|
||||
}
|
||||
} else {
|
||||
if (type == "long") {
|
||||
if (name ~ /^[0-9]{8}/) print dir name
|
||||
else if (name ~ /^[0-9]{6}/) print dir short2long(name)
|
||||
else print dir date "-" name
|
||||
} else if (type == "short") {
|
||||
if (name ~ /^[0-9]{8}/) print dir substr(name, 3)
|
||||
else if (name ~ /^[0-9]{6}/) print dir name
|
||||
else print dir date "-" name
|
||||
} else if (type == "custom") {
|
||||
print dir date "-" name
|
||||
}
|
||||
}
|
||||
} else if (pos == 1) {
|
||||
print dir date "-" substr(name, length(ph) + 1)
|
||||
} else if (pos == length(name) - length(ph) + 1) {
|
||||
|
@ -99,7 +189,27 @@ BEGIN {
|
|||
} else {
|
||||
pos = index(name, ph)
|
||||
if (pos == 0) {
|
||||
print dir date name
|
||||
if (force_date != "") {
|
||||
if (type == "long") {
|
||||
if (name ~ /^[0-9]{8}/) name = substr(name, 9)
|
||||
print dir date name
|
||||
} else if (type == "short") {
|
||||
if (name ~ /^[0-9]{6}/) name = substr(name, 7)
|
||||
print dir date name
|
||||
} else if (type == "custom") {
|
||||
print dir date name
|
||||
}
|
||||
} else {
|
||||
if (type == "long") {
|
||||
if (name ~ /^[0-9]{8}/) print dir name
|
||||
else print dir date name
|
||||
} else if (type == "short") {
|
||||
if (name ~ /^[0-9]{6}/) print dir name
|
||||
else print dir date name
|
||||
} else if (type == "custom") {
|
||||
print dir date name
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print dir substr(name, 1, pos - 1) date substr(name, pos + length(ph))
|
||||
}
|
||||
|
@ -109,8 +219,17 @@ BEGIN {
|
|||
|
||||
action=create
|
||||
create=
|
||||
case "$scriptname" in
|
||||
ddir) action=create; create=dir;;
|
||||
dfile) action=create; create=file;;
|
||||
dcopy) action=copy;;
|
||||
dmove) action=move;;
|
||||
dcmd) action=cmd;;
|
||||
esac
|
||||
placeholder=
|
||||
force_date=
|
||||
format=
|
||||
autof=
|
||||
parse_opts "${PRETTYOPTS[@]}" \
|
||||
--help '$exit_with display_help' \
|
||||
--create-or-rename action=create \
|
||||
|
@ -119,16 +238,19 @@ parse_opts "${PRETTYOPTS[@]}" \
|
|||
-k,--copy action=copy \
|
||||
-m,--move action=move \
|
||||
-c,--cmd action=cmd \
|
||||
--string action=string \
|
||||
-P:,--placeholder: placeholder= \
|
||||
-@:,--force-date: force_date= \
|
||||
-F:,--format: format= \
|
||||
-s,--short '$format=YYMMDD; autof=1' \
|
||||
-l,--long '$format=YYYYMMDD; autof=1' \
|
||||
--autof autof=1 \
|
||||
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
||||
|
||||
[ -n "$create" ] || create=file
|
||||
[ -n "$placeholder" ] || placeholder="~~"
|
||||
if [ -n "$format" ]; then
|
||||
autof=
|
||||
else
|
||||
[ -n "$force_date" ] && setx force_date=parse_date "$force_date"
|
||||
if [ -z "$format" ]; then
|
||||
format=YYMMDD
|
||||
autof=1
|
||||
fi
|
||||
|
@ -141,20 +263,20 @@ if [ "$action" == cmd ]; then
|
|||
for arg in "$@"; do
|
||||
if have_ph "$arg"; then
|
||||
check_interaction -c && evalx qvals "${args[@]}" "$arg" // estepi "Commande:"
|
||||
setx arg=replace_ph "$arg"
|
||||
setx arg=replace_ph "$arg" "$force_date"
|
||||
read_value "Veuillez confirmer le nom" arg "$arg"
|
||||
found=1
|
||||
fi
|
||||
args=("${args[@]}" "$arg")
|
||||
done
|
||||
if [ -z "$found" ]; then
|
||||
setx arg=replace_ph
|
||||
setx arg=replace_ph "" "$force_date"
|
||||
read_value "Veuillez entrer le nom du fichier" arg "$arg"
|
||||
args=("${args[@]}" "$arg")
|
||||
fi
|
||||
|
||||
if show_debug; then
|
||||
evalx qvals "${args[@]}" // edebug "Commande:"
|
||||
evalx qvals "${args[@]}" // edebug "Commande effective:"
|
||||
ask_yesno "Voulez-vous continuer?" X || die
|
||||
fi
|
||||
"${args[@]}"
|
||||
|
@ -184,12 +306,17 @@ elif [ "$action" == copy -o "$action" == move ]; then
|
|||
cmd=(mv -i)
|
||||
fi
|
||||
for src in "${srcs[@]}"; do
|
||||
setx src=abspath "$src"
|
||||
setx srcname=basename -- "$src"
|
||||
if [ -e "$src" ]; then
|
||||
einfo "$cmdinfo de $(ppath "$src")"
|
||||
setx destname=replace_ph "$srcname"
|
||||
estep "$cmdinfo de $(ppath "$src")"
|
||||
setx destname=replace_ph "$srcname" "$force_date"
|
||||
setx dest=abspath "$destdir/$destname"
|
||||
[ "$src" != "$dest" ] || ewarn "La source et la destination sont identiques"
|
||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||
"${cmd[@]}" "$src" "$destdir/$destname"
|
||||
|
||||
setx dest=abspath "$destdir/$destname"
|
||||
[ "$src" != "$dest" ] && "${cmd[@]}" "$src" "$destdir/$destname"
|
||||
else
|
||||
ewarn "$src: fichier introuvable"
|
||||
fi
|
||||
|
@ -198,30 +325,63 @@ elif [ "$action" == copy -o "$action" == move ]; then
|
|||
elif [ "$action" == create ]; then
|
||||
if [ $# -gt 0 ]; then
|
||||
for src in "$@"; do
|
||||
setx src=abspath "$src"
|
||||
setx dir=dirname -- "$src"
|
||||
setx srcname=basename -- "$src"
|
||||
if [ -e "$src" ]; then
|
||||
einfo "Renommage de $(ppath "$src")"
|
||||
setx destname=replace_ph "$srcname"
|
||||
estep "Renommage de $(ppath "$src")"
|
||||
setx destname=replace_ph "$srcname" "$force_date"
|
||||
setx dest=abspath "$dir/$destname"
|
||||
[ "$src" != "$dest" ] || ewarn "La source et la destination sont identiques"
|
||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||
mv -i "$src" "$dir/$destname"
|
||||
|
||||
setx dest=abspath "$dir/$destname"
|
||||
[ "$src" != "$dest" ] && mv -i "$src" "$dir/$destname"
|
||||
else
|
||||
einfo "Création de $(ppath "$src")"
|
||||
setx destname=replace_ph "$srcname"
|
||||
setx destname=replace_ph "$srcname" "$force_date"
|
||||
setx dest=abspath "$dir/$destname"
|
||||
if [ -d "$dest" ]; then
|
||||
ewarn "$srcname --> $(ppath "$dest"): répertoire existant"
|
||||
elif [ -e "$dest" ]; then
|
||||
ewarn "$srcname --> $(ppath "$dest"): fichier existant"
|
||||
else
|
||||
estep "$srcname --> Création de $(ppath "$dest")"
|
||||
fi
|
||||
read_value "Veuillez confirmer le nom" destname "$destname"
|
||||
|
||||
setx dest=abspath "$dir/$destname"
|
||||
create "$dir/$destname"
|
||||
fi
|
||||
done
|
||||
else
|
||||
case "$create" in
|
||||
file) einfo "Création d'un nouveau fichier";;
|
||||
dir) einfo "Création d'un nouveau répertoire";;
|
||||
esac
|
||||
setx destname=replace_ph
|
||||
read_value "Veuillez entrer le nom" destname "$destname"
|
||||
setx destname=replace_ph "" "$force_date"
|
||||
confirm_action=entrer
|
||||
if [ -d "$destname" ]; then
|
||||
ewarn "$destname: répertoire existant"
|
||||
confirm_action=confirmer
|
||||
elif [ -e "$destname" ]; then
|
||||
ewarn "$destname: fichier existant"
|
||||
confirm_action=confirmer
|
||||
else
|
||||
case "$create" in
|
||||
file) estep "Création d'un nouveau fichier";;
|
||||
dir) estep "Création d'un nouveau répertoire";;
|
||||
esac
|
||||
fi
|
||||
|
||||
read_value "Veuillez $confirm_action le nom" destname "$destname"
|
||||
create "$destname"
|
||||
fi
|
||||
|
||||
elif [ "$action" == string ]; then
|
||||
if [ $# -gt 0 ]; then
|
||||
for string in "$@"; do
|
||||
replace_ph "$string" "$force_date"
|
||||
done
|
||||
else
|
||||
replace_ph "" "$force_date"
|
||||
fi
|
||||
|
||||
else
|
||||
die "$action: action non implémentée"
|
||||
fi
|
||||
|
|
|
@ -34,6 +34,7 @@ done
|
|||
./udist --nutools-makelinks
|
||||
./ulink --nutools-makelinks
|
||||
./doinplace --nutools-makelinks
|
||||
./fndate --nutools-makelinks
|
||||
|
||||
# complétion programmable
|
||||
rm -f ~/etc/bashrc.d/bash_completion.nutools # renommé
|
||||
|
|
Loading…
Reference in New Issue