~tardypad/dotfiles

ref: f37f64d54c959fb7ec9796c00b8a3f7d4bbbd08c dotfiles/files/scripts/shell/time-tracker -rwxr-xr-x 4.0 KiB
f37f64d5 — Damien Tardy-Panis Only show task name if -n option is set 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  CURRENT_FILE_PATH="${XDG_RUNTIME_DIR:-/tmp}/time-tracker"
  LOGGING_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/time-tracker/logs"

  ACTION='show'
  TASK_ID=
  TASK_DESC=
  TASK_DURATION=
  LOGGING_DAY=$( date +%Y-%m-%d )
  GROUPED_DISPLAY=false
  TAB_SEPARATED=false
}

parse_command_line() {
  while getopts gt OPT; do
    case "${OPT}" in
      g) GROUPED_DISPLAY=true ;;
      t) TAB_SEPARATED=true ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))

  [ -z "$1" ] || ACTION="$1"

  if [ "${ACTION}" = 'start' ]; then
    TASK_ID="$2"
    TASK_DESC="$3"
  elif [ "${ACTION}" = 'log' ]; then
    TASK_ID="$2"
    TASK_DURATION="$3"
    TASK_DESC="$4"
  elif [ "${ACTION}" = 'edit' ] || [ "${ACTION}" = 'overview' ]; then
    [ -z "$2" ] || LOGGING_DAY="$2"
  fi
}

validate_command_line() {
  if [ "${ACTION}" != 'start' ] \
     && [ "${ACTION}" != 'show' ] \
     && [ "${ACTION}" != 'stop' ] \
     && [ "${ACTION}" != 'log' ] \
     && [ "${ACTION}" != 'overview' ] \
     && [ "${ACTION}" != 'edit' ]; then
    exit_error "Invalid action '${ACTION}'"
  fi

  if [ "${ACTION}" = 'start' ] && [ -z "${TASK_ID}" ]; then
    exit_error 'Missing task identifier'
  fi

  if [ "${ACTION}" = 'log' ]; then
    if [ -z "${TASK_ID}" ]; then
      exit_error 'Missing task identifier'
    fi
    if [ -z "${TASK_DURATION}" ]; then
      exit_error 'Missing task duration'
    fi

    case "${TASK_DURATION}" in
      *[!0-9]*) exit_error 'task duration must be an integer' ;;
    esac
  fi

  if [ "${ACTION}" = 'edit' ] || [ "${ACTION}" = 'overview' ]; then
    case "${LOGGING_DAY}" in
      [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) ;;
      *) exit_error 'Invalid day format' ;;
    esac
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

current_time_min() {
  CUR_HOURS=$( date +%H )
  CUR_MINUTES=$( date +%M )
  # values with leading 0 are interpreted as octals
  # which leads to issue with "08" and "09"
  # so we need to remove them
  echo $((
    ${CUR_HOURS#0} * 60 + ${CUR_MINUTES#0}
  ))
}

time_tracker_start() {
  if is_tracking_task; then
    time_tracker_stop
  fi

  CURRENT_TIME=$( current_time_min )

  printf '%s\t%s\t%s' \
    "${CURRENT_TIME}" "${TASK_ID}" "${TASK_DESC}" \
    > "${CURRENT_FILE_PATH}"
}

time_tracker_show() {
  if ! is_tracking_task; then
    echo 'No current task being tracked' >&2
    return 1
  fi

  cut -f 2- "${CURRENT_FILE_PATH}"
}

time_tracker_stop() {
  if ! is_tracking_task; then
    echo 'No current task being tracked' >&2
    return 1
  fi

  START_TIME=$( cut -f 1 "${CURRENT_FILE_PATH}" )

  CURRENT_DAY=$( date +%Y-%m-%d )
  CURRENT_TIME=$( current_time_min )
  CURRENT_DURATION=$(( CURRENT_TIME - START_TIME ))
  CURRENT_ID=$( cut -f 2 "${CURRENT_FILE_PATH}" )
  CURRENT_DESC=$( cut -f 3 "${CURRENT_FILE_PATH}" )

  printf '%s\t%s\t%s\n' \
    "${CURRENT_ID}" "${CURRENT_DURATION}" "${CURRENT_DESC}" \
    >> "${LOGGING_DIR}/${CURRENT_DAY}"

  rm -rf "${CURRENT_FILE_PATH}"
}

is_tracking_task() {
  [ -f "${CURRENT_FILE_PATH}" ]
}

time_tracker_log()
{
  CURRENT_DAY=$( date +%Y-%m-%d )

  printf '%s\t%s\t%s\n' \
    "${TASK_ID}" "${TASK_DURATION}" "${TASK_DESC}" \
    >> "${LOGGING_DIR}/${CURRENT_DAY}"
}

time_tracker_edit()
{
  "${EDITOR:-vi}" "${LOGGING_DIR}/${LOGGING_DAY}"
}

time_tracker_overview()
{
  if [ ! -f "${LOGGING_DIR}/${LOGGING_DAY}" ]; then
    return
  fi

  if [ "${TAB_SEPARATED}" = 'false' ]; then
    FORMAT_CMD='column -s "	" -t'
  else
    FORMAT_CMD='cat'
  fi

  if [ "${GROUPED_DISPLAY}" = 'false' ]; then
    eval "${FORMAT_CMD}" "${LOGGING_DIR}/${LOGGING_DAY}"
    return
  fi

  awk -v FS='\t' -v OFS='\t' \
    '{
      sum[$1] += $2;
      if ($3 && desc[$1] !~ $3", ")
        desc[$1] = desc[$1] $3", "
     }
    END {
      for (id in sum)
      print id, sum[id], desc[id]
    }' \
    "${LOGGING_DIR}/${LOGGING_DAY}" \
    | sed 's/, $//' \
    | eval "${FORMAT_CMD}"
}

init_variables
parse_command_line "$@"
validate_command_line

mkdir -p "${LOGGING_DIR}"
"time_tracker_${ACTION}"