Make battmux into its own program.

Avoids so many exec()s, especially bothersome when using extrace(1).
It's definitely more efficient as well.
This commit is contained in:
Érico Nogueira 2021-07-28 02:14:34 -03:00
parent 91dae7bf38
commit 4bcb0d0704
4 changed files with 53 additions and 8 deletions

View File

@ -0,0 +1,3 @@
EXE = battmux
include ../install.mk

View File

@ -0,0 +1,44 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// tmux or term colors
#define FGCOLOR(c) "#[fg=colour" c "]"
//#define FGCOLOR(c) "\x1b[38;5;" c "m" // https://en.wikipedia.org/wiki/ANSI_escape_code
#define BATPATH "/sys/class/power_supply/BAT0/"
int main()
{
char s[64];
struct timespec t;
struct tm tm;
if (clock_gettime(CLOCK_REALTIME, &t) < 0) return 1;
localtime_r(&t.tv_sec, &tm);
strftime(s, sizeof s, "%H:%M %d/%m %Y", &tm);
fputs(" " FGCOLOR("15"), stdout);
fputs(s, stdout);
fputs(FGCOLOR("0") " ", stdout);
int capfd = open(BATPATH "capacity", O_RDONLY);
int stafd = open(BATPATH "status", O_RDONLY);
if (capfd<0 || stafd<0) return 0;
if (read(stafd, s, sizeof s) >= 0)
if (strstr(s, "Charging"))
fputs("+ ", stdout);
close(stafd);
if (read(capfd, s, sizeof s) < 0) return 1;
close(capfd);
int cap = atoi(s);
for (int i = 0; i < 5; i++)
if (i <= cap / 20)
fputs(FGCOLOR("1") "", stdout);
else
fputs(FGCOLOR("8") "", stdout);
}

View File

@ -3,7 +3,7 @@
set -eu
GOINSTALL=
SSHFS=
UTILITIES=
SUBPROJECTS=
MPV=
THEMES=
@ -11,13 +11,13 @@ THEMES=
while [ $# -gt 0 ]; do
case $1 in
all) GOINSTALL=1
SSHFS=1
UTILITIES=1
SUBPROJECTS=1
MPV=1
THEMES=1
;;
go) GOINSTALL=1 ;;
sshfs) SSHFS=1 ;;
utilities) UTILITIES=1 ;;
sub) SUBPROJECTS=1 ;;
mpv) MPV=1;;
theme) THEMES=1;;
@ -36,8 +36,9 @@ if [ "$GOINSTALL" ]; then
go install ./keyboard_layout_switcher
fi
if [ "$SSHFS" ]; then
if [ "$UTILITIES" ]; then
make install -C sshfs-map
make install -C battmux
fi
if [ "$SUBPROJECTS" ]; then

View File

@ -37,7 +37,4 @@ set -g window-status-current-format "#[fg=colour15, italics] #W "
set -g window-status-format "#[fg=colour8, italics] #W "
set -g status-left-length 100
set -g status-left "#[fg=colour15]#(date +"%H:%M") #[fg=colour8]hrs "
set -ag status-left "#[fg=colour15]#(date +"%d/%m")#[fg=colour8] #(date +"%Y") "
set -ag status-left "#[fg=colour7]#(battmux -q)#[fg=colour12] "
set -g status-left "#(battmux)"