From 89ac0867dda151c59dc5a8c4584880e3bab86e66 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 27 Mar 2019 15:14:35 +0100 Subject: i3 status: Make battery display more flexible Signed-off-by: Florian Pritz --- .i3/conky-wrapper.pl | 44 +++++++++++++++++++++++++++----------------- .i3/conkyrc-perl | 5 +---- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/.i3/conky-wrapper.pl b/.i3/conky-wrapper.pl index 687762b..2e69d29 100755 --- a/.i3/conky-wrapper.pl +++ b/.i3/conky-wrapper.pl @@ -65,12 +65,15 @@ LINE: while (<$conky>) { } - if (m/^COMMAND: (.*) (.*)$/) { + if (m/^COMMAND: ([^\s]*)(?: (.*)?)$/) { my $command = $1; my @args = split / /, $2; die "Unknown command requested: $command" unless defined $commands{$command}; - $in_progress{full_text} = $commands{$command}->(@args); - push @data, clone \%in_progress; + my $command_ret = $commands{$command}->(@args); + for my $text (@$command_ret) { + $in_progress{full_text} = $text; + push @data, clone \%in_progress; + } undef %in_progress; next; } @@ -80,20 +83,27 @@ LINE: while (<$conky>) { sub get_bat_stats { - my $bat = shift // "BAT0"; - - open my $fh, "<", "/sys/class/power_supply/$bat/uevent"; - my %values; - while (my $line = <$fh>) { - my ($key, $value) = split /=/, $line; - $key =~ s/^POWER_SUPPLY_//; - chomp $value; - $values{$key} = $value; - } - close $fh; + my $bats = [@_] // ["BAT0"]; + + my $ret = []; - my $time_remaining_hours = 0; - $time_remaining_hours = $values{ENERGY_NOW} / $values{POWER_NOW} unless $values{POWER_NOW} == 0; + for my $bat (@$bats) { + next unless -e "/sys/class/power_supply/$bat"; - return sprintf("%s %s%%, %.2fW, %.2fh", $bat, $values{CAPACITY}, $values{POWER_NOW} / 1e6, $time_remaining_hours); + open my $fh, "<", "/sys/class/power_supply/$bat/uevent"; + my %values; + while (my $line = <$fh>) { + my ($key, $value) = split /=/, $line; + $key =~ s/^POWER_SUPPLY_//; + chomp $value; + $values{$key} = $value; + } + close $fh; + + my $time_remaining_hours = 0; + $time_remaining_hours = $values{ENERGY_NOW} / $values{POWER_NOW} unless $values{POWER_NOW} == 0; + + push @$ret, sprintf("%s %s%%, %.2fW, %.2fh", $bat, $values{CAPACITY}, $values{POWER_NOW} / 1e6, $time_remaining_hours); + } + return $ret; } diff --git a/.i3/conkyrc-perl b/.i3/conkyrc-perl index e41e6c4..e1b6f33 100644 --- a/.i3/conkyrc-perl +++ b/.i3/conkyrc-perl @@ -65,10 +65,7 @@ FULLTEXT: SWAP ${swap}/${swapmax} (${swapperc}%) ${endif} COLOR: \#5555FF -COMMAND: battery BAT0 - -COLOR: \#5555FF -COMMAND: battery BAT1 +COMMAND: battery BAT0 BAT1 FULLTEXT: ${loadavg} -- cgit v1.2.3-24-g4f1b