-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.sh
More file actions
executable file
·112 lines (96 loc) · 2.35 KB
/
notify.sh
File metadata and controls
executable file
·112 lines (96 loc) · 2.35 KB
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
#!/usr/bin/env bash
set -euo pipefail
SKILL_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG_FILE="$SKILL_DIR/config.json"
DEFAULT_INPUT_SOUND="/System/Library/Sounds/Sosumi.aiff"
DEFAULT_DONE_SOUND="/System/Library/Sounds/Glass.aiff"
read_config() {
local key="$1"
local default="$2"
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "$default"
return
fi
local val
val=$(python3 -c "
import json
with open('$CONFIG_FILE') as f:
c = json.load(f)
v = c.get('$key')
print('' if v is None else v)
" 2>/dev/null || echo "")
if [[ -z "$val" ]]; then
echo "$default"
else
echo "$val"
fi
}
read_config_bool() {
local key="$1"
local default="$2"
if [[ ! -f "$CONFIG_FILE" ]]; then
echo "$default"
return
fi
local val
val=$(python3 -c "
import json
with open('$CONFIG_FILE') as f:
c = json.load(f)
v = c.get('$key')
if v is None:
print('$default')
else:
print('true' if v else 'false')
" 2>/dev/null || echo "$default")
echo "$val"
}
play_sound() {
local sound_file="$1"
if [[ -f "$sound_file" ]]; then
afplay "$sound_file"
fi
}
show_alert() {
local title="$1"
local message="$2"
osascript -e "display notification \"$message\" with title \"$title\"" 2>/dev/null &
}
get_sound_file() {
local event="$1"
local custom_path
custom_path=$(read_config "${event}_sound" "")
if [[ -n "$custom_path" && -f "$custom_path" ]]; then
echo "$custom_path"
return
fi
case "$event" in
input) echo "$DEFAULT_INPUT_SOUND" ;;
done) echo "$DEFAULT_DONE_SOUND" ;;
esac
}
main() {
local event="${1:-}"
if [[ "$event" != "input" && "$event" != "done" ]]; then
echo "Usage: notify.sh <input|done>" >&2
exit 1
fi
local sound_enabled alert_enabled
sound_enabled=$(read_config_bool "sound_enabled" "true")
alert_enabled=$(read_config_bool "alert_enabled" "true")
local sound_file title message
if [[ "$sound_enabled" == "true" ]]; then
sound_file=$(get_sound_file "$event")
fi
if [[ "$alert_enabled" == "true" ]]; then
title=$(read_config "${event}_alert_title" "OpenCode")
message=$(read_config "${event}_alert_message" \
"$(if [[ "$event" == "input" ]]; then echo "User input required"; else echo "Work complete"; fi)")
show_alert "$title" "$message"
fi
if [[ "$sound_enabled" == "true" ]]; then
play_sound "$sound_file"
fi
wait 2>/dev/null || true
}
main "$@"