Skip to content

Commit

Permalink
feat(modules/waybar): Improve battery script for the mxergo
Browse files Browse the repository at this point in the history
  • Loading branch information
etu committed Sep 16, 2024
1 parent d703174 commit 90f4605
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions modules/graphical/window-managers/waybar/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,36 @@
interval = 60;
tooltip = true;
return-type = "json";
exec = let
awk = "${pkgs.gawk}/bin/awk";
grep = "${pkgs.gnugrep}/bin/grep";
printf = "${pkgs.coreutils}/bin/printf";
sed = "${pkgs.gnused}/bin/sed";
test = "${pkgs.coreutils}/bin/test";
upower = "${config.services.upower.package}/bin/upower";
xargs = "${pkgs.findutils}/bin/xargs";
in
pkgs.writeShellScript "mxergo-battery" ''
DEVICE_PATH=$(${upower} -e | ${grep} battery_hidpp_battery)
exec = lib.getExe (pkgs.writeShellApplication {
name = "mxergo-battery";
bashOptions = ["nounset" "pipefail"]; # TODO: Improve script to work with ["errexit"]
runtimeInputs = [
pkgs.coreutils
pkgs.findutils
pkgs.gawk
pkgs.gnugrep
pkgs.gnused
config.services.upower.package
];
text = ''
DEVICE_PATH=$(upower -e | grep battery_hidpp_battery)
if test -z "$DEVICE_PATH"; then
echo "{}"
exit 0
fi
DEVICE_INFO=$(${upower} -i "$DEVICE_PATH")
DEVICE_INFO=$(upower -i "$DEVICE_PATH")
MODEL_NAME=$(echo "$DEVICE_INFO" | ${grep} model | ${awk} -F': ' '{print $2}' | ${xargs})
PERCENTAGE=$(echo "$DEVICE_INFO" | ${grep} percentage | ${awk} '{print $2}' | ${sed} 's/%//')
STATE=$(echo "$DEVICE_INFO" | ${grep} state | ${awk} '{print $2}')
MODEL_NAME=$(echo "$DEVICE_INFO" | grep model | awk -F': ' '{print $2}' | xargs)
PERCENTAGE=$(echo "$DEVICE_INFO" | grep percentage | awk '{print $2}' | sed 's/%//')
STATE=$(echo "$DEVICE_INFO" | grep state | awk '{print $2}')
# Determine the class based on battery percentage
if ${test} "$PERCENTAGE" -ge 80; then
if test "$PERCENTAGE" -ge 80; then
CLASS="good"
ICON=""
elif ${test} "$PERCENTAGE" -ge 40; then
elif test "$PERCENTAGE" -ge 40; then
CLASS="moderate"
ICON=""
else
Expand All @@ -77,11 +79,12 @@
fi
# Output JSON for Waybar
${printf} '{"text":"%s","tooltip":"%s","class":"%s"}' \
printf '{"text":"%s","tooltip":"%s","class":"%s"}' \
" $PERCENTAGE% $ICON" \
"Device: $MODEL_NAME \nBattery: $PERCENTAGE% $ICON\nState: $STATE" \
"$CLASS"
'';
});
};

"sway/workspaces" = {
Expand Down

0 comments on commit 90f4605

Please sign in to comment.