Skip to content

Instantly share code, notes, and snippets.

@zopsicle
Created February 9, 2020 18:24
Show Gist options
  • Save zopsicle/54d1b4fe01d914c393e6a55ce9429b96 to your computer and use it in GitHub Desktop.
Save zopsicle/54d1b4fe01d914c393e6a55ce9429b96 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.12;
use warnings;
sub cmus
{
my %stati = (
playing => '>>',
paused => '||️',
stopped => '[]️',
);
my $status;
my $artist;
my $title;
for (qx{2>/dev/null cmus-remote --query}) {
chomp;
$status = $1 if /^status (.*)$/;
$artist = $1 if /^tag artist (.*)$/;
$title = $1 if /^tag title (.*)$/;
}
my @segments = ($artist, $stati{$status // ''}, $title);
join(' ', grep($_, @segments));
}
sub battery
{
my %stati = (
'Charging' => '+',
'Discharging' => '-',
'Full' => '#',
);
my $status;
my $charge_full;
my $charge_now;
open(my $uevent, '<', '/sys/class/power_supply/BAT0/uevent');
while (<$uevent>) {
chomp;
$status = $1 if /^POWER_SUPPLY_STATUS=(.*)$/;
$charge_full = $1 if /^POWER_SUPPLY_CHARGE_FULL=(.*)$/;
$charge_now = $1 if /^POWER_SUPPLY_CHARGE_NOW=(.*)$/;
}
my $charge = sprintf("%2.f%%", 100 * $charge_now / $charge_full);
join(' ', grep($_, $stati{$status // ''}, $charge));
}
sub datetime
{
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime(time);
sprintf("%04d-%02d-%02d @ %02d:%02d:%02d",
$year + 1900, $mon + 1, $mday,
$hour, $min, $sec);
}
sub main
{
for (;;) {
sleep 1;
my @segments = (cmus, battery, datetime);
say(join(' ', grep($_, @segments)));
}
}
main;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment