~tardypad/dotfiles

ref: 52edb52002d004d9fdc50737a5ed70202c75b268 dotfiles/files/scripts/shell/task -rwxr-xr-x 2.4 KiB
52edb520 — Damien Tardy-Panis Rename task and time-tracker description to comment 3 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
#!/bin/sh

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

  CURRENT_FILE_PATH="${XDG_RUNTIME_DIR:-/tmp}/task"
  TASKS_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/tasks"

  ACTION='show'
  TASK_ID=
  TASK_NAME=
  TASK_COMMENT=
  LOG=false
  ID_WITH_NAME=false
}

parse_command_line() {
  while getopts nl OPT; do
    case "${OPT}" in
      n) ID_WITH_NAME=true ;;
      l) LOG=true ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))

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

  if [ "${ACTION}" = 'create' ]; then
    TASK_ID="$2"
    TASK_NAME="$3"
  elif [ "${ACTION}" = 'pick' ]; then
    TASK_ID="$2"
    TASK_COMMENT="$3"
  fi
}

validate_command_line() {
  if [ "${ACTION}" != 'create' ] \
     && [ "${ACTION}" != 'ids' ] \
     && [ "${ACTION}" != 'pick' ] \
     && [ "${ACTION}" != 'show' ] \
     && [ "${ACTION}" != 'drop' ]; then
    exit_error "Invalid action '${ACTION}'"
  fi

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

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

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

task_create() {
  printf '%s\t%s\n' "${TASK_ID}" "${TASK_NAME}" \
    >> "${TASKS_DIR}"/local
}

task_pick() {
  if has_current_task; then
    task_drop
  fi

  if [ "${LOG}" = 'true' ]; then
    time-tracker start "${TASK_ID}" "${TASK_COMMENT}"
  fi

  printf '%s' "${TASK_ID}" > "${CURRENT_FILE_PATH}" \
    && update_i3blocks
}

task_show() {
  if ! has_current_task; then
    echo 'No current task' >&2
    return 1
  fi

  TASK_ID="$( cat "${CURRENT_FILE_PATH}" )"

  if [ "${ID_WITH_NAME}" = 'true' ]; then
    TASK_NAME="$(
      sed -n "s/^${TASK_ID}	\(.*\)/\1/p" "${TASKS_DIR}"/* 2> /dev/null \
        | sed 1q
    )"
    printf '%s\t%s\n' "${TASK_ID}" "${TASK_NAME}"
  else
    printf '%s\n' "${TASK_ID}"
  fi
}

task_drop() {
  if ! has_current_task; then
    echo 'No current task' >&2
    return 1
  fi

  time-tracker stop 2> /dev/null

  rm -rf "${CURRENT_FILE_PATH}" \
    && update_i3blocks
}

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

task_ids()
{
  RANGE='1'
  if [ "${ID_WITH_NAME}" = 'true' ]; then
    RANGE='1-2'
  fi

  cut -f"${RANGE}" "${TASKS_DIR}"/* 2> /dev/null \
    | sort -u
}

update_i3blocks() {
  pkill -x -RTMIN+6 i3blocks
}

init_variables
parse_command_line "$@"
validate_command_line

mkdir -p "${TASKS_DIR}"
"task_${ACTION}"