From 3db59345749db3edc45097962a3ade98211aa937 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 18 Apr 2023 10:46:12 -0400 Subject: add "test" subcommand --- go.sh | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/go.sh b/go.sh index e23404a..676718e 100755 --- a/go.sh +++ b/go.sh @@ -1,5 +1,6 @@ #!/bin/bash -capacity_threshold=50 +default_capacity_threshold=50 +capacity_threshold_file=$HOME/.config/powerloss-handler/capacity_threshold battery_dir=/sys/class/power_supply/BAT0 status_file=$battery_dir/status @@ -8,6 +9,8 @@ capacity_file=$battery_dir/capacity UNIT=powerloss-handler CMD=$0 +FORCE_SUSPEND= + self_install() { if systemctl --user is-active "$UNIT" >/dev/null @@ -21,27 +24,51 @@ self_install() -- "$CMD" } -go() +self_test() +{ + FORCE_SUSPEND=y + go + echo +} + +do_suspend() { + systemctl suspend +} +go() +{ read capacity < "$capacity_file" || return case "$capacity" in '' | *[^0-9]*) echo "Error: unexpected value for battery capacity: $capacity" >&2 return 1 ;; esac - read status < "$status_file" || return + if [ "$FORCE_SUSPEND" ] + then + status=Discharging + capacity=0 + echo 'Warning: test: pretending battery is discharging at 0% capacity' >&2 + fi + if [ -t 2 ] then printf '\r %20s %.2d%%\r' "$status" "$capacity" >&2 fi + if [ -f "$capacity_threshold_file" ] + then + read capacity_threshold < "$capacity_threshold_file" + else + capacity_threshold=$default_capacity_threshold + fi + [ "$status" = Discharging ] || return 0 if [ "$capacity" -lt "$capacity_threshold" ] then - systemctl suspend + do_suspend fi } @@ -61,9 +88,15 @@ main() done } -if [ "$1" = install ] +if [ $# = 0 ] then - self_install -else main +else + case "$1" in + install) self_install ;; + test) self_test ;; + *) echo "Error: unknown command: $*" >&2 + exit 1 + ;; + esac fi -- cgit v1.2.3