src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

mlight.go (959B)


      1 package mlight
      2 
      3 import (
      4 	"context"
      5 	"fmt"
      6 	"os/exec"
      7 	"strconv"
      8 	"strings"
      9 )
     10 
     11 const (
     12 	// VCPBrightnessCode is the VCP code for monitor brightness.
     13 	VCPBrightnessCode = "10"
     14 )
     15 
     16 // Block implements the statusbar.Block interface for monitor brightness.
     17 type Block struct{}
     18 
     19 // New returns a new mlight Block.
     20 func New() *Block {
     21 	return &Block{}
     22 }
     23 
     24 // Name returns the name of the block.
     25 func (b *Block) Name() string {
     26 	return "mlight"
     27 }
     28 
     29 // Render displays the current monitor brightness.
     30 func (b *Block) Render(ctx context.Context) (string, error) {
     31 	out, err := exec.CommandContext(
     32 		ctx,
     33 		"ddcutil",
     34 		"--terse",
     35 		"getvcp",
     36 		VCPBrightnessCode,
     37 	).Output()
     38 	if err != nil {
     39 		return "", fmt.Errorf("failed to exec ddcutil: %v", err)
     40 	}
     41 
     42 	fields := strings.Fields(string(out))
     43 	if len(fields) < 4 {
     44 		return " ", nil
     45 	}
     46 
     47 	val, err := strconv.Atoi(fields[3])
     48 	if err != nil {
     49 		return " ", nil
     50 	}
     51 
     52 	return fmt.Sprintf(" %d%%", val), nil
     53 }