-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect-theme
More file actions
executable file
·40 lines (38 loc) · 1.24 KB
/
Copy pathdetect-theme
File metadata and controls
executable file
·40 lines (38 loc) · 1.24 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
#!/usr/bin/env bash
# Detect terminal dark/light mode. Prints "dark" or "light" to stdout.
# 1. macOS: `defaults read` (instant, no TTY needed)
# 2. OSC 11: query terminal background color (works over SSH, tmux)
# 3. Fallback: "dark"
# Fast path: macOS defaults (no TTY or network roundtrip needed)
if command -v defaults &>/dev/null; then
if defaults read -g AppleInterfaceStyle &>/dev/null; then
echo dark
else
echo light
fi
exit 0
fi
# OSC 11: query terminal background color via /dev/tty
exec 3<>/dev/tty 2>/dev/null || { echo dark; exit 0; }
old=$(stty -g </dev/tty 2>/dev/null)
stty raw -echo </dev/tty 2>/dev/null
printf '\033]11;?\033\\' >&3
r=
while IFS= read -r -t 3 -n 1 c <&3 2>/dev/null; do
r="$r$c"
case "$r" in *\\) break;; esac
if [ ${#r} -gt 60 ]; then break; fi
done
stty "$old" </dev/tty 2>/dev/null
exec 3>&-
case "$r" in
*rgb:*)
hex=$(echo "$r" | sed 's/.*rgb:\([^\\]*\).*/\1/')
R=$(printf '%d' "0x$(echo "$hex" | cut -d/ -f1 | cut -c1-2)")
G=$(printf '%d' "0x$(echo "$hex" | cut -d/ -f2 | cut -c1-2)")
B=$(printf '%d' "0x$(echo "$hex" | cut -d/ -f3 | cut -c1-2)")
lum=$(( (R * 299 + G * 587 + B * 114) / 1000 ))
if [ "$lum" -lt 128 ]; then echo dark; else echo light; fi
;;
*) echo dark ;;
esac