battery ~> juice - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
DIR commit 6923e24d2277d276f92bf8c31af5f77900028cbb
DIR parent 037074bb44ff3e1e6fa0dc3ec8b69c36cb1fe0cd
HTML Author: Willy Goiffon <dev@z3bra.org>
Date: Thu, 20 Aug 2026 21:12:31 +0000
battery ~> juice
Diffstat:
D battery | 57 -------------------------------
A juice | 32 +++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 57 deletions(-)
---
DIR diff --git a/battery b/battery
@@ -1,57 +0,0 @@
-#!/bin/sh
-#
-# z3bra - (c) wtfpl 2014
-
-usage () {
- cat <<EOF
-usage: $(basename $0) [-hlsb]
- -h : print this help
- -l : print battery percentage (default)
- -s : print battery state
- -b : beep under critical level (see BAT_BELL)
-
-environment:
- CRITICAL : the critical state level
- BAT_BELL : the command to run when run with -b flag under CRITICAL level
-EOF
-}
-
-# if battery is under a critical level, $BAT_BELL will be run
-bell () {
- # don't do anything if we're over the critical level, or the battery is
- # charging
- test ${BATC} -gt ${CRITICAL} && return 0
- test ${BATS} != "Discharging" && return 0
-
- $BAT_BELL
-}
-
-# output the current battery level
-level () {
- echo "${BATC}%"
-}
-
-# print the current battery state
-state () {
- echo "${BATS}"
-}
-
-# get battery name
-BATN=$(ls /sys/class/power_supply/ | grep BAT)
-
-# exit if no battery available
-test -z "$BATN" && exit 1
-
-# get battery level and status (charging or not)
-BATC=`cat /sys/class/power_supply/${BATN}/capacity`
-BATS=`cat /sys/class/power_supply/${BATN}/status`
-
-CRITICAL=${CRITICAL:-7}
-BAT_BELL=${BAT_BELL:-beep -f 1000 -l 200}
-
-case $1 in
- -h) usage ;;
- -s) state ;;
- -b) bell ;;
- *) level ;;
- esac
DIR diff --git a/juice b/juice
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+# for notify-send
+export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
+
+fmt='%d%%\n'
+treshold=10
+bat=/sys/class/power_supply/BAT0
+
+usage() {
+ echo "usage: $(basename $0) [-hcnst]" >&2
+}
+
+c=$(cat $bat/capacity)
+s=$(cat $bat/status)
+v=$c
+
+while getopts "hcstn" OPT; do
+ case $OPT in
+ c) fmt='%d\n'; v=$c ;; # capacity
+ s) fmt='%s\n'; v=$s ;; # charging status
+ t) test "$s" = "Charging" -o "$c" -lt "$treshold"; exit $? ;;
+ n) [ "$s" != "Charging" -a "$c" -lt "$treshold" ] \
+ || notify-send -u critical "🪫 low battery ($c%)"
+ exit ;;
+ h) usage; exit 0 ;;
+ *) usage; exit 1 ;;
+ esac
+done
+shift $((OPTIND-1))
+
+printf "$fmt" "$v"