diff options
| -rwxr-xr-x | gromo | 152 | 
1 files changed, 102 insertions, 50 deletions
| @@ -27,88 +27,140 @@ TODAY_DIR=$DATA_DIR/$(date -I)  DING_FILE=$CACHE_DIR/ding.opus  STATE_FILE=${XDG_STATE_HOME:-$HOME/.local/state}/gromo -optspec="hlx" +cmd_help() { +    cmd=$(basename "$0") + +    echo -e "Usage: +    $cmd -h           \tShow this help +    $cmd -s           \tShow status +    $cmd -l           \tList past gromos +    $cmd -x           \tReturn state formatted for display in xmobar + +    $cmd [task]\tStart task + +Tasks can be divided in subtasks by using dots (.), e.g., 'gromo work.retro'. Only the first level +is taken into account for organization purposes: the top-level task is considered the 'main task' +and a file is created after it to account for its subtasks. +    " +} + +cmd_list() { +    find "$DATA_DIR/" -type f -print0 | xargs -0 -n 1 basename | sort -u +} + +cmd_status() { +    format=$1 + +    state=idle +    [ -f "$STATE_FILE" ] && state=$(cat "$STATE_FILE") + +    if [ "$format" = "xmobar" ]; then +        color=red +        [ "$state" = "idle" ] && color=green + +        if [ -d "$TODAY_DIR" ]; then +            today="" +            # needs nullglob +            for task_file in "$TODAY_DIR"/*; do +                today+="$(basename "$task_file"): $(sum_subtasks "$task_file"), " +            done +            [ -n "$today" ] && today=" (${today%, })" +        fi + +        echo "<fc=$color>$state</fc>$today" +    else +        echo -e "state: $state\n" + +        if [ -d "$TODAY_DIR" ]; then +            # needs nullglob +            for task_file in "$TODAY_DIR"/*; do +                sort < "$task_file" +            done +        fi +    fi +} + +sum_subtasks() { +    file=$1 +    awk -F: '{ sum += $2 } END { print sum }' "$file" +} + +inc_subtask() { +    task=$1 +    maintask="${fulltask%%.*}" +    task_file="$TODAY_DIR/$maintask" +    if [ -f "$task_file" ]; then +        awk -F: "\$1 == \"$task\" { \$2 += 1; added=1 } { print } END { if (!added) { print \"$task\" FS 1 } }" "$task_file" > tmp +        mv tmp "$task_file" +    else +        echo "$task 1" > "$task_file" +    fi +} + +pp_seconds() { +    seconds=$1 +    if [ "$seconds" -lt 60 ]; then +        echo "$seconds seconds" +    elif [ "$seconds" -lt 3600 ]; then +        echo "$((seconds / 60)) minutes" +    else +        echo "$((seconds / 3600)) hours" +    fi +} + +optspec="hslx"  while getopts "$optspec" optchar; do      case "$optchar" in          h) -            cmd=$(basename $0) -            echo -e "Usage: -    $cmd -h\tShow this help -    $cmd -l\tList past gromos -    $cmd -x\tReturn state formatted for display in xmobar - -    $cmd [task] [subtask]\tStart task (default: "work") -            " +            cmd_help +            exit 0 +            ;; +        s) +            cmd_status              exit 0              ;;          l) -            find $DATA_DIR/ -type f -print | xargs -n 1 basename | sort -u +            cmd_list              exit 0              ;;          x) -            state=idle -            [ -f "$STATE_FILE" ] && state=$(cat $STATE_FILE) - -            color=red -            [ "$state" = "idle" ] && color=green - -            if [ -d "$TODAY_DIR" ]; then -                declare -a today -                for gromo in "$TODAY_DIR"/*; do -                    today+=("$(basename $gromo): $(cat $gromo)") -                done -                [ -n "$today" ] && today=" (${today[@]})" -            fi -            echo "<fc=$color>$state</fc>$today" +            cmd_status xmobar +            exit 0 +            ;; +        *) +            cmd_help              exit 0              ;;      esac  done  if [ "$#" -ge 1 ]; then -    gromo="$1" -    desc="$2" +    fulltask="$1"  else -    gromo=work +    cmd_help +    exit 0  fi -gromos=0 -gromo_file="$TODAY_DIR/$gromo"  mkdir -p "$DATA_DIR" "$TODAY_DIR"  [ -f "$DING_FILE" ] || curl $DEFAULT_DING --create-dirs -so "$DING_FILE" -if [ -f $STATE_FILE ]; then +if [ -f "$STATE_FILE" ]; then      echo "Another instance is currently running; exiting (remove $STATE_FILE to override)"      exit 1  fi  echo idle > "$STATE_FILE" -trap 'rm $STATE_FILE 2> /dev/null && echo -e "\n\nToday: $gromos \"$gromo\" gromos ($gromo_time)"; exit' INT TERM EXIT +trap 'rm $STATE_FILE 2> /dev/null; exit' INT TERM EXIT  while true; do -    [ -f "$gromo_file" ] && read -r gromos < "$gromo_file" -    seconds=$((gromos * GROMO_SECONDS)) -    if [ $seconds -lt 60 ]; then -        gromo_time="$seconds seconds" -    elif [ $seconds -lt 3600 ]; then -        gromo_time="$((seconds / 60)) minutes" -    else -        gromo_time="$((seconds / 3600)) hours" -    fi - -    echo -ne "\r\033[K[** IN PROGRESS: $gromo **] " +    echo -ne "\r\033[K[** IN PROGRESS: $fulltask **] " -    if [ -n "$desc" ]; then -        echo "$gromo ($desc)" > "$STATE_FILE" -    else -        echo "$gromo" > "$STATE_FILE" -    fi +    echo "$fulltask" > "$STATE_FILE"      sleep $GROMO_SECONDS      echo idle > "$STATE_FILE" -    gromos=$((gromos+1)) -    echo "$gromos" > "$gromo_file" +    inc_subtask "$fulltask"      (sleep $STOP_SECONDS && mpv --no-terminal "$DING_FILE") & -    slock +    slock || exit  done | 
