From 1a45915976ed1ca2dd0cc690828ff160d8206622 Mon Sep 17 00:00:00 2001 From: archos Date: Tue, 30 Apr 2024 20:47:08 +0200 Subject: [PATCH] =?UTF-8?q?p=C5=99id=C3=A1n=20skript=20pro=20monitorov?= =?UTF-8?q?=C3=A1n=C3=AD=20stavu=20baterie=20notebooku.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/i3/scripts/battery1 | 114 ++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 .config/i3/scripts/battery1 diff --git a/.config/i3/scripts/battery1 b/.config/i3/scripts/battery1 new file mode 100644 index 0000000..3b9d5a7 --- /dev/null +++ b/.config/i3/scripts/battery1 @@ -0,0 +1,114 @@ +#!/usr/bin/perl +# +# Copyright 2014 Pierre Mavro +# Copyright 2014 Vivien Didelot +# +# 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. + +# Edited by Andreas Lindlbauer + +use strict; +use warnings; +use utf8; + +# otherwise we get in console "Wide character in print at" +binmode(STDOUT, ':utf8'); + +# my $acpi; +my $upower; +my $percent; +my $bat_state; +my $status; +my $ac_adapt; +my $full_text; +my $short_text; +my $label = '😅'; +my $bat_number = $ENV{BLOCK_INSTANCE} || 0; + +open (UPOWER, "upower -i /org/freedesktop/UPower/devices/battery_BAT$bat_number | grep 'percentage' |") or die; +$upower = ; +close(UPOWER); + +# fail on unexpected output +if ($upower !~ /: (\d+)%/) { + die "$upower\n"; +} + +$percent = $1; +$full_text = "$percent%"; + +open (BAT_STATE, "upower -i /org/freedesktop/UPower/devices/battery_BAT$bat_number | grep 'state' |") or die; +$bat_state = ; +close(BAT_STATE); + +if ($bat_state !~ /: (\w+)/) { + die "$bat_state\n"; +} +$status = $1; + +if ($status eq 'discharging') { + $full_text .= ' '; +} elsif ($status eq 'charging') { + $full_text .= ' '; +} elsif ($status eq 'Unknown') { + open (AC_ADAPTER, "acpi -a |") or die; + $ac_adapt = ; + close(AC_ADAPTER); + + if ($ac_adapt =~ /: ([\w-]+)/) { + $ac_adapt = $1; + + if ($ac_adapt eq 'on-line') { + $full_text .= ' CHR'; + } elsif ($ac_adapt eq 'off-line') { + $full_text .= ' DIS'; + } + } +} + +$short_text = $full_text; + +if ($percent < 20) { + $label = ''; +} elsif ($percent < 45) { + $label = ''; +} elsif ($percent < 70) { + $label = ''; +} elsif ($percent < 95) { + $label = ''; +} else { + $label = ''; +} + +# print text +print " ${label}"; +print " $full_text\n"; +print " ${label}"; +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 "#FFAE00\n"; + } elsif ($percent < 60) { + print "#FFF600\n"; + } elsif ($percent < 85) { + print "#A8FF00\n"; + } + + if ($percent < 5) { + exit(33); + } +} + +exit(0);