summaryrefslogtreecommitdiff
path: root/.local/bin/dwmbar/dwmb-battery
blob: c888fd85825dd17af364bddac5ab74d4062780ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh

# Usage:
# This script prints a battery icon and percentage charge.
# If charging, it will show a charging icon instead of a battery
#
# By default, it will show the first battery found.
# If you have multiple batteries, you can specify the battery as an argument to this script.
#
# Example:
# $ dwmb-battery BAT1
#  󱐋 86%

#  00-19%
#  20-39%
#  40-59%
#  60-79%
#  80-100%
# 󱐋 or  charging

[ -z "$1" ] && bat=$(echo /sys/class/power_supply/BAT* | head -n 1) ||
	bat=/sys/class/power_supply/"$1"

[ ! -d "$bat" ] && echo "No battery found." && exit 1

status=$(cat "$bat"/status)
capacity=$(cat "$bat"/capacity)

# for debugging, $2 is the percentage to print
[ -n "$2" ] && capacity=$2

case "$status" in
Charging) icon="󱐋" ;;
*) icon="" ;;
esac

# If the battery is not charging, evaluate its current charge and print an icon accordingly
[ -z "$icon" ] &&
	case "$capacity" in
	[0-9] | [0-1][0-9]) icon=" " ;;
	[2-3][0-9]) icon=" " ;;
	[4-5][0-9]) icon=" " ;;
	[6-7][0-9]) icon=" " ;;
	[8-9][0-9] | 100) icon=" " ;;
	esac

printf '{ "text": "%s%s%%", "class":"block"}\n' "$icon" "$capacity"