From 69571666aeb20c254ae231caae0ad216fa334a10 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Fri, 15 Oct 2021 11:55:01 +0200 Subject: [PATCH] Timew-function: modify the start of a time tracking --- zshrc | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/zshrc b/zshrc index b8a45e9..c3f8756 100644 --- a/zshrc +++ b/zshrc @@ -710,6 +710,74 @@ function tirm() { unset tirm_confirmation tirm_task_id } ## }}} +## Modify the start of a time tracking {{{ +function tistart() { + + # Define new_start empty by default + tistart_time_new_start="" + + # Verify argument + case "${#}" in + 1 ) + ## Get the first arg as time ID + tistart_time_id="${1}" + ;; + 2 ) + ## Get the first arg as time ID + tistart_time_id="${1}" + + ## Get the second arg as new start time for this track + tistart_time_new_start="${2}" + ;; + * ) + ## Ask the user to choose an ID in time tracking from the yesterday {{{ + timew summary from yesterday :ids || return 0 + + printf '%b' "Enter the ${MAGENTAB}ID${RESET} of the time tracking to modify : " + read -r tistart_time_id + ## }}} + ;; + esac + + # If no time tracking exists with this ID, exit function {{{ + if ! timew summary :year :ids | grep --quiet -- " @${tistart_time_id} "; then + printf '%b' "No available time tracking in the last year with ${REDB}${tistart_time_id}${RESET} ID." + return 1 + fi + # }}} + + # Get time tracking's description from all time tracking of this year + local tistart_time_desc=$(timew summary :year :ids | sed -n "s/.*@\(${tistart_time_id} .*\)/\1/p" | sed 's/ */ − /g') + + # Get time tracking's start day from all time tracking + local tistart_time_start_day=$(timew export | sed -nE "s/^\{\"id\":${tistart_time_id},\"start\":\"([0-9]{4})([0-9]{2})([0-9]{2})T.*end.*/\1-\2-\3/p") + + # Check or ask for new start time {{{ + if [ -z "${tistart_time_new_start}" ]; then + printf '%b' "Enter the ${MAGENTAB}new start time${RESET} (or new date 'YYYY-MM-DD${REDB}T${RESET}HH:MM:SS'; default day is ${tistart_time_start_day}) for this time tracking : " + read -r tistart_time_new_start + fi + # }}} + + printf '%b' "Change start time of \"${MAGENTAB}${tistart_time_desc}${RESET}\" to ${REDB}=> ${tistart_time_new_start} <=${RESET} [Y/n] ? " + read -r tistart_confirmation + + # Check confirmation + if printf -- '%s' "${tistart_confirmation:=y}" | grep --quiet --word-regexp -- "y"; then + ## If the new start time entered by user contains a "T" (user enter a dateTtime) + if printf -- '%s' "${tistart_time_new_start}" | grep --quiet -- "T"; then + printf '%b' "start time of ${tistart_time_desc} is now ${tistart_time_new_start}." + timew modify start @"${tistart_time_id}" "${tistart_time_new_start}" || return 0 + else + printf '%b' "start time of ${tistart_time_desc} is now ${tistart_time_start_day}T${tistart_time_new_start}." + timew modify start @"${tistart_time_id}" "${tistart_time_start_day}T${tistart_time_new_start}" || return 0 + fi + fi + + # Unset defined variables + unset tistart_confirmation tistart_time_id tistart_time_new_start +} +## }}} ## Modify the end of a time tracking {{{ function tiend() {