<commit_before>// Simple tools to read SysFS info on battery status.
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"path/filepath"
	"strconv"
	"strings"
)

var path = "/sys/class/power_supply/BAT*/"

// readGlob reads the file contents at a glob and returns their contents.
func readGlob(glob string) ([]string, error) {
	paths, err := filepath.Glob(glob)
	if err != nil {
		return []string{}, err
	}
	result := make([]string, len(paths))
	for i, p := range paths {
		bytes, err := ioutil.ReadFile(p)
		if err != nil {
			return []string{}, err
		}
		result[i] = strings.TrimRight(string(bytes), "\n")
	}
	return result, nil
}

func getBatteries() ([]battery, error) {
	pwr, err := readGlob(path + "energy_now")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/power_now: %v\n", path, err)
	}
	status, err := readGlob(path + "status")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/status: %v\n", path, err)
	}
	full, err := readGlob(path + "energy_full")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/energy_full: %v\n", path, err)
	}
	if len(pwr) != len(status) || len(status) != len(full) {
		return []battery{}, fmt.Errorf("%d power_now files, %d status files but %d energy_full files; expected all to exist\n", len(pwr), len(status), len(full))
	}
	result := make([]battery, len(pwr))
	for i := range status {
		c, err := parseCharge(pwr[i], full[i])
		if err != nil {
			return []battery{}, fmt.Errorf("couldn't parse battery charge: %v\n", err)
		}
		result[i] = battery{current: c, status: status[i]}
	}
	return result, nil
}

// parseCharge parses the charge percentage from string values.
func parseCharge(p, f string) (float32, error) {
	pi, err := strconv.ParseInt(p, 10, 0)
	if err != nil {
		return 0.0, err
	}
	fi, err := strconv.ParseInt(f, 10, 0)
	if err != nil {
		return 0.0, err
	}
	return float32(pi) / float32(fi), nil
}

type battery struct {
	current float32
	status  string
}

func main() {
	bat, err := getBatteries()
	if err != nil {
		log.Fatalf("failed to get battery info: %v\n", err)
	}
	for i, b := range bat {
		log.Printf("[Battery %d]: %+v\n", i, b)
	}
}<commit_msg>adds comments, fixes error msg<commit_after>// Simple tools to read SysFS info on battery status.
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"path/filepath"
	"strconv"
	"strings"
)

// Glob for SysFS paths, within which energy_now, energy_full and status files are assumed to exist.
var path = "/sys/class/power_supply/BAT*/"

type battery struct {
	current float32 // current charge, [0.0, 1.0]
	status  string  // status of battery
}

// readGlob reads the file contents at a glob and returns their contents.
func readGlob(glob string) ([]string, error) {
	paths, err := filepath.Glob(glob)
	if err != nil {
		return []string{}, err
	}
	result := make([]string, len(paths))
	for i, p := range paths {
		bytes, err := ioutil.ReadFile(p)
		if err != nil {
			return []string{}, err
		}
		result[i] = strings.TrimRight(string(bytes), "\n")
	}
	return result, nil
}

// getBatteries reads SysFS paths to find status of batteries.
func getBatteries() ([]battery, error) {
	pwr, err := readGlob(path + "energy_now")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/energy_now: %v\n", path, err)
	}
	status, err := readGlob(path + "status")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/status: %v\n", path, err)
	}
	full, err := readGlob(path + "energy_full")
	if err != nil {
		return []battery{}, fmt.Errorf("couldn't read SysFS %s/energy_full: %v\n", path, err)
	}
	if len(pwr) != len(status) || len(status) != len(full) {
		return []battery{}, fmt.Errorf("%d power_now files, %d status files but %d energy_full files; expected all to exist\n", len(pwr), len(status), len(full))
	}
	result := make([]battery, len(pwr))
	for i := range status {
		c, err := parseCharge(pwr[i], full[i])
		if err != nil {
			return []battery{}, fmt.Errorf("couldn't parse battery charge: %v\n", err)
		}
		result[i] = battery{current: c, status: status[i]}
	}
	return result, nil
}

// parseCharge parses the charge percentage from string values.
func parseCharge(p, f string) (float32, error) {
	pi, err := strconv.ParseInt(p, 10, 0)
	if err != nil {
		return 0.0, err
	}
	fi, err := strconv.ParseInt(f, 10, 0)
	if err != nil {
		return 0.0, err
	}
	return float32(pi) / float32(fi), nil
}

func main() {
	bat, err := getBatteries()
	if err != nil {
		log.Fatalf("failed to get battery info: %v\n", err)
	}
	for i, b := range bat {
		log.Printf("[Battery %d]: %+v\n", i, b)
	}
}
{'fraction_non_alphanumeric': 0.16956144159791575, 'fraction_numerical': 0.008684324793747287, 'mean_word_length': 3.8039624608967673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1302088', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1735, 'n_words': 533}