dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

55 lines2262 bytesBourne Again Shell (bash)executable
1#!/usr/bin/env bash
2# Battery of a wireless Logitech mouse (G Pro X Superlight) as one line of JSON.
3# Used by the Eww control center (and by a Waybar custom module if you re-add one).
4# When the mouse isn't found: "connected":false and an empty "text" (Waybar hides the module).
5#
6# Source 1: the kernel's HID++ driver, which exposes the mouse under
7# /sys/class/power_supply/hidpp_battery_N (the number changes between
8# reconnects, so we glob instead of hardcoding it).
9# Source 2: Solaar (`solaar show`), used only if source 1 finds nothing.
10#
11# MOUSE_MATCH is matched (case-insensitive) against the device name.
12
13MATCH="${MOUSE_MATCH:-superlight}"
14cap=""; status=""; name=""
15
16for d in /sys/class/power_supply/hidpp_battery_*; do
17 [ -d "$d" ] || continue
18 model=$(tr -d '"\\' < "$d/model_name" 2>/dev/null)
19 if [ -z "$cap" ] || echo "$model" | grep -qi "$MATCH"; then
20 c=$(cat "$d/capacity" 2>/dev/null)
21 if [ -n "$c" ]; then
22 cap=$c
23 status=$(cat "$d/status" 2>/dev/null)
24 name=$model
25 fi
26 fi
27done
28
29if [ -z "$cap" ] && command -v solaar >/dev/null 2>&1; then
30 out=$(timeout 8 solaar show "$MATCH" 2>/dev/null)
31 line=$(echo "$out" | grep -i 'Battery:' | head -1)
32 cap=$(echo "$line" | grep -oE '[0-9]+%' | head -1 | tr -d '%')
33 status=$(echo "$line" | grep -oiE 'discharging|charging|recharging|full' | head -1)
34 name="Logitech mouse (via Solaar)"
35fi
36
37if [ -z "$cap" ]; then
38 echo '{"text":"","percentage":0,"class":"","tooltip":"","label":"Not connected","connected":false}'
39 exit 0
40fi
41
42# Icon: 5 steps from full to empty (charging gets its own icon).
43if [ "${status,,}" = "charging" ] || [ "${status,,}" = "recharging" ]; then icon="󰂄"; class="charging"
44elif [ "$cap" -gt 85 ]; then icon="󰁹"; class=""
45elif [ "$cap" -gt 60 ]; then icon="󰂀"; class=""
46elif [ "$cap" -gt 35 ]; then icon="󰁾"; class=""
47elif [ "$cap" -gt 15 ]; then icon="󰁼"; class="warning"
48else icon="󰂎"; class="critical"
49fi
50
51label="${cap}%"
52[ "$class" = "charging" ] && label="${cap}% · charging"
53
54printf '{"text":"%s","percentage":%s,"class":"%s","tooltip":"%s: %s%% %s","label":"%s","connected":true}\n' \
55 "$icon" "$cap" "$class" "${name:-Mouse}" "$cap" "${status:+($status)}" "$label"