Bash · 1601 bytes Raw Blame History
1 #!/bin/zsh
2 # dynamic battery icon script for SketchyBar
3 #
4 # NOTE: the code points for icons.
5 # I'm using FA icons, which I pay for, but they have a
6 # free set you can add w. package manager
7
8 COLOR_GOOD=0xff81A1C1
9 COLOR_NORMAL=0xffEBCB8B
10 COLOR_CHARGING=0xffA3BE8C
11 COLOR_CRITICAL=0xffBF616A
12
13 # default to bad news
14 color=$COLOR_CRITICAL
15
16 # Read PMSET output: first line for state, second for stats
17 raw_state=$(pmset -g batt | head -1)
18 raw=$(pmset -g batt | tail -1)
19
20 # check if charging or discharging
21 if echo "$raw_state" | grep -q "AC Power"; then
22 state="charging"
23 else
24 state="discharging"
25 fi
26
27 # extract numeric percentage (and strip the '%')
28 percent=$(echo "$raw" | grep -Eo '[0-9]+%' | head -1 | tr -d '%')
29
30 # select icon based on state and percentage
31 # TODO: @espadonne (mfw) case and cascade
32 # on the color transitions
33 if [ "$state" = "charging" ]; then
34 icon=$'\uf376'
35 color=$COLOR_CHARGING
36 elif [ "$percent" -ge 90 ]; then
37 icon=$'\uf240'
38 color=$COLOR_GOOD
39 elif [ "$percent" -ge 65 ]; then
40 icon=$'\uf241'
41 color=$COLOR_GOOD
42 elif [ "$percent" -ge 50 ]; then
43 icon=$'\uf241'
44 color=$COLOR_NORMAL
45 elif [ "$percent" -ge 40 ]; then
46 icon=$'\uf242'
47 color=$COLOR_NORMAL
48 elif [ "$percent" -ge 20 ]; then
49 icon=$'\uf243'
50 color=$COLOR_NORMAL
51 elif [ "$percent" -ge 15 ]; then
52 icon=$'\uf243'
53 color=$COLOR_CRITICAL
54 elif [ "$percent" -ge 5 ]; then
55 icon=$'\ue0b1'
56 color=$COLOR_CRITICAL
57 else
58 icon=$'\uf377'
59 color=$COLOR_CRITICAL
60 fi
61
62 sketchybar --set "$NAME" icon=$icon
63 sketchybar --set "$NAME" icon.color="${color}"
64 sketchybar --set "$NAME" label="${percent}%"