Skip to content

Commit

Permalink
style: minor formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tshaka Eric Lekholoane committed Sep 9, 2021
1 parent 0e22a62 commit 7f1c61c
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 421 deletions.
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/bug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: File a bug report
title: "issue: "
labels: [bug]
assignees:
- leveson
- tshakalekholoane
body:
- type: textarea
id: describe-issue
Expand Down Expand Up @@ -40,7 +40,8 @@ body:
label: Version
description: Which version of bat are you running?
options:
- "0.8.2 (Latest)"
- "0.8.3 (Latest)"
- "0.8.2"
- "0.8.1"
- "0.8"
- "0.7"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ The goal is to replicate the functionality of the [ASUS Battery Health Charging]

This has only shown to work on ASUS laptops. For Dell systems, see [smbios-utils](https://github.com/dell/libsmbios), particularly the `smbios-battery-ctl` command, or install it using your package manager. For other manufacturers there is also [TLP](https://linrunner.de/tlp/).

There have also been some [problems setting the charging threshold inside of a virtual machine](https://github.com/leveson/bat/issues/3#issuecomment-858581495).
There have also been some [problems setting the charging threshold inside of a virtual machine](https://github.com/tshakalekholoane/bat/issues/3#issuecomment-858581495).

## Installation

Precompiled binaries (Linux x86-64) are available from the [GitHub releases page](https://github.com/leveson/bat/releases), the latest of which can be downloaded from [here](https://github.com/leveson/bat/releases/download/0.8.2/bat).
Precompiled binaries (Linux x86-64) are available from the [GitHub releases page](https://github.com/tshakalekholoane/bat/releases), the latest of which can be downloaded from [here](https://github.com/tshakalekholoane/bat/releases/download/0.8.3/bat).

After downloading the binary, give it permission to execute on your system by running the following command. For example, assuming the binary is located in the user's Downloads folder:

Expand Down
247 changes: 124 additions & 123 deletions cmd/bat/main.go
Original file line number Diff line number Diff line change
@@ -1,136 +1,137 @@
package main

import (
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/leveson/bat/internal/docs"
"github.com/leveson/bat/internal/io"
"github.com/leveson/bat/internal/persist"
"github.com/leveson/bat/internal/threshold"
"github.com/tshakalekholoane/bat/internal/docs"
"github.com/tshakalekholoane/bat/internal/io"
"github.com/tshakalekholoane/bat/internal/persist"
"github.com/tshakalekholoane/bat/internal/threshold"
)

// printFile is a wrapper around `io.FileContents` to simplify printing
// the values of some (battery) virtual files.
func printFile(vf string) {
s, err := io.FileContents(vf)
if err != nil {
if err.Error() == "virtual file not found" {
fmt.Println(
"This program is most likely not compatible with your " +
"system. See\nhttps://github.com/leveson/bat#disclaimer.")
os.Exit(1)
}
log.Fatal(err)
}
fmt.Println(s)
s, err := io.FileContents(vf)
if err != nil {
if err.Error() == "virtual file not found" {
fmt.Println(
"This program is most likely not compatible with your system." +
" See\nhttps://github.com/tshakalekholoane/bat#disclaimer.")
os.Exit(1)
}
log.Fatal(err)
}
fmt.Println(s)
}

func main() {
if len(os.Args) == 1 {
docs.Help()
os.Exit(0)
}
switch os.Args[1] {
case "-c", "--capacity":
printFile("capacity")
case "-h", "--help":
err := docs.Help()
if err != nil {
log.Fatal(err)
}
case "-p", "--persist":
err := persist.WriteServices()
if err != nil {
switch {
case err.Error() == "bash not found":
fmt.Println("Requires Bash to persist the charging threshold.")
os.Exit(1)
case err.Error() == "incompatible systemd version":
fmt.Println("Requires systemd version 244-rc1 or later.")
os.Exit(1)
case err.Error() == "virtual file not found":
fmt.Println(
"This program is most likely not compatible with your " +
"system. See\nhttps://github.com/leveson/bat#disclaimer.")
os.Exit(1)
case strings.HasSuffix(err.Error(), "permission denied"):
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
default:
log.Fatal(err)
}
}
fmt.Println("Persistence of the current charging threshold enabled.")
case "-r", "--reset":
err := persist.RemoveServices()
if err != nil {
if strings.HasSuffix(err.Error(), "permission denied") {
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
}
log.Fatal(err)
}
fmt.Println("Charging threshold persistence reset.")
case "-s", "--status":
printFile("status")
case "-t", "--threshold":
switch {
case len(os.Args) > 3:
fmt.Println("Expects a single argument.")
os.Exit(1)
case len(os.Args) == 3:
t, err := strconv.Atoi(os.Args[2])
if err != nil {
if errors.Is(err, strconv.ErrSyntax) {
fmt.Println("Argument should be an integer.")
os.Exit(1)
}
log.Fatal(err)
}
if t < 1 || t > 100 {
fmt.Println("Number should be between 1 and 100.")
os.Exit(1)
}
err = threshold.Write(t)
if err != nil {
switch {
case err.Error() == "incompatible kernel version":
fmt.Println("Requires Linux kernel version 5.4 or later.")
os.Exit(1)
case err.Error() == "virtual file not found":
fmt.Println(
"This program is most likely not compatible with " +
"your system. See\n" +
"https://github.com/leveson/bat#disclaimer.")
os.Exit(1)
case strings.HasSuffix(err.Error(), "permission denied"):
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
default:
log.Fatal(err)
}
}
fmt.Println(
"Charging threshold set.\nUse `sudo bat --persist` to " +
"persist the setting between restarts.")
default:
printFile("charge_control_end_threshold")
}
case "-v", "--version":
err := docs.VersionInfo()
if err != nil {
log.Fatal(err)
}
default:
fmt.Printf(
"There is no %s option. Use `bat --help` to see a list of " +
"available options.\n",
os.Args[1])
os.Exit(1)
}
if len(os.Args) == 1 {
docs.Help()
os.Exit(0)
}
switch os.Args[1] {
case "-c", "--capacity":
printFile("capacity")
case "-h", "--help":
err := docs.Help()
if err != nil {
log.Fatal(err)
}
case "-p", "--persist":
err := persist.WriteServices()
if err != nil {
switch {
case err.Error() == "bash not found":
fmt.Println("Requires Bash to persist the charging threshold.")
os.Exit(1)
case err.Error() == "incompatible systemd version":
fmt.Println("Requires systemd version 244-rc1 or later.")
os.Exit(1)
case err.Error() == "virtual file not found":
fmt.Println(
"This program is most likely not compatible with your " +
"system. See\n" +
"https://github.com/tshakalekholoane/bat#disclaimer.")
os.Exit(1)
case strings.HasSuffix(err.Error(), "permission denied"):
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
default:
log.Fatal(err)
}
}
fmt.Println("Persistence of the current charging threshold enabled.")
case "-r", "--reset":
err := persist.RemoveServices()
if err != nil {
if strings.HasSuffix(err.Error(), "permission denied") {
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
}
log.Fatal(err)
}
fmt.Println("Charging threshold persistence reset.")
case "-s", "--status":
printFile("status")
case "-t", "--threshold":
switch {
case len(os.Args) > 3:
fmt.Println("Expects a single argument.")
os.Exit(1)
case len(os.Args) == 3:
t, err := strconv.Atoi(os.Args[2])
if err != nil {
if errors.Is(err, strconv.ErrSyntax) {
fmt.Println("Argument should be an integer.")
os.Exit(1)
}
log.Fatal(err)
}
if t < 1 || t > 100 {
fmt.Println("Number should be between 1 and 100.")
os.Exit(1)
}
err = threshold.Write(t)
if err != nil {
switch {
case err.Error() == "incompatible kernel version":
fmt.Println("Requires Linux kernel version 5.4 or later.")
os.Exit(1)
case err.Error() == "virtual file not found":
fmt.Println(
"This program is most likely not compatible with your" +
" system. See\n" +
"https://github.com/tshakalekholoane/bat#disclaimer.")
os.Exit(1)
case strings.HasSuffix(err.Error(), "permission denied"):
fmt.Println("This command requires sudo permissions.")
os.Exit(1)
default:
log.Fatal(err)
}
}
fmt.Println(
"Charging threshold set.\nUse `sudo bat --persist` to " +
"persist the setting between restarts.")
default:
printFile("charge_control_end_threshold")
}
case "-v", "--version":
err := docs.VersionInfo()
if err != nil {
log.Fatal(err)
}
default:
fmt.Printf(
"There is no %s option. Use `bat --help` to see a list of " +
"available options.\n",
os.Args[1])
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/leveson/bat
module github.com/tshakalekholoane/bat

go 1.16
go 1.17
46 changes: 23 additions & 23 deletions internal/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package docs

import (
_ "embed"
"os"
"os/exec"
"strings"
_ "embed"
"os"
"os/exec"
"strings"
)

//go:embed help.txt
Expand All @@ -13,35 +13,35 @@ var help string
//go:embed version.txt
var version string

// page calls the less pager on the `doc` string input. A successful
// page calls the less pager on the `doc` string input. A successful
// call returns err == nil.
func page(doc string) error {
cmd := exec.Command("less", "--IGNORE-CASE", "--quit-if-one-screen")
cmd.Stdin = strings.NewReader(doc)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return err
}
return nil
cmd := exec.Command("less", "--IGNORE-CASE", "--quit-if-one-screen")
cmd.Stdin = strings.NewReader(doc)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return err
}
return nil
}

// Help shows the help document through the less pager. A successful
// call returns err == nil.
func Help() error {
err := page(help)
if err != nil {
return err
}
return nil
err := page(help)
if err != nil {
return err
}
return nil
}

// VersionInfo shows version information through the less pager. A
// successful call returns err == nil.
func VersionInfo() error {
err := page(version)
if err != nil {
return err
}
return nil
err := page(version)
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion internal/docs/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ EXAMPLES
$ sudo bat --persist

SUPPORT
Report any issues on https://github.com/leveson/bat/issues.
Report any issues on https://github.com/tshakalekholoane/bat/issues.

REFERENCE
https://wiki.archlinux.org/title/Laptop/ASUS#Battery_charge_threshold
Expand Down
2 changes: 1 addition & 1 deletion internal/docs/version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bat 0.8.2
bat 0.8.3
Copyright (c) 2021 Tshaka Eric Lekholoane.
MIT Licence.
Loading

0 comments on commit 7f1c61c

Please sign in to comment.