Add mod command to rename a task using either search/replace or sed command

This commit is contained in:
Rob Pearce 2021-11-25 20:27:36 +11:00
parent af108cbfc8
commit 0e8e774b69
1 changed files with 55 additions and 0 deletions

55
t.sh
View File

@ -706,6 +706,52 @@ function rename() { # rename uid new name goes here
return $rv
}
function sedmod() { # sedmod uid 'sed command'
local uid sedscript newname sedrv rv file
local id
uid=$1
id=$(getid $uid)
rv=0
shift
if [[ $# -ge 2 ]]; then
sedscript="s/$1/$2/g"
else
sedscript="$*"
fi
newname=$(echo "${tasksum[$id]}" | sed "$sedscript" 2>/dev/null )
if [[ $? -ne 0 ]]; then
error "Invalid sed script: $BOLD$sedscript"
return 1
fi
info "Current name: ${BOLD}${tasksum[$id]}"
info "New name: ${BOLD}${newname}"
confirm "Is this correct"
if [[ $? -ne 0 ]]; then
action "Aborted."
return 1
fi
file="$dir/$uid.vcf"
sed -i "s/^SUMMARY:.*/SUMMARY:$newname/" "$file"
sedrv=$?
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Renamed task #${id} to '$newname'."
loadtask "$file"
rv=0
else
error "failed to rename task #${id}."
rv=1
fi
return $rv
}
function toggle() {
local uid id rv children
uid=$1
@ -1486,6 +1532,7 @@ addcmd "add" "add [parent] <name>" "Add a new task [as subtask of parent]" "a|ad
addcmd "del" "del [id1] .. [idX]" "Delete given task(s)" "del|rm|delete"
addcmd "cleanup" "cleanup" "Delete all completed tasks" "cleanup|clean|flush|dc"
addcmd "rename" "rename <id> <newname>" "Rename given task" "rename"
addcmd "mod" "mod <id> (<sed_script>|<lookfor> <replacewith>)" "Rename given task using sed script" "mod|sed"
addcmd "left" "left <id>" "Decrease indent of given task" "left|h|out|up"
addcmd "right" "right <id> <parent>" "Move task below the given parent" "right|l|mv|in"
addcmd "note" "note <id> <notes>" "Change notes for given task" "note|desc|description|comment"
@ -1658,6 +1705,14 @@ elif [[ $mode == "rename" ]]; then
uid=$(getuid_witherror $1)
shift
rename $uid "$*"
elif [[ $mode == "mod" ]]; then
uid=$(getuid_witherror $1)
shift
if [[ $# -ge 2 ]]; then
sedmod $uid "$1" "$2"
else
sedmod $uid "$*"
fi
elif [[ $mode == "note" ]]; then
uid=$(getuid_witherror $1)
shift