#! /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; }