diff --git a/nixos/modules/hyprland/battery-status.nix b/nixos/modules/hyprland/battery-status.nix new file mode 100644 index 0000000..cf4a0f4 --- /dev/null +++ b/nixos/modules/hyprland/battery-status.nix @@ -0,0 +1,83 @@ +{pkgs, hwconfig, ...}: +let + battery-path = "/sys/class/power_supply/${if hwconfig.hostname == "kylekrein-mac" then "macsmc-battery" else "BAT0"}"; + get-battery-level = "${pkgs.writeShellScriptBin "get-battery-level" '' + cat ${battery-path}/capacity 2>/dev/null || echo "N/A" + ''}/bin/get-battery-level"; + get-status = "${pkgs.writeShellScriptBin "get-status" '' + cat ${battery-path}/status 2>/dev/null || echo "Unknown" + ''}/bin/get-status"; + get-icon = "${pkgs.writeShellScriptBin "get-icon" '' +BATTERY_LEVEL=$(${get-battery-level}) +STATUS=$(${get-status}) +if [[ "$BATTERY_LEVEL" == "N/A" ]]; then + ICON="󰂑 " +elif [[ "$STATUS" == "Charging" ]]; then + if [[ $BATTERY_LEVEL -ge 90 ]]; then + ICON="󰂋 " + elif [[ $BATTERY_LEVEL -ge 80 ]]; then + ICON="󰂊 " + elif [[ $BATTERY_LEVEL -ge 70 ]]; then + ICON="󰢞 " + elif [[ $BATTERY_LEVEL -ge 60 ]]; then + ICON="󰂉 " + elif [[ $BATTERY_LEVEL -ge 50 ]]; then + ICON="󰢝 " + elif [[ $BATTERY_LEVEL -ge 40 ]]; then + ICON="󰂈 " + elif [[ $BATTERY_LEVEL -ge 30 ]]; then + ICON="󰂇 " + elif [[ $BATTERY_LEVEL -ge 20 ]]; then + ICON="󰂆 " + elif [[ $BATTERY_LEVEL -ge 10 ]]; then + ICON="󰢜 " + else + ICON="󱊦 " + fi +else + if [[ $BATTERY_LEVEL -ge 90 ]]; then + ICON="󰂂 " + elif [[ $BATTERY_LEVEL -ge 70 ]]; then + ICON="󰂀 " + elif [[ $BATTERY_LEVEL -ge 50 ]]; then + ICON="󰁾 " + elif [[ $BATTERY_LEVEL -ge 30 ]]; then + ICON="󰁼 " + elif [[ $BATTERY_LEVEL -ge 10 ]]; then + ICON="󰁺 " + else + ICON="󱊣 " + fi +fi + +echo "$ICON" + ''}/bin/get-icon"; + get-remaining-time = "${pkgs.writeShellScriptBin "get-remaining-time" '' +REMAINING_ENERGY=$(cat ${battery-path}/energy_now) +POWER_USAGE=$(cat ${battery-path}/power_now) +if [[ -n "$REMAINING_ENERGY" && -n "$POWER_USAGE" && "$POWER_USAGE" -ne 0 ]]; then + TIME_LEFT=$((0 - (REMAINING_ENERGY / POWER_USAGE))) + MINUTES_LEFT=$(((0 - ( (REMAINING_ENERGY * 60) / POWER_USAGE )) - (TIME_LEFT * 60))) + echo "$TIME_LEFT h $MINUTES_LEFT min" +else + echo "" +fi + ''}/bin/get-remaining-time"; +in +{ + available = hwconfig.isLaptop; + icon = get-icon; + status = get-status; + time = get-remaining-time; + level = get-battery-level; + labelAdaptive = "${pkgs.writeShellScriptBin "labelAdaptive" '' + if [[ "$(${get-status})" == "Charging" ]]; then + echo "$(${get-battery-level})% $(${get-icon})" + else + echo "$(${get-remaining-time}) $(${get-icon})" + fi + ''}/bin/labelAdaptive"; + labelPercent = "${pkgs.writeShellScriptBin "labelPercent" '' + echo "$(${get-battery-level})% $(${get-icon})" + ''}/bin/labelPercent"; +} diff --git a/nixos/modules/hyprland/hyprlock.nix b/nixos/modules/hyprland/hyprlock.nix index 4695e2a..d23c0c4 100644 --- a/nixos/modules/hyprland/hyprlock.nix +++ b/nixos/modules/hyprland/hyprlock.nix @@ -1,6 +1,7 @@ -{ pkgs, lib, ... }: +{ pkgs, lib, hwconfig, ... }: let profile-image = ./profile-image.png; + battery-level = (import ./battery-status.nix {inherit pkgs; inherit hwconfig;}).labelPercent; in { programs.hyprlock = { @@ -75,7 +76,15 @@ in halign = "right"; valign = "bottom"; } - ]; + ] ++ lib.optional (hwconfig.isLaptop) ( + { + text = ''cmd[update:10000] ${battery-level}''; + font_family = "JetBrains Mono"; + font_size = 28; + position = "20, 20"; + halign = "left"; + valign = "bottom"; + }); }; }; } diff --git a/nixos/modules/hyprland/waybar.nix b/nixos/modules/hyprland/waybar.nix index e76a9e0..bd2fa41 100644 --- a/nixos/modules/hyprland/waybar.nix +++ b/nixos/modules/hyprland/waybar.nix @@ -1,5 +1,7 @@ { pkgs, lib, hwconfig, ... }: - +let + battery = (import ./battery-status.nix {inherit pkgs; inherit hwconfig;}); +in { programs.waybar = { enable = true; @@ -37,7 +39,7 @@ #"memory" #"temperature" "hyprland/language" - ] ++ (if hwconfig.isLaptop then [ "battery" ] else [ ]) + ] ++ lib.optional battery.available "custom/battery" ++ [ "tray" "custom/notification" @@ -55,6 +57,14 @@ warning = 20; }; }; + "custom/battery" = { + exec ="${pkgs.writeShellScriptBin "battery-widget" '' + ${battery.labelAdaptive} + ${battery.labelPercent} + ''}/bin/battery-widget"; + interval = 20; + tooltip = true; + }; clock = { format-alt = "{:%d-%m-%Y}"; tooltip-format = "{:%d-%m-%Y | %H:%M}";