first version
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"backlight-control/cmd/set"
|
||||
"backlight-control/cmd/status"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd *cobra.Command
|
||||
|
||||
func main() {
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "backlight-control",
|
||||
Short: "backlight control tool",
|
||||
SilenceUsage: true,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.PersistentFlags().String("path", "", "control file directory path (/path/to/dir/{brightness,max_brightness})")
|
||||
|
||||
if err := rootCmd.MarkPersistentFlagRequired("path"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
set.NewCommand(rootCmd)
|
||||
status.NewCommand(rootCmd)
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package set
|
||||
|
||||
import (
|
||||
"backlight-control/internal/backlight"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCommand(parent *cobra.Command) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "set [+/-]VAL[%]",
|
||||
Aliases: []string{"s"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: run,
|
||||
Short: "set backlight value",
|
||||
}
|
||||
|
||||
parent.AddCommand(cmd)
|
||||
}
|
||||
|
||||
type operate int
|
||||
|
||||
const (
|
||||
None operate = 1 << iota
|
||||
Equal
|
||||
Add
|
||||
Subtract
|
||||
)
|
||||
|
||||
var changeValueRegex = regexp.MustCompile(`^([+-])?(\d+(\.\d+)?)%?$`)
|
||||
|
||||
func parseChange(val string) (op operate, value float32, err error) {
|
||||
|
||||
find := changeValueRegex.FindAllStringSubmatch(val, -1)
|
||||
if find == nil || len(find) < 1 {
|
||||
return None, 0, errors.New("input value pattern invalid")
|
||||
}
|
||||
|
||||
findList := find[0]
|
||||
if len(findList) < 3 {
|
||||
return None, 0, fmt.Errorf("parsed value invalid, %v", findList)
|
||||
}
|
||||
|
||||
opStr := findList[1]
|
||||
valStr := findList[2]
|
||||
|
||||
switch opStr {
|
||||
case "+":
|
||||
op = Add
|
||||
case "-":
|
||||
op = Subtract
|
||||
default:
|
||||
op = Equal
|
||||
}
|
||||
|
||||
f64, err := strconv.ParseFloat(valStr, 32)
|
||||
if err != nil {
|
||||
return None, 0, fmt.Errorf("parse value to float fail: (%w)", err)
|
||||
}
|
||||
|
||||
value = float32(f64)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) (err error) {
|
||||
dirPath, err := cmd.Flags().GetString("path")
|
||||
if err != nil {
|
||||
return fmt.Errorf("get directory path from flags fail: (%w)", err)
|
||||
}
|
||||
_ = dirPath
|
||||
|
||||
op, value, err := parseChange(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lightInstance, err := backlight.NewInstance(dirPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create backlight instance fail: %w", err)
|
||||
}
|
||||
|
||||
cur, err := lightInstance.GetCurrent()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current brightness fail: %w", err)
|
||||
}
|
||||
max, err := lightInstance.GetMax()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get max brightness fail: %w", err)
|
||||
}
|
||||
|
||||
// cale percentage
|
||||
curP := float32(int(float32(cur)/float32(max)*10000)) / 100
|
||||
|
||||
// 1% base
|
||||
oneP := float32(max) / 100
|
||||
|
||||
finalVal := 0
|
||||
|
||||
switch op {
|
||||
case Add:
|
||||
finalVal = int((curP + value) * oneP)
|
||||
if finalVal > max {
|
||||
finalVal = max
|
||||
}
|
||||
case Subtract:
|
||||
finalVal = int((curP - value) * oneP)
|
||||
if finalVal < 0 {
|
||||
finalVal = 0
|
||||
}
|
||||
case Equal:
|
||||
if value > 100 {
|
||||
value = 100
|
||||
} else if value < 0 {
|
||||
value = 0
|
||||
}
|
||||
finalVal = int((value) * oneP)
|
||||
default:
|
||||
return errors.New("operate invalid")
|
||||
}
|
||||
|
||||
err = lightInstance.SetValue(finalVal)
|
||||
if err != nil {
|
||||
return fmt.Errorf("set backlight to target value fail: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package status
|
||||
|
||||
import (
|
||||
"backlight-control/internal/backlight"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCommand(parent *cobra.Command) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "status",
|
||||
Aliases: []string{"st"},
|
||||
RunE: run,
|
||||
Short: "get backlight status (current,max,percentage)",
|
||||
}
|
||||
|
||||
parent.AddCommand(cmd)
|
||||
}
|
||||
|
||||
type backlightStatus struct {
|
||||
Current int `json:"current"`
|
||||
Max int `json:"max"`
|
||||
Percentage float32 `json:"percentage"`
|
||||
}
|
||||
|
||||
func run(cmd *cobra.Command, args []string) (err error) {
|
||||
dirPath, err := cmd.Flags().GetString("path")
|
||||
if err != nil {
|
||||
return fmt.Errorf("get directory path from flags fail: (%w)", err)
|
||||
}
|
||||
|
||||
lightInstance, err := backlight.NewInstance(dirPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create backlight instance fail: %w", err)
|
||||
}
|
||||
|
||||
cur, err := lightInstance.GetCurrent()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get current brightness fail: %w", err)
|
||||
}
|
||||
max, err := lightInstance.GetMax()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get max brightness fail: %w", err)
|
||||
}
|
||||
|
||||
status := backlightStatus{
|
||||
Current: cur,
|
||||
Max: max,
|
||||
}
|
||||
|
||||
// cale percentage
|
||||
f := float32(int(float32(cur)/float32(max)*10000)) / 100
|
||||
|
||||
status.Percentage = f
|
||||
|
||||
b, err := json.Marshal(status)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(b))
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user