aboutsummaryrefslogtreecommitdiff
path: root/.config/i3blocks/scripts/batterybar
blob: 5b5e869166d89bb67a63f36eafd7a5d4193d517e (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
#  batterybar; displays battery percentage as a bar on i3blocks
#
#  Copyright 2015 Keftaa <adnan.37h@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#
readarray -t output <<< $(acpi battery)
battery_count=${#output[@]}


for line in "${output[@]}";
do
	percentages+=($(echo "$line" | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%'))
	statuses+=($(echo "$line" | egrep -o -m1 'Discharging|Charging|AC|Full|Unknown'))
	remaining=$(echo "$line" | egrep -o -m1 '[0-9][0-9]:[0-9][0-9]')
	if [[ -n $remaining ]]; then
		remainings+=(" ($remaining)")
	else
		remainings+=("")
	fi
done

squares="■"

#There are 8 colors that reflect the current battery percentage when
#discharging
dis_colors=("#FF0027" "#FF3B05" "#FFB923" "#FFD000" "#E4FF00" "#ADFF00"
"#6DFF00" "#10BA00")
charging_color="#00FFFF"
full_color="#FFFFFF"
ac_color="#535353"


while getopts 1:2:3:4:5:6:7:8:c:f:a:h opt; do
	case "$opt" in
		1) dis_colors[0]="$OPTARG";;
		2) dis_colors[1]="$OPTARG";;
		3) dis_colors[2]="$OPTARG";;
		4) dis_colors[3]="$OPTARG";;
		5) dis_colors[4]="$OPTARG";;
		6) dis_colors[5]="$OPTARG";;
		7) dis_colors[6]="$OPTARG";;
		8) dis_colors[7]="$OPTARG";;
		c) charging_color="$OPTARG";;
		f) full_color="$OPTARG";;
		a) ac_color="$OPTARG";;
		h) printf "Usage: batterybar [OPTION] color
			When discharging, there are 8 [1-8] levels colors.
			You can specify custom colors, for example:

			batterybar -1 red -2 \"#F6F6F6\" -8 green

			You can also specify the colors for the charging, AC and
			charged states:

			batterybar -c green -f white -a \"#EEEEEE\"\n";
			exit 0;
	esac
done

end=$(($battery_count - 1))
for i in $(seq 0 $end);
do
	if (( percentages[$i] >=85 )); then
		squares=""
	elif (( percentages[$i] >=65 )); then
		squares=""
	elif (( percentages[$i] >=35 )); then
		squares=""
	elif (( percentages[$i] >=10 )); then
		squares=""
	else
		squares=""
	fi

	if [[ "${statuses[$i]}" = "Unknown" ]]; then
		#squares="<sup>?</sup>$squares"
		continue
	fi

	case "${statuses[$i]}" in
		"Charging")
			color="$charging_color"
			;;
		"Full")
			color="$full_color"
			;;
		"AC")
			color="$ac_color"
			;;
		"Discharging"|"Unknown")
			if (( percentages[$i] >= 80 )); then
				color="${dis_colors[7]}"
			elif  (( percentages[$i] >= 70)); then
				color="${dis_colors[6]}"
			elif  (( percentages[$i] >= 60)); then
				color="${dis_colors[5]}"
			elif  (( percentages[$i] >= 40)); then
				color="${dis_colors[4]}"
			elif  (( percentages[$i] >= 30)); then
				color="${dis_colors[3]}"
			elif  (( percentages[$i] >= 20)); then
				color="${dis_colors[2]}"
			elif  (( percentages[$i] >= 10)); then
				color="${dis_colors[1]}"
			else
				color="${dis_colors[0]}"
			fi
			;;
	esac

    # Print Battery number if there is more than one
    if (( $end > 1 )) ; then
	    message="$message $(($i + 1)):"
    fi

    if [[ "$BLOCK_BUTTON" -eq 1 ]]; then
	    message="$message ${statuses[$i]} <span foreground=\"$color\">${remainings[i]}</span>"
    fi
    message="$message <span foreground=\"$color\">$squares ${percentages[$i]}%</span>"
done

echo $message