1
0
mirror of https://github.com/frebib/dotfiles.git synced 2024-06-14 12:57:23 +00:00
dotfiles/.config/i3blocks/blocks/battery
2016-03-31 03:08:15 +01:00

98 lines
2.1 KiB
Perl
Executable File

#!/usr/bin/perl
#
# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
# Copyright 2014 Vivien Didelot <vivien@didelot.org>
#
# Licensed under the terms of the GNU GPL v3, or any later version.
#
# This script is meant to use with i3blocks. It parses the output of the "acpi"
# command (often provided by a package of the same name) to read the status of
# the battery, and eventually its remaining time (to full charge or discharge).
#
# The color will gradually change for a percentage below 85%, and the urgency
# (exit code 33) is set if there is less that 5% remaining.
use strict;
use warnings;
use Switch;
my $acpi;
my $status;
my $percent;
my $hours = -1;
my $mins;
my $full_text;
my $short_text;
my $bat_number = $ENV{BLOCK_INSTANCE} || 0;
# read the first line of the "acpi" command output
open (ACPI, "acpi -b | grep 'Battery $bat_number' |") or die;
$acpi = <ACPI>;
close(ACPI);
# fail on unexpected output
if ($acpi !~ /: (\w+), (\d+)%/) {
die "$acpi\n";
}
$status = $1;
$percent = $2;
if ($acpi =~ /, (\d+):(\d+)/) {
$hours = $1;
$mins = $2;
}
if ($status eq 'Discharging') {
if ($percent <= 10) {
$full_text = "";
} elsif ($percent <= 40) {
$full_text = "";
} elsif ($percent <= 75) {
$full_text = "";
} else {
$full_text = "";
}
} elsif ($status eq 'Charging') {
$full_text = '';
} else {
$full_text = '';
}
$full_text .= " $percent%";
$short_text = $full_text;
switch($ENV{BLOCK_BUTTON} || 0) {
case "1" {
if ($hours lt 0) {
$full_text .= " Charged";
} else {
$full_text .= " ($hours:$mins";
if ($status eq 'Discharging') {
$full_text .= ")";
} else {
$full_text .= " until full)";
}
}
}
case "4" { system("light -U -l 1") }
case "5" { system("light -A -l 1") }
}
# print text
print "$full_text\n";
print "$short_text\n";
# consider color and urgent flag only on discharge
if ($status eq 'Discharging') {
if ($percent < 20) {
print "#FF0000\n";
} elsif ($percent < 40) {
print "#FFCB00\n";
}
}
exit(0);