brightness/main.go

59 lines
1.1 KiB
Go

package main
import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
func main() {
if len(os.Args) <= 1 {
fmt.Printf("Usage: %s [+|-]", os.Args[0])
return
}
brightness()
}
func brightness() {
cmd := fmt.Sprintf("xrandr --verbose | grep %s -A 5 | grep Brightness | cut -f 2 -d ' '", display())
brightness, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
log.Fatal(err)
}
f, err := strconv.ParseFloat(strings.TrimSpace(string(brightness)), 64)
if err != nil {
log.Fatal(err)
}
switch {
case os.Args[1] == "+" && f < 1:
f += .25
case os.Args[1] == "-" && f > 0:
f -= .25
}
exe := fmt.Sprintf("xrandr --output %s --brightness %s", display(), strconv.FormatFloat(f, 'f', 2, 64))
exec.Command("sh", "-c", exe).Output()
}
func display() string {
cmd, err := exec.Command("xdotool", "getmouselocation", "--shell").CombinedOutput()
if err != nil {
log.Fatal(err)
}
lines := strings.SplitAfter(string(cmd), "\n")
i, err := strconv.Atoi(strings.TrimSpace(lines[0][len(lines)-3:]))
if err != nil {
log.Fatal(err)
}
switch {
case i < 1920:
return "HDMI-1"
case i < 4480:
return "DP-1"
default:
return "DP-2"
}
}