#!/usr/bin/env sh ###################################################################### # @author : swytch (adapted from Luke Smith - lukesmith.xyz) # @file : sb-battery # @license : GPLv3 # @created : Saturday Feb 13, 2021 17:19:04 CET # # @description : battery block dwmblocks ###################################################################### # Will show all batteries with approximate icon for remaining power. # Or show that the computer is plugged to a power source # In any case, show the remaining battery percentage # Also sends a notification if battery running low time="$XDG_STATE_HOME/batteryupdate" notify() { \ now=$(date +%s) if [ -e $time ]; then old=$(cat $time) delta=$(expr $now - $old) else delta=$now fi [ 300 -gt $delta ] && return; echo $now > $time case "$capacity" in 2[0-9]) notify-send -u "normal" "Battery is running low ($capacity%)" "Please plug your computer to a power source" ;; *) notify-send -u "critical" "Battery is dangerously low ($capacity%)" "Please plug your computer to a power source - NOW!" ;; esac } total=0 for bat in /sys/class/power_supply/BAT?/ do status="$(cat "$bat/status")" capacity="$(cat "$bat/capacity")" total="$(expr $total + $capacity)" slot="$(basename $bat)" if [ "Full" = "$status" ]; then status=" " && capacity="FULL" printf " %s:%s(%s) " "$slot" "$status" "$capacity" else if [ "$status" = "Charging" ]; then status="" else case "$capacity" in 100|[8-9][0-9]) status="" ;; [6-7][0-9]) status="" ;; [4-5][0-9]) status="" ;; [2-3][0-9]) status="" ;; *) status="" ;; esac [ "BAT0" = $slot ] && [ 30 -gt $capacity ] && notify; fi printf " %s:%s (%0.2d%%) " "$slot" "$status" "$capacity"; fi done