Created
April 10, 2020 11:38
-
-
Save JohnMertz/c0c7ac7b4588b2430126c9e9ad734a5d to your computer and use it in GitHub Desktop.
Battery status script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Not proud of this one. It had multi-battery supported shunted on when I got a new laptop and Waybay output taped on too. Could use to be cleanud up a lot, but it does the job. | |
my $output; | |
my $bar = 0; | |
if ($ARGV[0] eq '-h' || $ARGV[0] eq '--help') { | |
print' | |
pow.pl - Power Status Script | |
Usage: pow.pl | |
Prints information about all power devices as JSON by default. | |
-b --bar Simple colour and icon output <span> formatted for Waybar | |
-h --help Display help. | |
'; | |
exit(); | |
} elsif ($ARGV[0] eq '-b' || $ARGV[0] eq '--bar') { | |
$bar = 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; | |
# Set colour | |
$output = '<span background="'; | |
if ($powref->{AC}->{Status} eq "Plugged-In") { | |
$output .= 'green'; | |
$icon = ""; | |
} elsif ($powref->{Total}->{Percentage} le 10) { | |
$output .= 'red'; | |
$icon = ""; | |
} elsif ($powref->{Total}->{Percentage} le 35) { | |
$output .= 'orange'; | |
$icon = ""; | |
} elsif ($powref->{Total}->{Percentage} le 60) { | |
$output .= 'yellow'; | |
$icon = ""; | |
} elsif ($powref->{Total}->{Percentage} le 85) { | |
$output .= 'yellow'; | |
$icon = ""; | |
} else { | |
$output .= 'green'; | |
$icon = ""; | |
} | |
$output .= '" foreground="white">' . "$icon $powref->{Total}->{Percentage}" . '%</span>'; | |
} | |
print $output . "\n"; | |
exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment