From: Simon Ruderich Date: Fri, 12 Nov 2010 20:39:27 +0000 (+0100) Subject: screenrc: Display current battery charge. X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=commitdiff_plain;h=fb9ed9fba7a9d6acffa895de0a5ba9fd17514c40 screenrc: Display current battery charge. Also add battery.pl script and update setup.sh to only enable it if necessary. --- diff --git a/bin/battery.pl b/bin/battery.pl new file mode 100755 index 0000000..31501ac --- /dev/null +++ b/bin/battery.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + + +use strict; +use warnings; + + +my $path = '/sys/class/power_supply/BAT0/uevent'; + +# No battery available. +if (! -e $path) { + exit; +} + +my $screen_mode = (defined $ARGV[0] and $ARGV[0] eq '-s'); + +my %battery; + +open my $file, '<', $path; +while (<$file>) { + /^POWER_SUPPLY_([A-Z_]+)=(.+)$/; + $battery{$1} = $2; +} +close $file; + +my $charge = int($battery{CHARGE_NOW} / $battery{CHARGE_FULL} * 100); + +# GNU screen mode with colors: 0-20 red, 20-40 yellow, 40-100 green. +if ($screen_mode) { + my $color; + if ($charge < 20) { + $color = 'b r'; + } elsif ($charge < 40) { + $color = 'b y'; + } else { + $color = 'b g'; + } + + print "\005{+$color}$charge%\005{-}\n"; + +# Just text output. +} else { + print "$charge%\n"; +} diff --git a/screenrc.in b/screenrc.in index 02212fb..37d61da 100644 --- a/screenrc.in +++ b/screenrc.in @@ -37,6 +37,10 @@ attrcolor b ".I" # for window borders in split mode). sorendition = Bw +# Display current battery charge if running on a laptop. Redisplay every 3 +# minutes. +backtick 1 180 180 $HOME/.shell/bin/battery.pl -s # (BATTERY) + # Make sure the temporary variable we use is empty. Thanks to # http://gist.github.com/133000 for the idea to use setenv and to TauPan in # #screen on Freenode (2010-03-10 16:11 CET) for reminding me of it and @@ -61,6 +65,8 @@ setenv s "$s%=" # - right align the following text setenv s "$s%l" # - load average setenv s "$s " # - space setenv s "$s%H" # - hostname +setenv s "$s " # - space (BATTERY) +setenv s "$s%1`" # - battery status (BATTERY) hardstatus alwayslastline "$s" unsetenv s diff --git a/setup.sh b/setup.sh index a7ae8fd..ec6f9d7 100755 --- a/setup.sh +++ b/setup.sh @@ -33,6 +33,13 @@ if installed rxvt; then sed 's/attrcolor b ".I"/#attrcolor b ".I"/' screenrc > screenrc.tmp mv screenrc.tmp screenrc fi +# Display current battery charge on computers with a battery. Necessary lines +# are marked as "(BATTERY)". +if [ ! -d /sys/class/power_supply/BAT0 ]; then + echo screenrc: removing battery display + grep -v '(BATTERY)' screenrc > screenrc.tmp + mv screenrc.tmp screenrc +fi # Link setup for shells. link shell ~/.shell