battery 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. use strict;
  15. use warnings;
  16. use utf8;
  17. my $acpi;
  18. my $status;
  19. my $percent;
  20. my $full_text;
  21. my $short_text;
  22. my $bat_number = $ENV{BLOCK_INSTANCE} || 0;
  23. # read the first line of the "acpi" command output
  24. open (ACPI, "acpi -b | grep 'Battery $bat_number' |") or die;
  25. $acpi = <ACPI>;
  26. close(ACPI);
  27. # fail on unexpected output
  28. if ($acpi !~ /: (\w+), (\d+)%/) {
  29. die "$acpi\n";
  30. }
  31. $status = $1;
  32. $percent = $2;
  33. $full_text = "$percent%";
  34. if ($status eq 'Discharging') {
  35. $full_text .= ' DIS';
  36. } elsif ($status eq 'Charging') {
  37. $full_text .= ' CHR';
  38. }
  39. $short_text = $full_text;
  40. if ($acpi =~ /(\d\d:\d\d):/) {
  41. $full_text .= " ($1)";
  42. }
  43. # print text
  44. print "$full_text\n";
  45. print "$short_text\n";
  46. # consider color and urgent flag only on discharge
  47. if ($status eq 'Discharging') {
  48. if ($percent < 20) {
  49. print "#FF0000\n";
  50. } elsif ($percent < 40) {
  51. print "#FFAE00\n";
  52. } elsif ($percent < 60) {
  53. print "#FFF600\n";
  54. } elsif ($percent < 85) {
  55. print "#A8FF00\n";
  56. }
  57. if ($percent < 5) {
  58. exit(33);
  59. }
  60. }
  61. exit(0);