This commit is contained in:
ooks-io 2023-09-04 14:54:33 +12:00
parent 659b7407d5
commit ccffa13eb2
3 changed files with 57 additions and 35 deletions

View file

@ -3,6 +3,7 @@
../standard
../standard/wayland
../music
../standard/wayland/eww
./tty-init.nix
./binds.nix

View file

@ -0,0 +1,12 @@
{ pkgs, config, ... }:
{
programs.eww = {
enable = true;
package = pkgs.eww-wayland;
configDir = ./config;
};
}

View file

@ -1,51 +1,60 @@
{ config, lib, pkgs, ... }:
{ config, pkgs, lib, ... }:
# Regularly check the battery status and send a notification when it discharges
# below certain thresholds.
# Implemented by calling the `acpi` program regularly. This is the simpler and
# safer approach because the battery might not send discharging events.
# author: github:Julow
let conf = config.modules.battery-notify;
let
conf = config.modules.battery-notify;
in {
options.modules.battery-notify = with lib; {
enable = mkEnableOption "battery-notify";
};
config = lib.mkIf conf.enable {
# Regularly check battery status
systemd.user.services.battery-notify = {
wantedBy = [ "graphical-session.target" ];
script = ''
batval=$(cat /sys/class/power_supply/BAT0/capacity)
tempbatval="/tmp/bat_val_$batval"
if [ ! -f "$tempbatval" ]; then
case "$batval" in
75|50|25|10|5)
${pkgs.libnotify}/bin/notify-send -a bat-notify "battery:" "$batval"
touch "tempbatval"
;;
esac
fi
for level in 75 50 25 10 5; do
if [ "$batval" -gt "$level" ]; then
rm -f "/tmp/bat_val_$level"
prev_val=100
check () { [[ $1 -ge $val ]] && [[ $1 -lt $prev_val ]]; }
get_battery_glyph() {
local val="$1"
if [[ "$val" -le 10 ]]; then
echo "" # or echo -e "\uf244" if using the Unicode code
elif [[ "$val" -le 25 ]]; then
echo ""
elif [[ "$val" -le 50 ]]; then
echo ""
elif [[ "$val" -le 75 ]]; then
echo ""
else
echo ""
fi
}
notify () {
glyph=$(get_battery_glyph $val)
${pkgs.libnotify}/bin/notify-send -a bat-notify "$glyph $val%"
}
while true; do
IFS=: read _ bat0 < <(${pkgs.acpi}/bin/acpi -b)
IFS=\ , read status val remaining <<<"$bat0"
val=''${val%\%}
if [[ $status = Discharging ]]; then
echo "$val%, $remaining"
if check 75 || check 50 ||check 30 || check 25 || check 20; then notify
elif check 15 || [[ $val -le 10 ]]; then notify -u critical
fi
fi
prev_val=$val
# Sleep longer when battery is high to save CPU
if [[ $val -gt 30 ]]; then sleep 10m; elif [[ $val -ge 20 ]]; then sleep 5m; else sleep 1m; fi
done
'';
};
systemd.user.timers.battery-notify = {
wantedBy = [ "timers.target" ];
after = [ "basic.target" ];
requires = [ "battery-notify.service" ];
partOf = [ "battery-notify.service" ];
timerConfig = {
OnBootSec = "1m";
OnUnitActiveSec = "1m";
};
};
};
}
}