blob: 0308abab6c3011183da27e402c0b59dffd6f1b78 [file] [log] [blame]
Copybara botca5ce642024-11-08 17:38:08 +01001#!/usr/bin/env bash
2# batterybar; displays battery percentage as a bar on i3blocks
3#
4# Copyright 2015 Keftaa <adnan.37h@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19# MA 02110-1301, USA.
20#
21#
22readarray -t output <<< $(acpi battery)
23battery_count=${#output[@]}
24
25for line in "${output[@]}";
26do
27 percentages+=($(echo "$line" | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%'))
28 statuses+=($(echo "$line" | egrep -o -m1 'Discharging|Charging|AC|Full|Unknown'))
29 remaining=$(echo "$line" | egrep -o -m1 '[0-9][0-9]:[0-9][0-9]')
30 if [[ -n $remaining ]]; then
31 remainings+=(" ($remaining)")
32 else
33 remainings+=("")
34 fi
35done
36
37squares="■"
38
39#There are 8 colors that reflect the current battery percentage when
40#discharging
41dis_colors=("${C1:-#FF0027}" "${C2:-#FF3B05}" "${C3:-#FFB923}"
42 "${C4:-#FFD000}" "${C5:-#E4FF00}" "${C6:-#ADFF00}"
43 "${C7:-#6DFF00}" "${C8:-#10BA00}")
44charging_color="${CHARGING_COLOR:-#00AFE3}"
45full_color="${FULL_COLOR:-#FFFFFF}"
46ac_color="${AC_COLOR:-#535353}"
47
48
49while getopts 1:2:3:4:5:6:7:8:c:f:a:h opt; do
50 case "$opt" in
51 1) dis_colors[0]="$OPTARG";;
52 2) dis_colors[1]="$OPTARG";;
53 3) dis_colors[2]="$OPTARG";;
54 4) dis_colors[3]="$OPTARG";;
55 5) dis_colors[4]="$OPTARG";;
56 6) dis_colors[5]="$OPTARG";;
57 7) dis_colors[6]="$OPTARG";;
58 8) dis_colors[7]="$OPTARG";;
59 c) charging_color="$OPTARG";;
60 f) full_color="$OPTARG";;
61 a) ac_color="$OPTARG";;
62 h) printf "Usage: batterybar [OPTION] color
63 When discharging, there are 8 [1-8] levels colors.
64 You can specify custom colors, for example:
65
66 batterybar -1 red -2 \"#F6F6F6\" -8 green
67
68 You can also specify the colors for the charging, AC and
69 charged states:
70
71 batterybar -c green -f white -a \"#EEEEEE\"\n";
72 exit 0;
73 esac
74done
75
76end=$(($battery_count - 1))
77for i in $(seq 0 $end);
78do
79 if (( percentages[$i] > 0 && percentages[$i] < 20 )); then
80 squares="■"
81 elif (( percentages[$i] >= 20 && percentages[$i] < 40 )); then
82 squares="■■"
83 elif (( percentages[$i] >= 40 && percentages[$i] < 60 )); then
84 squares="■■■"
85 elif (( percentages[$i] >= 60 && percentages[$i] < 80 )); then
86 squares="■■■■"
87 elif (( percentages[$i] >=80 )); then
88 squares="■■■■■"
89 fi
90
91 if [[ "${statuses[$i]}" = "Unknown" ]]; then
92 squares="<sup>?</sup>$squares"
93 fi
94
95 case "${statuses[$i]}" in
96 "Charging")
97 color="$charging_color"
98 ;;
99 "Full")
100 color="$full_color"
101 ;;
102 "AC")
103 color="$ac_color"
104 ;;
105 "Discharging"|"Unknown")
106 if (( percentages[$i] >= 0 && percentages[$i] < 10 )); then
107 color="${dis_colors[0]}"
108 elif (( percentages[$i] >= 10 && percentages[$i] < 20 )); then
109 color="${dis_colors[1]}"
110 elif (( percentages[$i] >= 20 && percentages[$i] < 30 )); then
111 color="${dis_colors[2]}"
112 elif (( percentages[$i] >= 30 && percentages[$i] < 40 )); then
113 color="${dis_colors[3]}"
114 elif (( percentages[$i] >= 40 && percentages[$i] < 60 )); then
115 color="${dis_colors[4]}"
116 elif (( percentages[$i] >= 60 && percentages[$i] < 70 )); then
117 color="${dis_colors[5]}"
118 elif (( percentages[$i] >= 70 && percentages[$i] < 80 )); then
119 color="${dis_colors[6]}"
120 elif (( percentages[$i] >= 80 )); then
121 color="${dis_colors[7]}"
122 fi
123 ;;
124 esac
125
126 # Print Battery number if there is more than one
127 if (( $end > 0 )) ; then
128 message="$message $(($i + 1)):"
129 fi
130
131 if [[ "$BLOCK_BUTTON" -eq 1 ]]; then
132 message="$message ${statuses[$i]} <span foreground=\"$color\">${percentages[$i]}%${remainings[i]}</span>"
133 fi
134 message="$message <span foreground=\"$color\">$squares</span>"
135done
136
137echo $message