Dotfiles for my tiling window manager + terminal workflow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.7 KiB

  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 2>/dev/null| grep 'Battery $bat_number' |") or die;
  25. $acpi = <ACPI>;
  26. close(ACPI);
  27. # fail on unexpected output
  28. if (not defined($acpi)) {
  29. # don't print anything to stderr if there is no battery
  30. exit(0);
  31. }
  32. elsif ($acpi !~ /: ([\w\s]+), (\d+)%/) {
  33. die "$acpi\n";
  34. }
  35. $status = $1;
  36. $percent = $2;
  37. $full_text = "$percent%";
  38. if ($status eq 'Discharging') {
  39. $full_text .= ' DIS';
  40. } elsif ($status eq 'Charging') {
  41. $full_text .= ' CHR';
  42. }
  43. $short_text = $full_text;
  44. if ($acpi =~ /(\d\d:\d\d):/) {
  45. $full_text .= " ($1)";
  46. }
  47. # print text
  48. print "$full_text\n";
  49. print "$short_text\n";
  50. # consider color and urgent flag only on discharge
  51. if ($status eq 'Discharging') {
  52. if ($percent < 20) {
  53. print "#FF0000\n";
  54. } elsif ($percent < 40) {
  55. print "#FFAE00\n";
  56. } elsif ($percent < 60) {
  57. print "#FFF600\n";
  58. } elsif ($percent < 85) {
  59. print "#A8FF00\n";
  60. } else {
  61. print "#2c9332\n";
  62. }
  63. if ($percent < 5) {
  64. exit(33);
  65. }
  66. } else {
  67. print "#2c9332\n";
  68. }
  69. exit(0);