~kota/battery

1414534a95d0e9e689bcfa4e71bed5fc86ec41a7 — Dakota Walsh 3 months ago
initial commit
6 files changed, 162 insertions(+), 0 deletions(-)

A .gitignore
A Makefile
A README.md
A config.mk
A go.mod
A main.go
A  => .gitignore +1 -0
@@ 1,1 @@
battery

A  => Makefile +23 -0
@@ 1,23 @@
# battery
# BSD 3-Clause: See LICENSE in the root of this repo file for copyright and license details.
.POSIX:

include config.mk

all: clean build

build:
	go build

clean:
	rm -f battery

install: build
	mkdir -p $(DESTDIR)$(PREFIX)/bin
	cp -f battery $(DESTDIR)$(PREFIX)/bin
	chmod 755 $(DESTDIR)$(PREFIX)/bin/battery

uninstall:
	rm -f $(DESTDIR)$(PREFIX)/bin/battery

.PHONY: all build clean install uninstall

A  => README.md +45 -0
@@ 1,45 @@
Battery
========

`battery [charge_suffix] [decharge_suffix] [concat?]`

The battery tool is meant to read battery charge percent and status and print
to STDOUT.

Different amounts of options change how the program works. A single option `+`
for example will make the program print the option back to you if your battery
is charging. It will print nothing if it is not charging. If you instead want it
to print something when it is discharging, add another option. So, `battery + -`
will print a `+` when charging or a `-` when discharging.

Finally, to have the battery percent and charging status concatenated together
add a third option. `battery + - =` will print your battery charge percent and
the corresponding symbol for its charging status.

License
--------

See LICENSE in the root of this repo file for copyright and license details.

Build
------

Build dependencies  

 * golang
 * make

`make all`

Install
--------

Optionally configure `config.mk` to specify a different install location.  
Defaults to `/usr/local/`

`sudo make install`

Uninstall
----------

`sudo make uninstall`

A  => config.mk +7 -0
@@ 1,7 @@
# battery
VERSION = 0.3.0

# Customize below to fit your system

# paths
PREFIX = /usr/local

A  => go.mod +3 -0
@@ 1,3 @@
module git.sr.ht/~kota/useless/battery

go 1.12

A  => main.go +83 -0
@@ 1,83 @@
// battery - Print battery charge percent or status.
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

const (
	nowPath    string = "/sys/class/power_supply/BAT0/energy_now"
	maxPath    string = "/sys/class/power_supply/BAT0/energy_full"
	chargePath string = "/sys/class/power_supply/BAT0/status"
)

// return an int percent from two int values
func percent(n, m int) int {
	return int(float32(n) / (float32(m) / 100))
}

// read and return battery charge percentage as int
func battery(np, mp string) int {
	nb, err := ioutil.ReadFile(np)
	mb, err := ioutil.ReadFile(mp)
	if err != nil {
		fmt.Printf("failed to read file: %v\n", err)
		os.Exit(1)
	}

	// convert types
	ns := strings.TrimSuffix(string(nb), "\n")
	ni, err := strconv.Atoi(ns)
	ms := strings.TrimSuffix(string(mb), "\n")
	mi, err := strconv.Atoi(ms)

	// return int percentage
	return percent(ni, mi)
}

// read charging status and return a boolean
func charging(p string) bool {
	cb, err := ioutil.ReadFile(p)
	if err != nil {
		fmt.Printf("failed to read file: %v\n", err)
		os.Exit(1)
	}

	cs := strings.TrimSuffix(string(cb), "\n")
	if cs == "Charging" {
		return true
	} else {
		return false
	}
}

func main() {
	switch a := len(os.Args); a {
	case 1:
		// print the battery percent
		fmt.Println(battery(nowPath, maxPath))
	case 2:
		// print battery increase or no symbol
		if charging(chargePath) {
			fmt.Println(os.Args[1])
		}
	case 3:
		// print battery increase or decrease symbol
		if charging(chargePath) {
			fmt.Println(os.Args[1])
		} else {
			fmt.Println(os.Args[2])
		}
	default:
		// print battery increase or decrease symbol
		if charging(chargePath) {
			fmt.Print(battery(nowPath, maxPath), os.Args[1], "\n")
		} else {
			fmt.Print(battery(nowPath, maxPath), os.Args[2], "\n")
		}
	}
}