blob: 3a32acf98a42d58feff92af4be0e352880978ea8 [file] [log] [blame]
Copybara botca5ce642024-11-08 17:38:08 +01001#!/usr/bin/env perl
2#
3# Copyright 2014 Pierre Mavro <deimos@deimos.fr>
4# Copyright 2014 Vivien Didelot <vivien@didelot.org>
5#
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +01006# Original version:
7# https://github.com/vivien/i3blocks-contrib/blob/9d66d81da8d521941a349da26457f4965fd6fcbd/battery/battery
8# Modified by Adrià Vilanova, mainly to use upower
9# instead of acpi and adapt it to my style.
10#
11# I modified it because I found the battery numbers returned by acpi were not
12# stable and sometimes my battery switched numbers, so the percentage of
13# another "phantom" battery was shown. It seems like using upower fixes this.
Copybara botca5ce642024-11-08 17:38:08 +010014#
15# Licensed under the terms of the GNU GPL v3, or any later version.
16#
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010017# This script is meant to use with i3blocks. It parses the output of the
18# "upower" command to read the status of the battery, and eventually its
19# remaining time (to full charge or discharge).
Copybara botca5ce642024-11-08 17:38:08 +010020#
21# The color will gradually change for a percentage below 85%, and the urgency
22# (exit code 33) is set if there is less that 5% remaining.
23
24use strict;
25use warnings;
26use utf8;
27
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010028my $upower_result;
Copybara botca5ce642024-11-08 17:38:08 +010029my $status;
30my $percent;
31my $ac_adapt;
32my $full_text;
33my $short_text;
34my $bat_number = $ENV{BAT_NUMBER} || 0;
35my $label = $ENV{LABEL} || "";
36
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010037$upower_result = `upower --show-info "/org/freedesktop/UPower/devices/battery_BAT$bat_number"`;
Copybara botca5ce642024-11-08 17:38:08 +010038
39# fail on unexpected output
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010040if (not defined($upower_result)) {
41 # don't print anything to stderr if there is no battery
42 exit(0);
Copybara botca5ce642024-11-08 17:38:08 +010043}
44
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010045if ($upower_result !~ /^\s*state:\s*([\w\s]+)$/m) {
46 die("Can't read state")
47}
Copybara botca5ce642024-11-08 17:38:08 +010048$status = $1;
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010049
50if ($upower_result !~ /^\s*percentage:\s*(\d+)%$/m) {
51 die("Can't read percentage")
52}
53$percent = $1;
Copybara botca5ce642024-11-08 17:38:08 +010054$full_text = "$label";
55
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010056if ($status eq 'discharging') {
57 $full_text .= '⚡';
58} elsif ($status eq 'charging') {
59 $full_text .= '🔌';
Copybara botca5ce642024-11-08 17:38:08 +010060}
61
62$full_text .= "$percent%";
63
64$short_text = $full_text;
65
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010066# retrieve time
67if ($upower_result =~ /^\s*time to (?:empty|full):\s*(.+)$/m) {
68 $full_text .= " ($1)";
Copybara botca5ce642024-11-08 17:38:08 +010069}
70
71# print text
72print "$full_text\n";
73print "$short_text\n";
74
Copybara botca5ce642024-11-08 17:38:08 +010075if ($status eq 'Discharging') {
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010076 if ($percent < 20) {
77 print "#FF0000\n";
78 } elsif ($percent < 40) {
79 print "#FFAE00\n";
80 } elsif ($percent < 60) {
81 print "#FFF600\n";
82 } elsif ($percent < 85) {
83 print "#A8FF00\n";
84 }
Copybara botca5ce642024-11-08 17:38:08 +010085
Adrià Vilanova Martínezdb10c102024-12-27 20:06:39 +010086 # set the urgent flag (red background)
87 if ($percent < 5) {
88 exit(33);
89 }
Copybara botca5ce642024-11-08 17:38:08 +010090}
91
92exit(0);