#!/bin/zsh # $1: Date / verbose # $2: Date / Description # $3: Description alias tils="timew summary :ids" calculate_time() { TASK_DESC="Sleep" if [[ -n "$1" ]]; then DAY="$1" else DAY="today" fi GET_INITIAL_SLEEP=$(tils 10h before "$DAY" "$TASK_DESC") GET_END_SLEEP=$(tils 10h after "$DAY" "$TASK_DESC") if [[ ! "$GET_INITIAL_SLEEP" =~ "No filtered data" ]]; then INITIAL_SLEEP=$(echo "$GET_INITIAL_SLEEP" | tail -n 2 | tr -d ' ' | tr -d '\n') IFS=":" read -r initial_hours initial_minutes initial_seconds <<< "$INITIAL_SLEEP" INITIAL_SECS=$((initial_hours*3600 + initial_minutes*60 + initial_seconds)) fi if [[ ! "$GET_END_SLEEP" =~ "No filtered data" ]]; then END_SLEEP=$(echo "$GET_END_SLEEP" | tail -n 2 | tr -d ' ' | tr -d '\n') IFS=":" read -r end_hours end_minutes end_seconds <<< "$END_SLEEP" END_SECS=$((end_hours*3600 + end_minutes*60 + end_seconds)) fi TOTAL_SECS=0; if [[ -n "$INITIAL_SECS" ]]; then TOTAL_SECS=$((TOTAL_SECS + INITIAL_SECS)) fi if [[ -n "$END_SECS" ]]; then TOTAL_SECS=$((TOTAL_SECS + END_SECS)) fi if [[ "$TOTAL_SECS" == "0" ]]; then HOURS=0 MINUTES=0 SECONDS=0 TIME_IN_BED=0 TIME_BEFORE_ASLEEP=0 SLEEP_POINTS=0 else HOURS=$((TOTAL_SECS / 3600)) MINUTES=$(( (TOTAL_SECS % 3600) / 60 )) SECONDS=$((TOTAL_SECS % 60)) TIME_IN_BED=$((TOTAL_SECS / 60)) fi printf "Total Sleep: %02d:%02d:%02d (%02dm)\n" \ "$HOURS" "$MINUTES" "$SECONDS" "$TIME_IN_BED" if [[ "$TOTAL_SECS" != "0" ]]; then printf "How long did it take before you fell sleep? (minutes): " read -r BEFORE_ASLEEP_TIME SLEEP_POINTS=$(sleepc.mjs $TIME_IN_BED $BEFORE_ASLEEP_TIME) fi echo "Sleep Points: $SLEEP_POINTS" SLEEP_TASK_UUID=$(task "Sleep" due.after:yesterday due.before:today uuids) if [[ -n "$SLEEP_TASK_UUID" ]]; then _ask_dialog "Do you want to record this in the sleep task?" || return yes no | task "$SLEEP_TASK_UUID" annotate "Time In Bed: $TIME_IN_BED (min)" yes no | task "$SLEEP_TASK_UUID" annotate "Time Before Asleep: $BEFORE_ASLEEP_TIME (min)" yes no | task "$SLEEP_TASK_UUID" annotate "Pure Sleep: $((TIME_IN_BED - BEFORE_ASLEEP_TIME)) (min)" yes no | task "$SLEEP_TASK_UUID" annotate "Sleep Points: $SLEEP_POINTS" fypm ta-done "$SLEEP_TASK_UUID" else echo "No sleep task found!" fi } if [[ -z "$1" ]]; then calculate_time today elif [[ "$1" == "-v" ]]; then if [[ -n "$2" ]]; then TASK_DAY="$2" else TASK_DAY="today" fi if [[ -n "$3" ]]; then TASK_DESC="$3" else TASK_DESC="Sleep" fi tils 10h before "$TASK_DAY" "$TASK_DESC" tils 10h after "$TASK_DAY" "$TASK_DESC" else calculate_time "$1" "$2" fi