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