Add support for multiple calendars using -c argument.

This commit is contained in:
Rob Pearce 2021-11-06 11:05:48 +11:00
parent e564188289
commit de26b27202
1 changed files with 94 additions and 57 deletions

139
t.sh
View File

@ -123,19 +123,6 @@ function getuid() {
return 0 return 0
} }
function getorgenid() {
local res uid v=0
uid=$1
res=$(getid $uid)
if [[ -z $res ]]; then
res=$nextid
idmap[$nextid]=$uid
nextid=$((nextid + 1))
needsave=1
fi
echo "$res"
}
function loadids() { function loadids() {
local LINE n uid id max count local LINE n uid id max count
count=0 count=0
@ -161,12 +148,26 @@ dblog "got $count IDs from $idfile"
} }
function saveids() { function saveids() {
local id local id verbose=0
local ess nids=0
[[ $1 -eq "-v" ]] && verbose=1
if [[ ${#idmap[@]} -gt 0 ]]; then
cp /dev/null $idfile cp /dev/null $idfile
for id in ${!idmap[@]}; do for id in ${!idmap[@]}; do
#dblog "save to $idfile: uid ${idmap[$id]} id $id sum ${tasksum[$id]}" >> $idfile #dblog "save to $idfile: uid ${idmap[$id]} id $id sum ${tasksum[$id]}" >> $idfile
echo "${idmap[$id]}:$id" >> $idfile echo "${idmap[$id]}:$id" >> $idfile
nids=$((nids + 1))
done done
[[ $nids -eq 1 ]] && ess="" || ess="s"
[[ $verbose -eq 1 ]] && action "Saved $nids ID mapping$ess to $idfile."
else
[[ $verbose -eq 1 ]] && warn "No ID mappings found to save"
return 1
fi
return 0
} }
function dumpids() { function dumpids() {
@ -178,10 +179,14 @@ function dumpids() {
function loadtasks() { function loadtasks() {
local f ess x thislev par parid show thissum thisdesc local f ess x thislev par parid show thissum thisdesc
local verbose=0
maxid=-1 maxid=-1
maxindent=-1 maxindent=-1
maxtaskwidth=-1 maxtaskwidth=-1
ntasks=0 ntasks=0
while [[ $# -ge 1 ]]; do
[[ $1 == "-v" ]] && verbose=1 && shift 1
done
dblog "Loading tasks..." dblog "Loading tasks..."
for f in $dir/*.vcf; do for f in $dir/*.vcf; do
dblog "--> loading from $f" dblog "--> loading from $f"
@ -216,6 +221,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."
dblog "Finished loading $ntasks task$ess." dblog "Finished loading $ntasks task$ess."
return 0 return 0
} }
@ -300,7 +306,13 @@ dblog "process token '${tok[$i]}'"
#nexti=$((i + 1)) #nexti=$((i + 1))
#dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[i+1]=${tok[$((i + 1))]} uid $uid id $id" #dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[i+1]=${tok[$((i + 1))]} uid $uid id $id"
#dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[nexti]=${tok[$nexti]} uid $uid id $id" #dblog "i=$i tok[i]=${tok[$i]} i+1=$((i + 1)) tok[nexti]=${tok[$nexti]} uid $uid id $id"
id=$(getorgenid $uid) id=$(getid $uid)
if [[ -z $id ]]; then
id=$nextid
idmap[$nextid]=$uid
nextid=$((nextid + 1))
needsave=1
fi
#dblog "i=$i tok[i]=${tok[$i]} uid $uid id $id" #dblog "i=$i tok[i]=${tok[$i]} uid $uid id $id"
elif [[ ${tok[$i]} == "sum" ]]; then elif [[ ${tok[$i]} == "sum" ]]; then
sum="${tok[$((i + 1))]}" sum="${tok[$((i + 1))]}"
@ -1103,6 +1115,24 @@ function getchildren() {
return 0 return 0
} }
function makedir() {
local d
d="$1"
if [[ -d "$d" ]]; then
info "Directory $d already exists"
else
mkdir -p "${d}"
if [[ $? -eq 0 ]]; then
action "Created directory ${BOLD}$d${PLAIN}"
else
error "Couldn't mkdir ${BOLD}$d${PLAIN}${RED}"
return 1
fi
fi
return 0
}
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" action "Initialising configration"
@ -1120,17 +1150,7 @@ function init() {
if [[ $gotvdconf -ne 1 ]]; then if [[ $gotvdconf -ne 1 ]]; then
for d in "$vdirsyncdir" "$vdircaldir" "$vdirstatdir"; do for d in "$vdirsyncdir" "$vdircaldir" "$vdirstatdir"; do
if [[ -d "$d" ]]; then makedir "$d" || return 1
info "Directory $d already exists"
else
mkdir -p "$vdirsyncdir"
if [[ $? -eq 0 ]]; then
action "Created directory ${BOLD}$d${PLAIN}"
else
error "Couldn't mkdir ${BOLD}$d${PLAIN}${RED}"
return 1
fi
fi
done done
echo -e "${MAGENTA}Please provide the URL of your caldav server.${PLAIN}" echo -e "${MAGENTA}Please provide the URL of your caldav server.${PLAIN}"
@ -1183,7 +1203,14 @@ EOF
count=$(echo "$cals" | wc -l | tr -d ' ') count=$(echo "$cals" | wc -l | tr -d ' ')
fi fi
if [[ ! -e "${confdir}/docs" ]] ; then makedir "$confdir" || return 1
if [[ -e "${allcalsdir}" ]] ; then
warn "Calendar symlink ${allcalsdir} already exists. Not overwriting it."
else
ln -s "${vdircaldir}" "${allcalsdir}"
action "Created symlink to $vdircaldir"
fi
if [[ ! -e "${defaultcalfile}" ]] ; then
cals_arr=() cals_arr=()
selcal="" selcal=""
if [[ $count -gt 0 ]]; then if [[ $count -gt 0 ]]; then
@ -1202,38 +1229,27 @@ EOF
read sel read sel
done done
selcal=${cals_arr[$(($sel - 1))]} selcal=${cals_arr[$(($sel - 1))]}
echo "$selcal" >"$defaultcalfile"
else else
warn "No calendars found." warn "No calendars found."
fi fi
fi fi
if [[ ! -d "$confdir" ]] ; then if [[ -z $selcal ]]; then
mkdir -p "$confdir"
action "Created $confdir"
fi
if [[ ! -e "${confdir}/docs" ]] ; then
if [[ -n $selcal ]]; then
ln -s "${vdircaldir}/${selcal}" "${confdir}/docs"
action "Created symlink to $selcal"
else
warn "No calendar selected " warn "No calendar selected "
echo "${YELLOW}Please manually symlink ${confdir}/docs to the required local directory under $vdircaldir" echo "${YELLOW}Please put default calendar name in ${defaultcalfile}"
selcal="<none>" echo "${YELLOW}then: touch ${idfile}_\$(cat $defaultcalfile)"
fi selcal=""
else else
warn "Calendar symlink ${confdir}/docs already exists. Not overwriting it." if [[ -e "${idfile}_${selcal}" ]]; then
selcal=$(basename $(readlink -n ${confdir}/docs)) warn "ID mapping file for $selcal already exists. Not overwriting it."
fi
if [[ -e "$idfile" ]]; then
warn "ID mapping file $idfile already exists. Not overwriting it."
else else
touch "$idfile" touch "${idfile}_${selcal}"
info "Created ID mapping file in ${idfile}" fi
fi fi
info "Initialisation complete" info "Initialisation complete"
[[ -z $selcal ]] && selcal="<none>"
echo -e "${CYAN} vdirsyncer config is in ${BOLD}${vdirsyncdir}${PLAIN}" echo -e "${CYAN} vdirsyncer config is in ${BOLD}${vdirsyncdir}${PLAIN}"
echo -e "${CYAN} task_cli config is in ${BOLD}${confdir}${PLAIN}" echo -e "${CYAN} task_cli config is in ${BOLD}${confdir}${PLAIN}"
echo -e "${CYAN} selected calendar is '${BOLD}${selcal}${PLAIN}'" echo -e "${CYAN} selected calendar is '${BOLD}${selcal}${PLAIN}'"
@ -1328,8 +1344,9 @@ DEFAULT_VDS_LOCAL=cal_local
DEFAULT_VDS_REMOTE=cal_remote DEFAULT_VDS_REMOTE=cal_remote
basedir=/Users/rpearce/code/task_cli basedir=/Users/rpearce/code/task_cli
confdir=${HOME}/.task_cli confdir=${HOME}/.task_cli
dir=$confdir/docs # symlink to vdirsyncer/calendar/xxx/ allcalsdir=$confdir/allcals # symlink to vdirsyncer/calendar/
idfile=$confdir/idmappings.txt defaultcalfile=$confdir/defaultcal
idfile=$confdir/idmappings
nextid=1 nextid=1
indent=0 indent=0
indentamt=4 indentamt=4
@ -1368,8 +1385,9 @@ addcmd "view" "view <id>" "Show detailed info for given task" "v|view|info|vcal"
DEBUG=0 DEBUG=0
TESTMODE=0 TESTMODE=0
AUTOSYNC=0 AUTOSYNC=0
CALID=$(cat "$confdir/defaultcal")
ARGS="fhidstyl:r:" ARGS="fhic:dstyl:r:"
while getopts "$ARGS" i; do while getopts "$ARGS" i; do
case "$i" in case "$i" in
h) h)
@ -1380,6 +1398,9 @@ while getopts "$ARGS" i; do
init init
exit $? exit $?
;; ;;
c)
CALID="$OPTARG"
;;
l) l)
VDS_LOCAL="$OPTARG" VDS_LOCAL="$OPTARG"
;; ;;
@ -1408,10 +1429,26 @@ while getopts "$ARGS" i; do
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
dir=$allcalsdir/$CALID
idfile=$confdir/idmappings_$CALID
if [[ ! -e $idfile ]]; then
oooo
warn "No ID mapping file found for calendar '$CALID' ($idfile)"
confirm "Create one from existing tasks?"
if [[ $? -eq 0 ]]; then
loadtasks -v
saveids -v || exit 1
else
die
fi
fi
[[ -z $VDS_LOCAL ]] && VDS_LOCAL="$DEFAULT_VDS_LOCAL" [[ -z $VDS_LOCAL ]] && VDS_LOCAL="$DEFAULT_VDS_LOCAL"
[[ -z $VDS_REMOTE ]] && VDS_REMOTE="$DEFAULT_VDS_REMOTE" [[ -z $VDS_REMOTE ]] && VDS_REMOTE="$DEFAULT_VDS_REMOTE"
mode="" mode=""
for x in $valid_modes; do for x in $valid_modes; do
thisone="words_$x" thisone="words_$x"