From: Don Armstrong Date: Sun, 2 Oct 2005 22:26:27 +0000 (+0000) Subject: added pb X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;ds=sidebyside;h=6f3acdb4511e0010babb4d4ecd4a2deab9921445;p=bin.git added pb --- diff --git a/pb b/pb new file mode 100755 index 0000000..4d57fd1 --- /dev/null +++ b/pb @@ -0,0 +1,56 @@ +#! /usr/bin/perl -w + +use strict; + +use IO::File; + +use POSIX qw(floor); + +use Term::ANSIColor qw(:constants); + +die "No information on battery 0\n" if not -r '/proc/pmu/battery_0'; + +my $bat_inf_fh = new IO::File '/proc/pmu/battery_0'; + +my $max_out = -1300; +my $max_in = 1700; + +my $battery_info = {}; + +while (<$bat_inf_fh>) { + if (/(.+?)\s*:\s*(-?\d+)/) { + $battery_info->{$1} = int $2; + } +} + +undef $bat_inf_fh; + +#use Data::Dumper; +#print Dumper($battery_info); + + +if (exists $battery_info->{'time rem.'}) { + my $hours = int $battery_info->{'time rem.'}/3600; + my $minutes = (int $battery_info->{'time rem.'}/60)%60; + my $seconds = $battery_info->{'time rem.'}%60; + my $is_charging = hex $battery_info->{flags} & 2; + my $charge_state = $is_charging?'Charging':'Discharging'; + # TODO Split charging and discharging current + my $current; + if ($battery_info->{current} > 0) { + $current = floor($battery_info->{current}/$max_in*1000)/10; + } + else { + $current = floor($battery_info->{current}/$max_out*1000)/10; + } + + my $percentage = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10; + if ($is_charging) { + print BOLD GREEN; + } + else { + print BOLD RED; + } + print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n"; + print RESET; +}