Skip to content

Commit

Permalink
init: create bat
Browse files Browse the repository at this point in the history
  • Loading branch information
tshakalekholoane committed Jan 13, 2021
0 parents commit 3178ab0
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
bat

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Tshaka Eric Lekholoane

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# `bat`

```
bat
NAME
bat - battery management utility for Linux laptops
SYNOPSIS
bat [OPTION]
DESCRIPTION
-c, --capacity, print current battery level
-h, --help, print this help document
-t, --threshold, print the current charging threshold limit
append a value between 1 and 100 to set a new threshold
-s, --status print charging status
```

## About

Aims to replicate the functionality of the [ASUS Battery Health Charging](https://www.asus.com/us/support/FAQ/1032726/) utility for ASUS laptops on Windows which aims to prolong the battery's life-span <a href="https://electrek.co/2017/09/01/tesla-battery-expert-recommends-daily-battery-pack-charging/"><sup>1</sup></a> <a href="https://batteryuniversity.com/learn/article/how_to_prolong_lithium_based_batteries"><sup>2</sup></a>.

## Installation

Precompiled binaries (Linux x86-64) are available from the [GitHub releases page](https://github.com/leveson/bat/releases).

Alternatively, one could build the binary oneself by running the following [Go](https://golang.org/) command,
```shell
$ go build bat.go
```
and placing the resulting binary in a directory that is in their `$PATH` such as `/usr/local/bin`.
111 changes: 111 additions & 0 deletions bat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)

var help string = `
bat
NAME
bat - battery management utility for Linux laptops
SYNOPSIS
bat [OPTION]
DESCRIPTION
-c, --capacity, print current battery level
-h, --help, print this help document
-t, --threshold, print the current charging threshold limit
append a value between 1 and 100 to set a new threshold
-s, --status print charging status
REFERENCE
https://wiki.archlinux.org/index.php/Laptop/ASUS#Battery_charge_threshold
13 JANUARY 2021
`

func cat(file string) {
cmd := exec.Command("cat", file)
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
fmt.Print(string(out))
}

func page(out string) {
cmd := exec.Command("/usr/bin/less")
cmd.Stdin = strings.NewReader(out)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}

func setThreshold(t int) {
st := fmt.Sprintf("echo %d > "+
"/sys/class/power_supply/BAT0/charge_control_end_threshold", t)
cmd := exec.Command("su", "-c", st)
fmt.Print("Root password: ")
cmd.Stdin = os.Stdin
err := cmd.Run()
fmt.Println()
if err != nil {
fmt.Println("Authentication failure.")
os.Exit(1)
}
}

func main() {
nArgs := len(os.Args)

if nArgs == 1 {
page(help)
os.Exit(1)
}

switch os.Args[1] {
case "-c", "--capacity":
cat("/sys/class/power_supply/BAT0/capacity")
case "-h", "--help":
page(help)
case "-t", "--threshold":
switch {
case nArgs > 3:
fmt.Print("Expects single argument.\n")
case nArgs == 3:
t, err := strconv.Atoi(os.Args[2])
if err != nil {
if errors.Is(err, strconv.ErrSyntax) {
fmt.Print("Argument should be an integer.\n")
os.Exit(1)
} else {
log.Fatal(err)
}
}
if t < 1 || t > 100 {
fmt.Print("Number should be between 1 and 100.\n")
os.Exit(1)
}
setThreshold(t)
fmt.Printf("Charging threshold set to %s.\n", os.Args[2])
default:
cat("/sys/class/power_supply/BAT0/charge_control_end_threshold")
os.Exit(0)
}
case "-s", "--status":
cat("/sys/class/power_supply/BAT0/status")
default:
fmt.Printf("There is no %s option. Use bat --help to see a list of"+
"available options.\n", os.Args[1])
}
}

0 comments on commit 3178ab0

Please sign in to comment.