Adding battery charge to Tmux status bar
If you are not aware of tmux, do check it out. It’s a solution for problem you didn’t know you have. And, once you’re in its grasp, it’s very hard to get out.
One nice feature it does have is permanent status bar. My status bar used to just show CPU load.
set-window-option -g status-right ' #( vmstat 1 2 | tail -1 | awk "{ USAGE=100-\$15; if (USAGE<20) { printf \"#[fg=green]\"; } else if (USAGE<80) { printf \"#[fg=brightyellow]\"; } else { printf \"#[bg=red]#[fg=brightwhite]\"; }; print \" \" USAGE \"%\" }" ) 'However, after my laptop turned off on me while logged on remotely once too many, I decided to have battery charge there too. Since I don’t really care about the exact percentage, I decided to have it represented as a single “block” (▁▂▃▄▅▆▇█) character. To get capacity, I just used /sys/class/power_supply/BAT*/capacity. With percentage in hand, I then used a bit of math to have each block represent 14% of the charge, with the last 2% of charge using the last block. Since I use reverse coloring for status bar, blocks are ordered in increasing order.
#( awk '\''{print substr("▁▂▃▄▅▆▇█", int($1*7/98)+1, 1)}'\'' /sys/class/power_supply/BAT*/capacity || echo "█" )'So, mission accomplished! Well, there’s always some scope creep. In my case, I also wanted to see charging status. This can be found in /sys/class/power_supply/BAT*/status. If value is “Charging”, I wanted negative space above battery block to be a different color.
#( awk '\''$1=="Charging" {print "#[fg=brightmagenta]"; next} {print "#[fg=black]"}'\'' /sys/class/power_supply/BAT*/status )#[reverse]#( awk '\''{print substr("▁▂▃▄▅▆▇█", int($1*7/98)+1, 1)}'\'' /sys/class/power_supply/BAT*/capacity || echo "█" )'Essentially, if status is “Charging”, awk will output bright magenta ANSI foreground code. This then gets reversed (i.e. background and foreground swapped), followed by battery charge block. Probably as much information as one can push into a single character.
PS: For curious, this is my full status line.
set-window-option -g status-right ' #( vmstat 1 2 | tail -1 | awk "{ USAGE=100-\$15; if (USAGE<20) { printf \"#[fg=green]\"; } else if (USAGE<80) { printf \"#[fg=brightyellow]\"; } else { printf \"#[bg=red]#[fg=brightwhite]\"; }; print \" \" USAGE \"%\" }" )#( awk '\''$1=="Charging" {print "#[fg=brightmagenta]"; next} {print "#[fg=black]"}'\'' /sys/class/power_supply/BAT*/status )#[reverse]#( awk '\''{print substr("▁▂▃▄▅▆▇█", int($1*7/98)+1, 1)}'\'' /sys/class/power_supply/BAT*/capacity || echo "█" )'