#!/usr/bin/perl # battstat[.pl] # ACPI Battery Monitor # # version 1.0 use strict; use POSIX qw/floor/; #use warnings; my $path="/proc/acpi/battery/BAT1"; die "Cannot find battery information. Perhaps ACPI is not enabled?\n" if !-e $path; # Gather information about battery my %info; for(qw(info state)) { open FILE,"<$path/$_"; while (my $line=) { my($key,$value)=$line=~/^(.+?): *(.+?)$/i; $value=(split / /,$value)[0]; next if !$key; $info{$key}=$value; } close FILE; } # Calculations my $rc=$info{"remaining capacity"}; my $lfc=$info{"last full capacity"}; my $pr=$info{"present rate"}; my $cs=$info{"charging state"}; my($remaining,$t); if ($cs eq "charging") { $remaining=floor(100-($lfc-$rc)/$lfc*100); $t=floor(($lfc-$rc)/$pr*3600) if $pr; } else { $remaining=floor($rc/$lfc*100); $t=floor($rc/$pr*3600) if $pr; } $t=0 if !$pr; # Format time my $h=floor($t/3600); $t-=$h*3600; my $m=floor($t/60); $t-=$m*60; my $s=$t; my $time_format=$h."h".$m."m".$s."s"; my $str=($cs eq "discharging") ? "Battery status: $remaining% remaining ($time_format)" : "Battery charging: $remaining% ($time_format)"; print "$str\n";