scripts/thinkpad/pow.pl

170 lines
5.1 KiB
Perl
Raw Normal View History

#!/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' ) {
2023-01-06 19:41:31 +00:00
print'
pow.pl - Power Status Script
Usage: pow.pl
Prints information about all power devices as JSON.
2023-01-06 19:41:31 +00:00
-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') {
2023-01-06 19:41:31 +00:00
$bar = 1;
} elsif ($ARGV[0] eq '-p' || $ARGV[0] eq '--pretty') {
2023-01-06 19:41:31 +00:00
$pretty = 1;
}
my %battery_total = (
2023-01-06 19:41:31 +00:00
'current' => 0,
'max' => 0,
'percentage' => 0
);
my @devices = <"/sys/class/power_supply/*">;
2023-01-06 19:41:31 +00:00
unless (scalar(@devices)) {
print('{"AC":{"Type":"AC","Status":"Plugged-In"}}');
exit();
}
my $nobat = 1;
$output .= "{";
foreach (@devices) {
2023-01-06 19:41:31 +00:00
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 {
2023-01-06 19:41:31 +00:00
$status = "Unplugged";
}
2023-01-06 19:41:31 +00:00
$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",
2023-01-06 19:41:31 +00:00
$battery_total{'current'} / $battery_total{'max'} * 100
);
$output .= '"Total":{"Current":"'
2023-01-06 19:41:31 +00:00
. $battery_total{'current'}
. '","Max":"'
. $battery_total{'max'}
. '","Percentage":"'
. $battery_total{'percentage'}
. '"}}';
if ($bar) {
2023-01-06 19:41:31 +00:00
use JSON::XS;
my $json = JSON::XS->new();
my $powref = $json->decode($output);
my $class = 'discharging';
$output = '';
if ($powref->{AC}->{Status} eq "Plugged-In") {
2023-01-07 13:16:47 +00:00
$class = 'charging';
$output .= " ";
2023-01-07 13:16:47 +00:00
} elsif ($powref->{Total}->{Percentage} <= 10) {
$class = 'critical';
$output .= " ";
2023-01-07 13:16:47 +00:00
} elsif ($powref->{Total}->{Percentage} <= 35) {
$class = 'low';
$output .= " ";
2023-01-06 19:41:31 +00:00
} elsif ($powref->{Total}->{Percentage} <= 60) {
$output .= " ";
2023-01-06 19:41:31 +00:00
} elsif ($powref->{Total}->{Percentage} <= 85) {
$output .= " ";
2023-01-06 19:41:31 +00:00
} else {
$output .= " ";
2023-01-06 19:41:31 +00:00
}
$output = '{'
. '"text":"' . $output . $powref->{Total}->{Percentage} . '%",'
2023-01-06 19:41:31 +00:00
. '"icon":"' . $output . '",'
. '"percentage":"' . $powref->{Total}->{Percentage} . '",'
. '"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}) {
2023-01-07 13:16:47 +00:00
my $status;
if ($powref->{$device}->{Status} eq "Plugged-In") {
$status = "";
} elsif (!defined($powref->{$device}->{Percentage}) || $powref->{$device}->{Percentage} == 0) {
$status = " ";
2023-01-06 19:41:31 +00:00
} 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'} . "%";
2023-01-07 13:16:47 +00:00
} 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);
}
2023-01-06 19:41:31 +00:00
}
$output .= $total;
}
print $output . "\n";
exit();