3 # Copyright (C) 2011-2013 Simon Ruderich
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 if (scalar @ARGV != 1 and (scalar @ARGV != 2 or not $ARGV[0] =~ /-[st]/)) {
24 print STDERR "Usage: $0 [-s | -t] <path/to/device>\n";
26 print STDERR "Normally this is /sys/class/power_supply/BAT*.\n";
29 if (scalar @ARGV == 1) {
30 @ARGV = ('', $ARGV[0]);
33 my $path = "$ARGV[1]/uevent";
35 # No battery available.
37 print STDERR "'$path' not found.\n";
41 my $screen_mode = ($ARGV[0] eq '-s');
42 my $tmux_mode = ($ARGV[0] eq '-t');
46 open my $file, '<', $path or die $!;
48 /^POWER_SUPPLY_([A-Z_]+)=(.+)$/;
51 close $file or die $!;
54 if (defined $battery{ENERGY_NOW}) {
55 $battery{CHARGE_NOW} = $battery{ENERGY_NOW};
56 $battery{CHARGE_FULL} = $battery{ENERGY_FULL};
59 my $charge = int($battery{CHARGE_NOW} / $battery{CHARGE_FULL} * 100);
61 if ($battery{STATUS} eq 'Charging' or $battery{STATUS} eq 'Full') {
65 # GNU screen mode with colors: 0-20 red, 20-40 yellow, 40-100 green.
70 } elsif ($charge < 40) {
76 print "\005{+$color}$charge%\005{-}$charging\n";
79 } elsif ($tmux_mode) {
83 } elsif ($charge < 40) {
89 print "#[fg=$color,bold]$charge%#[default]$charging\n";
93 print "$charge%$charging\n";