~tardypad/dotfiles

ref: 00c0c349a1b8960152233f11caa9ea1a40e395a3 dotfiles/files/scripts/shell/do-not-disturb -rwxr-xr-x 2.2 KiB
00c0c349 — Damien Tardy-Panis Add presentation mode to mako to hide notifications 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
#!/bin/sh

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

  FILE_PATH="${XDG_RUNTIME_DIR:-/tmp}/do_not_disturb"

  ACTION='status'
  DURATION_MIN=
  WORK=
}

parse_command_line() {
  while getopts d:u:U:w: OPT; do
    case "${OPT}" in
      d) DURATION_MIN="${OPTARG}" ;;
      w) WORK="${OPTARG}" ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))

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

validate_command_line() {
  if [ "${ACTION}" != 'enable' ] \
     && [ "${ACTION}" != 'disable' ] \
     && [ "${ACTION}" != 'toggle' ] \
     && [ "${ACTION}" != 'remote-status' ] \
     && [ "${ACTION}" != 'status' ]; then
    exit_error "Invalid action '${ACTION}'"
  fi

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

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

end_time() {
  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
  TIME_MIN=$((
    ${CUR_HOURS#0} * 60 + ${CUR_MINUTES#0} + DURATION_MIN
  ))

  TIME_H=$(( TIME_MIN / 60 ))
  TIME_M=$(( TIME_MIN % 60 ))
  printf '%02dH%02d' "${TIME_H}" "${TIME_M}"
}

dnd_enable() {
  STATUS_TEXT=

  if [ -n "${DURATION_MIN}" ]; then
    STATUS_TEXT="until $( end_time )"
    ( sleep $(( DURATION_MIN * 60 )) && dnd_disable )&
  fi

  if [ -n "${WORK}" ]; then
    STATUS_TEXT="${STATUS_TEXT} working on ${WORK}"
  fi

  # shellcheck disable=SC2029
  ssh surg bin/set-status busy "${STATUS_TEXT}" 2> /dev/null &

  touch "${FILE_PATH}" \
    && update_i3blocks
}

dnd_disable() {
  ssh surg bin/set-status available 2> /dev/null &

  rm -f "${FILE_PATH}" \
    && update_i3blocks
}

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

dnd_status() {
  if is_dnd_enabled; then
    echo 'enabled'
  else
    echo 'disabled'
  fi
}

dnd_toggle() {
  if is_dnd_enabled; then
    dnd_disable
  else
    dnd_enable
  fi
}

is_dnd_enabled() {
  [ -f "${FILE_PATH}" ]
}

dnd_remote_status() {
  curl -s -H 'Accept: text/plain' https://status.tardypad.me \
    | grep -v '^$'
}

init_variables
parse_command_line "$@"
validate_command_line

"dnd_$( echo "${ACTION}" | tr '-' '_' )"