Differences
This shows you the differences between two versions of the page.
tmux_battery_meter [2013/02/19 18:48] zashi created |
tmux_battery_meter [2013/02/19 18:56] (current) zashi |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== tmux Battery Meter ====== | ||
+ | I spend most of my time working from a terminal within a tmux session. When on my laptop it's nice to have instant visual access to my battery charge and if I'm on AC or not. | ||
+ | |||
+ | I wrote a little program that provides the battery's percent charge and shows a + if on AC and a - if running off the battery. | ||
+ | |||
+ | Amazingly enough, this is called tmux_bat_meter. | ||
+ | |||
+ | |||
+ | ===== Building ===== | ||
+ | |||
+ | This thing is pretty damn simple. To build it: | ||
+ | |||
+ | <code> | ||
+ | gcc tmux_bat_meter.c -o tmux_bat_meter | ||
+ | </code> | ||
+ | |||
+ | ===== Using ===== | ||
+ | |||
+ | To use this in tmux, plunk the compiled binary in your path add this to your tmux.conf: | ||
+ | |||
+ | <code> | ||
+ | set -g status-right '#(tmux_bat_meter) #[fg=blue]%H:%M#[default]' | ||
+ | </code> | ||
+ | |||
+ | This puts tmux_bat_meter in the right hand part of the status bar. See below for a screenshot. | ||
+ | |||
+ | ===== Screenshot ===== | ||
+ | |||
+ | {{:tmux_bat_meter.png?direct&600|}} | ||
+ | |||
+ | ===== Source ===== | ||
+ | |||
+ | <file c tmux_bat_meter.c> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | #include <unistd.h> | ||
+ | |||
+ | #define AC_PATH "/sys/class/power_supply/AC/online" | ||
+ | #define BAT_FULL "/sys/class/power_supply/BAT0/energy_full" | ||
+ | #define BAT_NOW "/sys/class/power_supply/BAT0/energy_now" | ||
+ | #define BAT_FULL2 "/sys/class/power_supply/BAT0/charge_full" | ||
+ | #define BAT_NOW2 "/sys/class/power_supply/BAT0/charge_now" | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | unsigned int ac; | ||
+ | float bat_full, bat_now; | ||
+ | |||
+ | FILE *fp; | ||
+ | |||
+ | /* are we on AC? */ | ||
+ | fp = fopen(AC_PATH, "r"); | ||
+ | fscanf(fp, "%d", &ac); | ||
+ | fclose(fp); | ||
+ | |||
+ | /* get current charge and max charge */ | ||
+ | if ( (! (fp = fopen( BAT_FULL, "r"))) && | ||
+ | (! (fp = fopen(BAT_FULL2, "r"))) ) | ||
+ | { | ||
+ | fprintf(stderr, "Could not open BAT_FULL/2\n"); | ||
+ | return(1); | ||
+ | } | ||
+ | |||
+ | fscanf(fp, "%f", &bat_full); | ||
+ | fclose(fp); | ||
+ | |||
+ | if ( (! (fp = fopen( BAT_NOW, "r"))) && | ||
+ | (! (fp = fopen(BAT_NOW2, "r"))) ) | ||
+ | { | ||
+ | fprintf(stderr, "Could not open BAT_NOW/2\n"); | ||
+ | return(2); | ||
+ | } | ||
+ | |||
+ | fscanf(fp, "%f", &bat_now); | ||
+ | fclose(fp); | ||
+ | |||
+ | printf("%c%2.0f%%", ac ? '+' : '-', (bat_now * 100) / bat_full); | ||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </file> |