scripts/thinkpad/pow.pl

159 lines
5.2 KiB
Perl
Executable File

#!/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 $class = 'discharging';
$output = '{"text": "';
if ($powref->{AC}->{Status} eq "Plugged-In") {
$class = 'charging';
$output .= "";
} elsif ($powref->{Total}->{Percentage} <= 10) {
$class = 'critical';
$output .= "";
} elsif ($powref->{Total}->{Percentage} <= 35) {
$class = 'low';
$output .= "";
} elsif ($powref->{Total}->{Percentage} <= 60) {
$output .= "";
} elsif ($powref->{Total}->{Percentage} <= 85) {
$output .= "";
} else {
$output .= "";
}
$output .= $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}) {
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();