From d1ca9c43aa55859d08ff3b57e2ee676975c30648 Mon Sep 17 00:00:00 2001 From: Justine Smithies Date: Wed, 7 Feb 2024 18:58:43 +0000 Subject: Removed python battery script from eww and added bash script --- .config/eww/eww.yuck | 6 +-- .config/eww/scripts/battery.py | 89 ------------------------------------------ .config/eww/scripts/battery.sh | 54 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 93 deletions(-) delete mode 100755 .config/eww/scripts/battery.py create mode 100755 .config/eww/scripts/battery.sh diff --git a/.config/eww/eww.yuck b/.config/eww/eww.yuck index d0e832a..6867096 100644 --- a/.config/eww/eww.yuck +++ b/.config/eww/eww.yuck @@ -146,14 +146,12 @@ :spacing 10 (button :class "" - :onclick "scripts/battery.py --c left-click" - :onmiddleclick "scripts/battery.py --c middle-click" - :onrightclick "scripts/battery.py --c right-click" + :onclick "scripts/battery.sh left-click" battery) )) (defpoll battery :interval "10s" - "scripts/battery.py --c status") + "scripts/battery.sh") (defpoll scratchpad :interval "1s" "scripts/scratchpad-indicator.sh Update || echo null") diff --git a/.config/eww/scripts/battery.py b/.config/eww/scripts/battery.py deleted file mode 100755 index 79d3471..0000000 --- a/.config/eww/scripts/battery.py +++ /dev/null @@ -1,89 +0,0 @@ -#!/usr/bin/env python3 - -import psutil -import argparse -import subprocess - - -def secs2hours(secs): - mm, ss = divmod(secs, 60) - hh, mm = divmod(mm, 60) - return "%d:%02d:%02d" % (hh, mm, ss) - - -parser = argparse.ArgumentParser() -parser.add_argument('--c', - choices=('status', 'left-click', 'middle-click', 'right-click'), - dest='command', - default='status', - help='Allowed values are status, left-click, middle-click and right-click' - ) -args = parser.parse_args() - -battery = psutil.sensors_battery() -icon = "" -percent = int(battery.percent) -time_left = battery.secsleft -isPlugged = battery.power_plugged -remaining = secs2hours(time_left) - -if args.command == "status": - if isPlugged: - if percent == 100: - icon = "󰂅" - elif percent > 89 and percent < 100: - icon = "󰂋" - elif percent > 79 and percent < 90: - icon = "󰂊" - elif percent > 69 and percent < 80: - icon = "󰢞" - elif percent > 59 and percent < 70: - icon = "󰂉" - elif percent > 49 and percent < 60: - icon = "󰢝" - elif percent > 39 and percent < 50: - icon = "󰂈" - elif percent > 29 and percent < 40: - icon = "󰂇" - elif percent > 19 and percent < 30: - icon = "󰂆" - elif percent > 9 and percent < 20: - icon = "󰢜" - elif percent > 0 and percent < 10: - icon = "󰢟" - message = str(percent) + "%" - print(icon, message, end="") - else: - if percent == 100: - icon = "" - elif percent > 89 and percent < 100: - icon = "󰂂" - elif percent > 79 and percent < 90: - icon = "󰂁" - elif percent > 69 and percent < 80: - icon = "󰂀" - elif percent > 59 and percent < 70: - icon = "󰁿" - elif percent > 49 and percent < 60: - icon = "󰁾" - elif percent > 39 and percent < 50: - icon = "󰁽" - elif percent > 29 and percent < 40: - icon = "󰁼" - elif percent > 19 and percent < 30: - icon = "󰁻" - elif percent > 9 and percent < 20: - icon = "󰁺" - elif percent > 0 and percent < 10: - icon = "󱃍" - message = str(percent) + "%" - print(icon, message, end="") -if args.command == "left-click": - if not isPlugged: - subprocess.call(["notify-send", "-r", "55555", "-u", "normal", "Est remaining time left: " + remaining]) - else: - subprocess.call(["notify-send", "-r", "55555", "-u", "normal", str(percent) + "% Charged"]) -if args.command == "middle-click": - print("Middle click") -if args.command == "right-click": - print("Right click") diff --git a/.config/eww/scripts/battery.sh b/.config/eww/scripts/battery.sh new file mode 100755 index 0000000..ca11210 --- /dev/null +++ b/.config/eww/scripts/battery.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +# Send a notification if battery is low, change status in info file accordingly +notify_low () { + + # Battery isn't much but is charging (don't notify) + [ "$status" = "Charging" ] && return + + # Battery critically low + if [ "$capacity" -le 10 ] && ! grep -q critically-low "$lowinfo"; then + notify-send -i "$HOME/.config/icons/critical-battery.png" -u critical "Battery critically low!" + echo critically-low >"$lowinfo" + + # Battery is low + elif [ "$capacity" -le 20 ] && ! grep -q low "$lowinfo"; then + notify-send -i "$HOME/.config/icons/low-battery.png" -t 5500 "Battery low!" + echo low >"$lowinfo" + fi +} + +battery="/sys/class/power_supply/BAT0" +status="$(cat "$battery/status")" +capacity="$(cat "$battery/capacity")" + +# Get the low battery status from file ~/.cache/battery-low-status +lowinfo="$HOME/.cache/battery-low-status" +[ ! -e "$lowinfo" ] && touch "$lowinfo" + + +case $1 in + left-click) + ;; + *) + # Set charging icon and capacity icon + [ "$status" = "Charging" ] && charging_icon=" " || charging_icon="" + [ "$capacity" -gt 100 ] && capacity=100 + if [ "$capacity" -lt 15 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 40 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 60 ]; then capacity_icon=' ' + elif [ "$capacity" -lt 90 ]; then capacity_icon=' ' + else capacity_icon=' ' + fi + + # Report low battery + [ "$capacity" -le 20 ] && notify_low + + # Reset low battery information in these cases + { [ "$capacity" -gt 20 ] || [ "$status" = "Charging" ]; } && [ -n "$(cat "$lowinfo")" ] && echo "" >"$lowinfo" + + printf "%s%s%d%%\n" "$charging_icon" "$capacity_icon" "$capacity" + ;; +esac + + -- cgit v1.2.3