dotfiles

Yes, my $HOME has a git repo now :(

git clone git://git.shimmy1996.com/dotfiles.git

battery (2169B)

    1 #!/usr/bin/perl
    2 #
    3 # Copyright 2014 Pierre Mavro <deimos@deimos.fr>
    4 # Copyright 2014 Vivien Didelot <vivien@didelot.org>
    5 #
    6 # Licensed under the terms of the GNU GPL v3, or any later version.
    7 #
    8 # This script is meant to use with i3blocks. It parses the output of the "acpi"
    9 # command (often provided by a package of the same name) to read the status of
   10 # the battery, and eventually its remaining time (to full charge or discharge).
   11 #
   12 # The color will gradually change for a percentage below 85%, and the urgency
   13 # (exit code 33) is set if there is less that 5% remaining.
   14 
   15 use strict;
   16 use warnings;
   17 use utf8;
   18 
   19 my $acpi;
   20 my $status;
   21 my $percent;
   22 my $ac_adapt;
   23 my $full_text;
   24 my $short_text;
   25 my $bat_number = $ENV{BAT_NUMBER} || 0;
   26 my $label = $ENV{LABEL} || "";
   27 
   28 # read the first line of the "acpi" command output
   29 open (ACPI, "acpi -b 2>/dev/null| grep 'Battery $bat_number' |") or die;
   30 $acpi = <ACPI>;
   31 close(ACPI);
   32 
   33 # fail on unexpected output
   34 if (not defined($acpi)) {
   35     # don't print anything to stderr if there is no battery
   36     exit(0);
   37 }
   38 elsif ($acpi !~ /: ([\w\s]+), (\d+)%/) {
   39     die "$acpi\n";
   40 }
   41 
   42 $status = $1;
   43 $percent = $2;
   44 $full_text = "$label$percent%";
   45 
   46 if ($status eq 'Discharging') {
   47     $full_text .= ' DIS';
   48 } elsif ($status eq 'Charging') {
   49     $full_text .= ' CHR';
   50 } elsif ($status eq 'Unknown') {
   51     open (AC_ADAPTER, "acpi -a |") or die;
   52     $ac_adapt = <AC_ADAPTER>;
   53     close(AC_ADAPTER);
   54 
   55     if ($ac_adapt =~ /: ([\w-]+)/) {
   56         $ac_adapt = $1;
   57 
   58         if ($ac_adapt eq 'on-line') {
   59             $full_text .= ' CHR';
   60         } elsif ($ac_adapt eq 'off-line') {
   61             $full_text .= ' DIS';
   62         }
   63     }
   64 }
   65 
   66 $short_text = $full_text;
   67 
   68 if ($acpi =~ /(\d\d:\d\d):/) {
   69     $full_text .= " ($1)";
   70 }
   71 
   72 # print text
   73 print "$full_text\n";
   74 print "$short_text\n";
   75 
   76 # consider color and urgent flag only on discharge
   77 if ($status eq 'Discharging') {
   78 
   79     if ($percent < 20) {
   80         print "#FF0000\n";
   81     } elsif ($percent < 40) {
   82         print "#FFAE00\n";
   83     } elsif ($percent < 60) {
   84         print "#FFF600\n";
   85     } elsif ($percent < 85) {
   86         print "#A8FF00\n";
   87     }
   88 
   89     if ($percent < 5) {
   90         exit(33);
   91     }
   92 }
   93 
   94 exit(0);