-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.sh
More file actions
193 lines (167 loc) · 5.7 KB
/
notify.sh
File metadata and controls
193 lines (167 loc) · 5.7 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
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
192
193
#!/usr/bin/env bash
# notify.sh — Claude Code cross-platform desktop notification
# Supports: macOS (osascript / terminal-notifier), Linux (notify-send)
#
# Usage: notify.sh [info|success|warning|error]
# Reads hook JSON from stdin, same as notify.ps1
set -euo pipefail
# ============================================================
# Configuration
# ============================================================
ICON_DIR="$HOME/.claude"
ICON_PATH="$ICON_DIR/notify-icon.png"
APP_NAME="Claude Code"
NOTIFY_TYPE="${1:-info}"
# ============================================================
# Module: JSON Parsing (jq optional, python fallback)
# ============================================================
json_get() {
local json="$1" key="$2"
if command -v jq &>/dev/null; then
echo "$json" | jq -r ".$key // empty" 2>/dev/null || echo ""
return
fi
# python fallback (stdin to avoid ARG_MAX)
local py
py=$(command -v python3 || command -v python || true)
if [ -n "$py" ]; then
echo "$json" | "$py" -c "import sys,json; d=json.load(sys.stdin); print(d.get(sys.argv[1],''))" "$key" 2>/dev/null || echo ""
return
fi
# crude sed fallback
echo "$json" | sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" | head -1 || echo ""
}
read_stdin() {
cat
}
# ============================================================
# Module: Context Detection
# ============================================================
get_project_name() {
local cwd="$1"
[ -z "$cwd" ] && return
if command -v git &>/dev/null; then
local git_root
git_root=$(git -C "$cwd" rev-parse --show-toplevel 2>/dev/null || true)
if [ -n "$git_root" ]; then
local name branch
name=$(basename "$git_root")
branch=$(git -C "$cwd" branch --show-current 2>/dev/null || true)
if [ -n "$branch" ]; then
echo "$name [$branch]"
return
fi
echo "$name"
return
fi
fi
# fallback: last two path segments
echo "$cwd" | awk -F/ '{if(NF>=2) print $(NF-1)"/"$NF; else print $NF}'
}
get_terminal_identity() {
if [ -n "${WT_SESSION:-}" ]; then echo "Windows Terminal"; return; fi
case "${TERM_PROGRAM:-}" in
vscode) echo "VS Code"; return ;;
WezTerm) echo "WezTerm"; return ;;
alacritty) echo "Alacritty"; return ;;
iTerm.app) echo "iTerm2"; return ;;
ghostty) echo "Ghostty"; return ;;
esac
case "${TERM:-}" in
xterm-kitty) echo "Kitty"; return ;;
esac
echo "Terminal"
}
build_label() {
local project="$1" terminal="$2"
local result=""
[ -n "$project" ] && result="$project"
[ -n "$terminal" ] && result="${result:+$result · }$terminal"
echo "$result"
}
# ============================================================
# Module: Type Configuration
# ============================================================
get_type_config() {
case "$1" in
success) echo "Done|✓|Default|Task completed" ;;
warning) echo "Attention|⚠|IM|Claude needs your approval" ;;
error) echo "Error|✗|Reminder|An error occurred" ;;
*) echo "Idle|●|Default|Claude is waiting for your input" ;;
esac
}
# ============================================================
# Module: Platform Notification
# ============================================================
notify_macos() {
local title="$1" message="$2" sound="$3"
# Prefer terminal-notifier (supports custom icon)
if command -v terminal-notifier &>/dev/null; then
local icon_args=()
[ -f "$ICON_PATH" ] && icon_args=(-appIcon "$ICON_PATH")
terminal-notifier -title "$APP_NAME" -subtitle "$title" \
-message "$message" "${icon_args[@]}" -sound default 2>/dev/null && return
fi
# Fallback: osascript
local sound_name="default"
case "$sound" in
IM) sound_name="Submarine" ;;
Reminder) sound_name="Sosumi" ;;
esac
local safe_title="$title" safe_message="$message"
safe_title="${safe_title//\\/\\\\}"; safe_title="${safe_title//\"/\\\"}"
safe_message="${safe_message//\\/\\\\}"; safe_message="${safe_message//\"/\\\"}"
osascript -e "display notification \"$safe_message\" with title \"$APP_NAME\" subtitle \"$safe_title\" sound name \"$sound_name\"" 2>/dev/null
}
notify_linux() {
local title="$1" message="$2"
if ! command -v notify-send &>/dev/null; then
echo "[$title] $message"
return
fi
local icon_args=()
[ -f "$ICON_PATH" ] && icon_args=(-i "$ICON_PATH")
local urgency="low"
case "$NOTIFY_TYPE" in
error) urgency="critical" ;;
warning) urgency="normal" ;;
esac
notify-send -u "$urgency" "${icon_args[@]}" "$title" "$message" 2>/dev/null
}
send_notification() {
local title="$1" message="$2" sound="$3"
case "$(uname -s)" in
Darwin) notify_macos "$title" "$message" "$sound" ;;
Linux) notify_linux "$title" "$message" ;;
*) echo "[$title] $message" ;;
esac
}
# ============================================================
# Main
# ============================================================
main() {
local stdin_data
stdin_data=$(read_stdin)
local cwd message
cwd=$(json_get "$stdin_data" "cwd")
message=$(json_get "$stdin_data" "message")
[ -z "$cwd" ] && cwd="$PWD"
local project terminal label
project=$(get_project_name "$cwd")
terminal=$(get_terminal_identity)
label=$(build_label "$project" "$terminal")
# Parse type config
local type_config prefix emoji sound fallback
type_config=$(get_type_config "$NOTIFY_TYPE")
IFS='|' read -r prefix emoji sound fallback <<< "$type_config"
# Build title & message
local title
if [ -n "$label" ]; then
title="$emoji $prefix · $label"
else
title="$emoji $prefix"
fi
[ -z "$message" ] && message="$fallback"
send_notification "$title" "$message" "$sound"
}
main