commit 165bdcb09c1124b82c4585b0d8e99e24aea614ce
parent fbe8f2ab494c621aebdcbaaa6b69327baeec5760
Author: dwrz <dwrz@dwrz.net>
Date: Sun, 9 Nov 2025 19:14:03 +0000
Add statusbar mlight package
Diffstat:
2 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/cmd/statusbar/config/config.go b/cmd/statusbar/config/config.go
@@ -15,6 +15,7 @@ import (
"code.dwrz.net/src/pkg/statusbar/light"
"code.dwrz.net/src/pkg/statusbar/memory"
"code.dwrz.net/src/pkg/statusbar/mic"
+ "code.dwrz.net/src/pkg/statusbar/mlight"
"code.dwrz.net/src/pkg/statusbar/power"
"code.dwrz.net/src/pkg/statusbar/temp"
"code.dwrz.net/src/pkg/statusbar/volume"
@@ -115,6 +116,8 @@ func CreateBlock(bconf BlockConfig, cfg *Config) (statusbar.Block, error) {
return eth.New(bconf.Eth.Iface), nil
case "light":
return light.New(), nil
+ case "mlight":
+ return mlight.New(), nil
case "memory":
return memory.New(), nil
case "mic":
diff --git a/pkg/statusbar/mlight/mlight.go b/pkg/statusbar/mlight/mlight.go
@@ -0,0 +1,53 @@
+package mlight
+
+import (
+ "context"
+ "fmt"
+ "os/exec"
+ "strconv"
+ "strings"
+)
+
+const (
+ // VCPBrightnessCode is the VCP code for monitor brightness.
+ VCPBrightnessCode = "10"
+)
+
+// Block implements the statusbar.Block interface for monitor brightness.
+type Block struct{}
+
+// New returns a new mlight Block.
+func New() *Block {
+ return &Block{}
+}
+
+// Name returns the name of the block.
+func (b *Block) Name() string {
+ return "mlight"
+}
+
+// Render displays the current monitor brightness.
+func (b *Block) Render(ctx context.Context) (string, error) {
+ out, err := exec.CommandContext(
+ ctx,
+ "ddcutil",
+ "--terse",
+ "getvcp",
+ VCPBrightnessCode,
+ ).Output()
+ if err != nil {
+ return "", fmt.Errorf("failed to exec ddcutil: %v", err)
+ }
+
+ fields := strings.Fields(string(out))
+ if len(fields) < 2 {
+ return " ", nil
+ }
+
+ val, err := strconv.Atoi(fields[1])
+ if err != nil {
+ return " ", nil
+ }
+
+ return fmt.Sprintf(" %d%%", val), nil
+}