Switch to use bashtools for helper functikns

This commit is contained in:
Rob Pearce 2022-03-13 11:28:18 +11:00
parent 44d24535e2
commit ef2c263cd6
1 changed files with 56 additions and 64 deletions

120
t.sh
View File

@ -3,21 +3,14 @@
trap "exit 1" TERM
export MYPID=$$
# ANSI stuff
BOLD="\033[1m"
ITALIC="\033[3m"
STRIKE="\033[9m"
PLAIN="\033[0m"
if [[ -z $HAVE_BASHTOOLS ]]; then
echo "ERROR: bashtools not installed download from https://git.nethack.net/rob/bashtools"
exit 1
fi
INFORMCOL="$GREEN"
INFORMCOLB="$GREENBOLD"
UNDERLINE="\033[4m"
RED="\033[31m"
MAGENTA="\033[35m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
CYAN="\033[36m"
GREY="\033[2;37m"
LINK="$BLUE$UNDERLINE"
brack="$CYAN"
brackfold="$CYAN"
HILITE="\033[43;1m"
@ -26,7 +19,6 @@ GREYBG="\033[48;2;30;30;30m"
TOPTITLE="\033[44;1m"
LEFTTITLE="\033[31;1m"
#FOLDBG="\033[100m"
FOLDBG="$BOLDD"
function die() {
@ -160,7 +152,7 @@ function saveids() {
nids=$((nids + 1))
done
[[ $nids -eq 1 ]] && ess="" || ess="s"
[[ $verbose -eq 1 ]] && action "Saved $nids ID mapping$ess to $idfile."
[[ $verbose -eq 1 ]] && inform "Saved $nids ID mapping$ess to $idfile."
else
[[ $verbose -eq 1 ]] && warn "No ID mappings found to save"
return 1
@ -220,7 +212,7 @@ function loadtasks() {
done
[[ $ntasks -eq 1 ]] && ess="" || ess="s"
[[ $verbose -eq 1 ]] && action "Finished loading $ntasks task$ess."
[[ $verbose -eq 1 ]] && inform "Finished loading $ntasks task$ess."
dblog "Finished loading $ntasks task$ess."
return 0
}
@ -249,6 +241,7 @@ function dblog() {
[[ $DEBUG -eq 1 ]] && echo "$(date) ${FUNCNAME[1]}() $*" >/dev/stderr
}
if [[ 1 -eq 0 ]]; then
function action() {
echo -e "$BOLD$GREEN* $PLAIN$GREEN$*$PLAIN"
}
@ -265,6 +258,7 @@ function info() {
[[ $AUTOYES -eq 1 ]] && return
echo -e "$BOLD${CYAN}>> $PLAIN$CYAN$*$PLAIN"
}
fi
function confirm() {
local yn
@ -285,7 +279,7 @@ function loadtask() {
dblog "open $1"
file="$1"
res=$(cat "$file" | awk -F: 'BEGIN { checked=0; folded=0; } /^UID:/ { printf("uid©%s©",$2); } /^SUMMARY:/ { sub("SUMMARY:",""); printf("sum©%s©",$0); } /^CATEGORIES:/ { sub("^CATEGORIES:",""); printf("cats©%s©",$0); } /^DESCRIPTION:/ { sub("DESCRIPTION:",""); printf("desc©%s©",$0); } /^REQUEST-STATUS:4.1;/ { sub("REQUEST-STATUS:4.1;",""); printf("breason©%s©",$0);} /^RELATED-TO/ { sub("^RELATED-TO.*:",""); printf("parent©%s©",$0); } /^X-OC-HIDESUBTASKS:1/ { folded=1; } /^STATUS:/ { stat=substr($0,8); if (stat == "COMPLETED") { checked=2; } else if (stat == "NEEDS-ACTION") { checked=3; } else if (stat == "IN-PROCESS") { checked = 1 } } END { printf("checked©%d©folded©%d\n",checked,folded); }')
res=$(cat "$file" | awk -F: 'BEGIN { checked=0; folded=0; } /^UID:/ { printf("uid©%s©",$2); } /^SUMMARY:/ { sub("SUMMARY:",""); printf("sum©%s©",$0); } /^CATEGORIES:/ { sub("^CATEGORIES:",""); printf("cats©%s©",$0); } /^DESCRIPTION:/ { sub("DESCRIPTION:",""); printf("desc©%s©",$0); } /^REQUEST-STATUS:4.1;/ { sub("REQUEST-STATUS:4.1;",""); printf("breason©%s©",$0);} /^RELATED-TO/ { sub("^RELATED-TO.*:",""); printf("parent©%s©",$0); } /^X-OC-HIDESUBTASKS:1/ { folded=1; } /^STATUS:/ { stat=substr($0,8); if (stat == "COMPLETED") { checked=2; } else if (stat == "NEEDS-inform") { checked=3; } else if (stat == "IN-PROCESS") { checked = 1 } } END { printf("checked©%d©folded©%d\n",checked,folded); }')
@ -392,7 +386,7 @@ function marknotdone() {
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Marked $noun #${id} as unfinished."
inform "Marked $noun #${id} as unfinished."
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -465,9 +459,9 @@ function delq_process() {
done
saveids
[[ $count -eq 1 ]] && ess="" || ess="s"
action "$count task$ess removed."
inform "$count task$ess removed."
else
action "Aborted."
inform "Aborted."
rv=1
fi
@ -507,7 +501,7 @@ function move_left() { # move_left uid
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Shifted task #${id} upwards/left."
inform "Shifted task #${id} upwards/left."
loadtask "$file"
showit ${taskparent[$id]} $uid
@ -561,7 +555,7 @@ function move_right() { # move_right uid new_parent
else
str="underneath #${newparentid} ('${tasksum[$newparentid]}')."
fi
action "Shifted task #${id} $str"
inform "Shifted task #${id} $str"
loadtask "$file"
showit ${taskparent[$id]} $uid
@ -605,9 +599,9 @@ function setnote() { # setnote uid new note goes here
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
if [[ -z $newnote ]]; then
action "Removed note from task #${id}."
inform "Removed note from task #${id}."
else
action "Set note of task #${id} to '$newnote'."
inform "Set note of task #${id} to '$newnote'."
fi
loadtask "$file"
rv=0
@ -619,23 +613,22 @@ function setnote() { # setnote uid new note goes here
return $rv
}
function modtag() { # setnote uid add|del new tag goes here
local uid arg sedrv rv file str
local id action x catarr newcat
local id inform x catarr newcat
uid=$1
id=$(getid $uid)
rv=0
shift
action=$1
inform=$1
shift
arg="$*"
file="$dir/$uid.vcf"
if [[ -z $arg ]]; then
if [[ $action == "del" ]]; then
if [[ $inform == "del" ]]; then
sed -i "/^CATEGORIES:/d" "$file"
sedrv=$?
else
@ -647,11 +640,11 @@ function modtag() { # setnote uid add|del new tag goes here
catarr=( ${taskcats[$id]} )
IFS=" "
for x in ${!catarr[@]} ; do
if [[ $action == "add" || ${catarr[$x]} != $arg ]]; then
if [[ $inform == "add" || ${catarr[$x]} != $arg ]]; then
[[ -z $newcat ]] && newcat="${catarr[$x]}" || newcat="$newcat,${catarr[$x]}"
fi
done
if [[ $action == "add" ]]; then
if [[ $inform == "add" ]]; then
[[ -z $newcat ]] && newcat="${arg}" || newcat="$newcat,$arg"
fi
@ -669,14 +662,14 @@ function modtag() { # setnote uid add|del new tag goes here
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
if [[ $action == "del" ]]; then
if [[ $inform == "del" ]]; then
if [[ -z $arg ]]; then
action "Removed all tags from task #${id}."
inform "Removed all tags from task #${id}."
else
action "Removed tag '$arg' from task #${id}."
inform "Removed tag '$arg' from task #${id}."
fi
else
action "Added tag '$arg' to task #${id}."
inform "Added tag '$arg' to task #${id}."
fi
loadtask "$file"
rv=0
@ -705,7 +698,7 @@ function rename() { # rename uid new name goes here
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Renamed task #${id} to '$newname'."
inform "Renamed task #${id} to '$newname'."
loadtask "$file"
rv=0
else
@ -740,7 +733,7 @@ function sedmod() { # sedmod uid 'sed command'
info "New name: ${BOLD}${newname}"
confirm "Is this correct"
if [[ $? -ne 0 ]]; then
action "Aborted."
inform "Aborted."
return 1
fi
@ -751,7 +744,7 @@ function sedmod() { # sedmod uid 'sed command'
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Renamed task #${id} to '$newname'."
inform "Renamed task #${id} to '$newname'."
loadtask "$file"
rv=0
else
@ -820,7 +813,7 @@ function fold() {
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Folded task #${id}."
inform "Folded task #${id}."
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -864,7 +857,7 @@ function unfold() {
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Unfolded task #${id}."
inform "Unfolded task #${id}."
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -877,11 +870,10 @@ function unfold() {
return $rv
}
function clearstatus() {
local file
file="$1"
[[ -e $file ]] && sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/^STATUS:IN-PROCESS:/d;/^STATUS:NEEDS-ACTION/d;/^$/d;/^REQUEST-STATUS:/d;' "$file"
[[ -e $file ]] && sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/^STATUS:IN-PROCESS:/d;/^STATUS:NEEDS-inform/d;/^$/d;/^REQUEST-STATUS:/d;' "$file"
}
function markblocked() { # $1=uid $2=reason
@ -905,10 +897,10 @@ function markblocked() { # $1=uid $2=reason
file="$dir/$uid.vcf"
# first remove any existing completion
#sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/STATUS:IN-PROCESS:/d;/STATUS:NEEDS-ACTION/d' "$file"
#sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/STATUS:IN-PROCESS:/d;/STATUS:NEEDS-inform/d' "$file"
clearstatus "$file"
# then add blocked
toadd1="STATUS:NEEDS-ACTION"
toadd1="STATUS:NEEDS-inform"
sed -i "/$str/i $toadd1\n" "$file"
sedrv=$?
if [[ -n $reason ]]; then
@ -926,7 +918,7 @@ function markblocked() { # $1=uid $2=reason
txt="${txt} due to ${BOLD}$reason$PLAIN"
fi
txt="${txt}."
action "$txt"
inform "$txt"
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -965,7 +957,7 @@ function markinprogress() {
file="$dir/$uid.vcf"
# first remove any existing completion
#sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/STATUS:NEEDS-ACTION/d;' "$file"
#sed -i '/^COMPLETED:/d;/^STATUS:COMPLETED/d;/PERCENT-COMPLETE:/d;/STATUS:NEEDS-inform/d;' "$file"
clearstatus "$file"
# then add partial completion
@ -975,7 +967,7 @@ function markinprogress() {
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Marked $noun #${id} as in-progress."
inform "Marked $noun #${id} as in-progress."
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -1017,7 +1009,7 @@ function markdone() {
set_lastmodified $uid
sedrv=$((sedrv + $?))
if [[ $sedrv -eq 0 ]]; then
action "Marked $noun #${id} as completed."
inform "Marked $noun #${id} as completed."
processed="$processed ${uid} "
loadtask "$file"
rv=0
@ -1041,7 +1033,7 @@ function viewtask() {
id=$(getid $uid)
file="$dir/$uid.vcf"
action "Viewing task #$id ($file):"
inform "Viewing task #$id ($file):"
cat "$file" | sed -e 's/^/ /'
return 0
}
@ -1113,10 +1105,10 @@ EOF2
saveids
needtitle=1
if [[ -z ${parentuid} ]]; then
action "Created new task #${taskid[$thisid]}."
inform "Created new task #${taskid[$thisid]}."
showit $uid
else
action "Created new subtask #${taskid[$thisid]} under '${tasksum[${parentid}]}'."
inform "Created new subtask #${taskid[$thisid]} under '${tasksum[${parentid}]}'."
showit ${taskparent[$thisid]} $uid
fi
}
@ -1348,7 +1340,7 @@ function makedir() {
else
mkdir -p "${d}"
if [[ $? -eq 0 ]]; then
action "Created directory ${BOLD}$d${PLAIN}"
inform "Created directory ${BOLD}$d${PLAIN}"
else
error "Couldn't mkdir ${BOLD}$d${PLAIN}${RED}"
return 1
@ -1359,7 +1351,7 @@ function makedir() {
function init() {
local gotvdconf=0 d url count cals cals_arr thiscal selcal
action "Initialising configration"
inform "Initialising configration"
if ! which -s vdirsyncer; then
error "Can't find ${BOLD}vdirsyncer${PLAIN}${RED} - please install it."
@ -1418,9 +1410,9 @@ EOF
count=$(echo "$cals" | wc -l | tr -d ' ')
if [[ $count -eq 0 ]]; then
action "Running initial vdirsyncer discover..."
inform "Running initial vdirsyncer discover..."
vdirsyncer discover
action "Running initial vdirsyncer sync..."
inform "Running initial vdirsyncer sync..."
vdirsyncer sync
fi
@ -1431,7 +1423,7 @@ EOF
warn "Calendar symlink ${allcalsdir} already exists. Not overwriting it."
else
ln -s "${vdircaldir}" "${allcalsdir}"
action "Created symlink to $vdircaldir"
inform "Created symlink to $vdircaldir"
fi
if [[ ! -e "${defaultcalfile}" ]] ; then
selcal=""
@ -1495,13 +1487,13 @@ function tasksync() {
[[ $downcount -eq 1 ]] && downess="" || downess="s"
if [[ $upcount -eq 0 && $downcount -eq 0 ]]; then
action "$noun complete - no changes."
inform "$noun complete - no changes."
elif [[ $upcount -eq 0 ]]; then
action "$noun complete - $downcount change$downess pulled."
inform "$noun complete - $downcount change$downess pulled."
elif [[ $downcount -eq 0 ]]; then
action "$noun complete - $upcount change$upess pushed."
inform "$noun complete - $upcount change$upess pushed."
else
action "$noun complete - $upcount change$upess pushed, $downcount change$downess pulled."
inform "$noun complete - $upcount change$upess pushed, $downcount change$downess pulled."
fi
else
error "$noun failed. Output:"
@ -1792,7 +1784,7 @@ elif [[ $mode == "setcal" ]]; then
fi
foundcal=$(getcalname $1)
if [[ -n $foundcal ]]; then
action "Default calendar set to '${foundcal}'"
inform "Default calendar set to '${foundcal}'"
echo "${foundcal}" > "$defaultcalfile"
else
error "Calendar '$1' not found"
@ -1853,7 +1845,7 @@ elif [[ $mode == "view" ]]; then
uid=$(getuid_witherror $1)
viewtask $uid
elif [[ $mode == "cleanup" ]]; then
action "Deleting all completed tasks."
inform "Deleting all completed tasks."
for id in ${!taskuid[@]}; do
uid=$(getuid $id)
[[ ${taskchecked[$id]} -eq 2 ]] && delq_add $uid
@ -1938,7 +1930,7 @@ else
done
confirm "Really proceed"
if [[ $? -ne 0 ]]; then
action "Aborted."
inform "Aborted."
exit 1
fi
fi
@ -1993,7 +1985,7 @@ else
fi
done
# do queued actions
# do queued informs
if [[ $mode == "del" ]]; then
delq_process
fi