-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.pl
executable file
·69 lines (60 loc) · 1.53 KB
/
battery.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
# Reads (Linux) battery statistics, and returns various monitors
# Arguments are iterated over, and monitors are returned in the order specified
# Multiple monitors may be returned
#
# Sampe usable 'battery.pl -m -p -t'
open(my $sys_charging, "<", "/sys/class/power_supply/BAT0/status");
open(my $sys_full, "<", "/sys/class/power_supply/BAT0/energy_full");
open(my $sys_now, "<", "/sys/class/power_supply/BAT0/energy_now");
open(my $sys_rate, "<", "/sys/class/power_supply/BAT0/power_now");
chomp(my $charging = <$sys_charging>);
my $capacity = <$sys_full>;
my $current = <$sys_now>;
my $rate = <$sys_rate>;
sub battery_time {
my $status = "";
my $time = "";
if ($charging eq "Discharging") {
$status = "-";
$time = $current/$rate;
} else {
$status = "+";
$time = $rate/$capacity;
}
return sprintf("%s%.1f hours", $status, $time);
}
sub battery_percent {
return sprintf("%d%%", 100 * $current/$capacity);
}
sub battery_meter {
if ($charging eq "Discharging") {
my $meter = "[";
for (my $i=0; $i<4; $i++) {
if ($current/$capacity > $i*0.26) {
if ($current/$capacity > $i*0.25+0.13) {
$meter .= ":";
} else {
$meter .= ".";
}
} else {
$meter .= " ";
}
}
$meter .= "]";
return $meter;
} else {
return "[ -E ]";
}
}
my %args = (
"-t" => \&battery_time,
"-p" => \&battery_percent,
"-m" => \&battery_meter,
);
my $battery_string = "";
foreach my $arg (@ARGV) {
if ($args{$arg}) { $battery_string .= $args{$arg}->() . " "; }
}
$battery_string =~ s/\s+$//;
print $battery_string;