Skip to content

Instantly share code, notes, and snippets.

@purpleposeidon
Forked from supertunaman/drawbat.sh
Created March 15, 2011 23:49
Show Gist options
  • Save purpleposeidon/871745 to your computer and use it in GitHub Desktop.
Save purpleposeidon/871745 to your computer and use it in GitHub Desktop.
//purpleposeidon@gmail.com
//Mar 15 2011
//Checks the battery level every so often, and writes a color code escape sequence to ~/.battery_color
//compile this program, have it run at startup:
// crontab -e
// @reboot /home/USER/bin/batcolor
//Or something
//Change PS1 to cat ~/.battery_color in .bashrc
//Here's mine:
// PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u$(cat ~/.battery_color)@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\$ '
//TODO: It'd be nice to have it only update when charge_now is changed
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <wordexp.h>
const int sleep_time = 45; /* how often to check, in seconds */
const char out_file[] = "~/.battery_color"; /* color escape code written here */
const char cn_file[] = "/sys/class/power_supply/BAT0/charge_now";
const char cf_file[] = "/sys/class/power_supply/BAT0/charge_full";
const char color_not_running[] = "\033[0m\033[33m"; /* brown */
const char color_battery_gone[] = "\033[30m\033[1m"; /* dark gray */
const char *colors[] = {
"\033[0m\033[31m", /* red */
"\033[31m\033[1m", /* orange */
"\033[33m\033[1m", /* yellow */
"\033[0m\033[32m", /* green */
"\033[32m\033[1m", /* bright green */
};
const int col_count = 5;
int colorize(const char *s) {
/* Write string to file */
FILE *out;
wordexp_t p;
if (wordexp(out_file, &p, WRDE_SHOWERR)) {
return -1;
}
out = fopen(p.we_wordv[0], "w");
fputs(s, out);
fclose(out);
wordfree(&p);
return 0;
}
void not_running() {
colorize(color_not_running);
}
void battery_gone() {
colorize(color_battery_gone);
}
int update_color() {
/* Get charge, pick appropriate color */
int charge_now, charge_full, charge_index;
float perc;
FILE *fd;
fd = fopen(cf_file, "r");
if (!fd) return -1; /* battery probably removed */
fscanf(fd, "%i", &charge_full);
fclose(fd);
fd = fopen(cn_file, "r");
if (!fd) return -1;
fscanf(fd, "%i", &charge_now);
fclose(fd);
perc = ((float)charge_now)/charge_full;
charge_index = perc*(col_count+1);
if (colorize(colors[charge_index])) {
exit(EXIT_FAILURE);
}
return 0;
}
void handle_sig(int signum) {
/* call atexits */
exit(EXIT_SUCCESS);
}
int main() {
/* Have the color set appropriately when program gets killed */
atexit(not_running);
signal(SIGINT, handle_sig);
signal(SIGHUP, handle_sig);
/* Check that there is a battery file */
struct stat result;
errno = 0;
if (stat(cn_file, &result) || errno) {
perror("Unable to access battery information");
return EXIT_FAILURE;
}
/* Wait quietly in the darkness */
for (;;) {
if (update_color()) {
battery_gone();
}
sleep(sleep_time);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment