Fetcher for Battery and Power status

Features outputs as JSON, simple output for status bar and notification-ready pretty formatting.
This commit is contained in:
John Mertz 2021-07-27 01:11:42 -04:00 committed by John Mertz
parent 4d2fbf6254
commit 855d9ca492
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 159 additions and 0 deletions

159
thinkpad/pow.pl Executable file
View File

@ -0,0 +1,159 @@
#!/usr/bin/perl
my $output;
my ($bar, $pretty) = (0, 0);
if ( defined($ARGV[0]) && $ARGV[0] ne '-b' && $ARGV[0] ne '--bar' && $ARGV[0] ne '-p' && $ARGV[0] ne '--pretty' ) {
print'
pow.pl - Power Status Script
Usage: pow.pl
Prints information about all power devices as JSON.
-p --pretty Display as human-readable
-b --bar Waybar simplified output
-h --help Display help. This is the only option.
';
exit();
} elsif ($ARGV[0] eq '-b' || $ARGV[0] eq '--bar') {
$bar = 1;
} elsif ($ARGV[0] eq '-p' || $ARGV[0] eq '--pretty') {
$pretty = 1;
}
my %battery_total = (
'current' => 0,
'max' => 0,
'percentage' => 0
);
my @devices = <"/sys/class/power_supply/*">;
$output .= "{";
foreach (@devices) {
my $path = $_;
my $name = $_;
$name =~ s/.*\///;
$output .= '"' . $name . '":{';
open(my $t,'<',"$_/type");
my $type = <$t>;
chomp $type;
close $t;
$output .= '"Type":"' . $type . '"';
if ($name =~ /BAT[0-9]+/) {
open(my $s,'<',"$_/status");
my $status = <$s>;
chomp $status;
$output .= ',"Status":"' . $status . '"';
} else {
open(my $s,'<',"$_/online");
my $status = <$s>;
chomp $status;
if ($status) {
$status = "Plugged-In";
} else {
$status = "Unplugged";
}
$output .= ',"Status":"' . $status . '"';
}
close $s;
if ($name =~ /BAT[0-9]+/) {
open(my $m,'<',"$_/energy_full");
my $max = <$m>;
close $m;
chomp $max;
$battery_total{'max'} += $max;
open(my $c,'<',"$_/energy_now");
my $current = <$c>;
chomp $current;
#$current =~ s/\n//;
$battery_total{'current'} += $current;
close $c;
$output .= ',"Current":"'
. $current
. '","Max":"'
. $max
. '","Percentage":"'
. int($current/$max*100)
. '"';
}
$output .= "},";
}
$battery_total{'percentage'} = sprintf("%0d",
$battery_total{'current'} / $battery_total{'max'} * 100
);
$output .= '"Total":{"Current":"'
. $battery_total{'current'}
. '","Max":"'
. $battery_total{'max'}
. '","Percentage":"'
. $battery_total{'percentage'}
. '"}}';
if ($bar) {
use JSON::XS;
my $json = JSON::XS->new();
my $powref = $json->decode($output);
my $icon;
my $class = 'discharging';
$output = '{"text": "' . $powref->{Total}->{Percentage} . '% ';
if ($powref->{AC}->{Status} eq "Plugged-In") {
$class = 'charging';
$output .= "";
} elsif ($powref->{Total}->{Percentage} le 10) {
$class = 'critical';
$output .= "";
} elsif ($powref->{Total}->{Percentage} le 35) {
$class = 'low';
$output .= "";
} elsif ($powref->{Total}->{Percentage} le 60) {
$output .= "";
} elsif ($powref->{Total}->{Percentage} le 85) {
$output .= "";
} else {
$output .= "";
}
$output .= '", "tooltip": "' . $class . '", "class": "' . $class . '"}';
} elsif ($pretty) {
use JSON::XS;
my $json = JSON::XS->new();
my $powref = $json->decode($output);
my $total = '';
$output = "Device Status Percentage\n";
foreach my $device (keys %{$powref}) {
my $status;
if ($powref->{$device}->{Status} eq "Plugged-In") {
$status = "";
} elsif (!defined($powref->{$device}->{Percentage}) || $powref->{$device}->{Percentage} == 0) {
$status = " ";
} elsif ($powref->{$device}->{Percentage} <= 10) {
$status = " " . $powref->{$device}->{'Percentage'} . "%";
} elsif ($powref->{$device}->{Percentage} <= 35) {
$status = " " . $powref->{$device}->{'Percentage'} . "%";
} elsif ($powref->{$device}->{Percentage} <= 60) {
$status = " " . $powref->{$device}->{'Percentage'} . "%";
} elsif ($powref->{$device}->{Percentage} <= 85) {
$status = " " . $powref->{$device}->{'Percentage'} . "%";
} else {
$status = " " . $powref->{$device}->{'Percentage'} . "%";
}
if ($device eq 'Total') {
$total .= sprintf("%-24s %-12s", $device, $status);
} else {
#$output .= sprintf("%".length($powref->{$device->{'Type'}})."s (%".length($powref->{$device->{'Type'}})."s): %3d\%\n", $device, $device->{'Type'}, $device->{'Percentage'});
my $name = ' ('.$powref->{$device}->{'Type'}.')';
if ((length($device)+length($name)) >= 24) {
$name = substr($device, 0, (21-length($name))) . '...' . $name;
} else {
$name = substr(($device.$name), 0, 24);
}
$output .= sprintf("%-24s %-12s %-12s\n", $name, $powref->{$device}->{Status}, $status);
}
}
$output .= $total;
}
print $output . "\n";
exit();