]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - bin/battery.pl
screenrc: Display current battery charge.
[config/dotfiles.git] / bin / battery.pl
1 #!/usr/bin/perl
2
3
4 use strict;
5 use warnings;
6
7
8 my $path = '/sys/class/power_supply/BAT0/uevent';
9
10 # No battery available.
11 if (! -e $path) {
12     exit;
13 }
14
15 my $screen_mode = (defined $ARGV[0] and $ARGV[0] eq '-s');
16
17 my %battery;
18
19 open my $file, '<', $path;
20 while (<$file>) {
21     /^POWER_SUPPLY_([A-Z_]+)=(.+)$/;
22     $battery{$1} = $2;
23 }
24 close $file;
25
26 my $charge = int($battery{CHARGE_NOW} / $battery{CHARGE_FULL} * 100);
27
28 # GNU screen mode with colors: 0-20 red, 20-40 yellow, 40-100 green.
29 if ($screen_mode) {
30     my $color;
31     if ($charge < 20) {
32         $color = 'b r';
33     } elsif ($charge < 40) {
34         $color = 'b y';
35     } else {
36         $color = 'b g';
37     }
38
39     print "\005{+$color}$charge%\005{-}\n";
40
41 # Just text output.
42 } else {
43     print "$charge%\n";
44 }