9 use Term::ANSIColor qw(:constants);
12 my $temp = IO::File->new($_[0],'r') or
13 die "Unable to open $_[0] for reading: $!";
21 if (-r '/proc/pmu/battery_0') {
22 my $bat_inf_fh = new IO::File '/proc/pmu/battery_0';
26 my $battery_info = {};
28 while (<$bat_inf_fh>) {
29 if (/(.+?)\s*:\s*(-?\d+)/) {
30 $battery_info->{$1} = int $2;
34 $bat_inf{charging} = hex $battery_info->{flags} & 2;
35 $bat_inf{current} = 0;
36 if ($battery_info->{current} > 0) {
37 $bat_inf{current} = floor($battery_info->{current}/$max_in*1000)/10;
40 $bat_inf{current} = floor($battery_info->{current}/$max_out*1000)/10;
42 $bat_inf{percent} = floor($battery_info->{'charge'}/$battery_info->{'max_charge'}*1000)/10;
43 $bat_inf{time_rem} = $battery_info->{'time rem.'};
45 elsif (-r '/sys/devices/platform/smapi/BAT0/remaining_capacity') {
46 $bat_inf{percent} = catfile('/sys/devices/platform/smapi/BAT0/remaining_percent');
47 $bat_inf{charging} = catfile('/sys/devices/platform/smapi/BAT0/state') ne 'discharging';
48 $bat_inf{time_rem} = $bat_inf{charging} ? catfile('/sys/devices/platform/smapi/BAT0/remaining_charging_time') :
49 catfile('/sys/devices/platform/smapi/BAT0/remaining_running_time');
50 $bat_inf{time_rem} !~ /\d/ and $bat_inf{time_rem} = 0;
51 $bat_inf{time_rem} *= 60;
52 $bat_inf{current} = -catfile('/sys/devices/platform/smapi/BAT0/current_now') / 10;}
54 print STDERR "No battery information\n";
60 #print Dumper($battery_info);
63 my $hours = int $bat_inf{time_rem}/3600;
64 my $minutes = (int $bat_inf{time_rem}/60)%60;
65 my $seconds = $bat_inf{time_rem}%60;
66 my $current = $bat_inf{current};
67 my $charge_state = $bat_inf{charging}?'Charging':'Discharging';
68 my $percentage = $bat_inf{percent};
69 if ($bat_inf{charging}) {
75 print "$charge_state at $current\%: $hours hours, $minutes minutes, and $seconds seconds remaining. [$percentage\%]\n";