]> git.donarmstrong.com Git - bin.git/blob - pb
added pb
[bin.git] / pb
1 #! /usr/bin/perl -w
2
3 use strict;
4
5 use IO::File;
6
7 use POSIX qw(floor);
8
9 use Term::ANSIColor qw(:constants);
10
11 die "No information on battery 0\n" if not -r '/proc/pmu/battery_0';
12
13 my $bat_inf_fh = new IO::File '/proc/pmu/battery_0';
14
15 my $max_out = -1300;
16 my $max_in = 1700;
17
18 my $battery_info = {};
19
20 while (<$bat_inf_fh>) {
21      if (/(.+?)\s*:\s*(-?\d+)/) {
22           $battery_info->{$1} = int $2;
23      }
24 }
25
26 undef $bat_inf_fh;
27
28 #use Data::Dumper;
29 #print Dumper($battery_info);
30
31
32 if (exists $battery_info->{'time rem.'}) {
33      my $hours = int $battery_info->{'time rem.'}/3600;
34      my $minutes = (int $battery_info->{'time rem.'}/60)%60;
35      my $seconds = $battery_info->{'time rem.'}%60;
36      my $is_charging = hex $battery_info->{flags} & 2;
37      my $charge_state = $is_charging?'Charging':'Discharging';
38      # TODO Split charging and discharging current
39      my $current;
40      if ($battery_info->{current} > 0) {
41           $current = floor($battery_info->{current}/$max_in*1000)/10;
42      }
43      else {
44           $current = floor($battery_info->{current}/$max_out*1000)/10; 
45      }
46
47      my $percentage = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10;
48      if ($is_charging) {
49           print BOLD GREEN;
50      }
51      else {
52           print BOLD RED;
53      }
54      print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n";
55      print RESET;
56 }