first version
This commit is contained in:
@@ -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