users/ryan/modules/sketchybar/bar-config/plugins/bluetooth.sh

0ce4331f742863ec03c711c27b0a0dee7e8cc187 · 1.3 KB · 37 lines raw

1 #!/usr/bin/env bash
2 source "$CONFIG_DIR/colors.sh"
3 source "$CONFIG_DIR/icons.sh"
4
5 CACHE="/tmp/sketchybar_${NAME}.txt"
6 case "$SENDER" in
7 mouse.entered)
8 [ -f "$CACHE" ] && sketchybar --set "$NAME.details" label="$(cat "$CACHE")"
9 sketchybar --set "$NAME" popup.drawing=on; exit 0 ;;
10 mouse.exited)
11 sketchybar --set "$NAME" popup.drawing=off; exit 0 ;;
12 esac
13
14 if ! command -v blueutil >/dev/null 2>&1; then
15 sketchybar --set "$NAME" label="$BT_OFF" label.color=$TEXT_DIM
16 printf '%s' "blueutil not installed" > "$CACHE"
17 exit 0
18 fi
19
20 if [ "$1" = "toggle" ]; then
21 if [ "$(blueutil -p)" = "1" ]; then blueutil -p 0; else blueutil -p 1; fi
22 fi
23
24 if [ "$(blueutil -p)" = "1" ]; then
25 NAMES="$(blueutil --connected 2>/dev/null | grep -o 'name: "[^"]*"' | sed 's/name: "//; s/"$//' | paste -sd , - | sed 's/,/, /g')"
26 COUNT="$(blueutil --connected 2>/dev/null | grep -c 'name:')"
27 if [ "$COUNT" -gt 0 ]; then
28 sketchybar --set "$NAME" label="$BT_CONNECTED" label.color=$TEXT
29 printf '%s' "Bluetooth on · $COUNT connected: $NAMES" > "$CACHE"
30 else
31 sketchybar --set "$NAME" label="$BT_ON" label.color=$TEXT
32 printf '%s' "Bluetooth on · no devices connected" > "$CACHE"
33 fi
34 else
35 sketchybar --set "$NAME" label="$BT_OFF" label.color=$TEXT_DIM
36 printf '%s' "Bluetooth off" > "$CACHE"
37 fi