aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/sketchybar/bar-config/plugins/bluetooth.sh
blob: 80185c0679fbffdcad367014b07166f8fdd99678 (plain)
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
#!/usr/bin/env bash
source "$CONFIG_DIR/colors.sh"
source "$CONFIG_DIR/icons.sh"

CACHE="/tmp/sketchybar_${NAME}.txt"
case "$SENDER" in
  mouse.entered)
    [ -f "$CACHE" ] && sketchybar --set "$NAME.details" label="$(cat "$CACHE")"
    sketchybar --set "$NAME" popup.drawing=on; exit 0 ;;
  mouse.exited)
    sketchybar --set "$NAME" popup.drawing=off; exit 0 ;;
esac

if ! command -v blueutil >/dev/null 2>&1; then
  sketchybar --set "$NAME" label="$BT_OFF" label.color=$TEXT_DIM
  printf '%s' "blueutil not installed" > "$CACHE"
  exit 0
fi

if [ "$1" = "toggle" ]; then
  if [ "$(blueutil -p)" = "1" ]; then blueutil -p 0; else blueutil -p 1; fi
fi

if [ "$(blueutil -p)" = "1" ]; then
  NAMES="$(blueutil --connected 2>/dev/null | grep -o 'name: "[^"]*"' | sed 's/name: "//; s/"$//' | paste -sd , - | sed 's/,/, /g')"
  COUNT="$(blueutil --connected 2>/dev/null | grep -c 'name:')"
  if [ "$COUNT" -gt 0 ]; then
    sketchybar --set "$NAME" label="$BT_CONNECTED" label.color=$TEXT
    printf '%s' "Bluetooth on · $COUNT connected: $NAMES" > "$CACHE"
  else
    sketchybar --set "$NAME" label="$BT_ON" label.color=$TEXT
    printf '%s' "Bluetooth on · no devices connected" > "$CACHE"
  fi
else
  sketchybar --set "$NAME" label="$BT_OFF" label.color=$TEXT_DIM
  printf '%s' "Bluetooth off" > "$CACHE"
fi