complete rewrite in golang

This commit is contained in:
Michael 2020-01-28 22:53:48 +00:00
parent 56320bede0
commit b5de960dae
3 changed files with 58 additions and 71 deletions

View File

@ -1,34 +0,0 @@
# brightness
Simple python script to change display brightness on active display using xrandr and xdotool.
## Setup Instructions
### Requirements
* python3
* xrandr
* xdotool
### Setup
```sh
git clone git@0cd.xyz:michael/brightness.git
```
create settings file with your display IDs
```json
{
"display0" : "DP-2",
"display1" : "DP-1"
}
```
run script with
```sh
Usage: ./brightness.py [+|-]
brightness + # increases brightness
brightness - # decreases brightness
```

View File

@ -1,37 +0,0 @@
#!/usr/bin/env python3
import os, sys, string, subprocess, json
def main():
cmd = "xrandr --verbose | grep {} -A 5 | grep Brightness | cut -f 2 -d ' '".format(display())
brightness = float(subprocess.getoutput(cmd))
if sys.argv[1] == "+" and brightness < 1:
brightness += .25
elif sys.argv[1] == "-" and brightness > 0:
brightness -= .25
os.system("xrandr --output {} --brightness {}".format(display(), brightness))
def display():
cmd = "xdotool getmouselocation --shell | head -n -3 | cut -d'=' -f2-"
cmd = subprocess.getoutput(cmd)
#print(getDisplay()['display0']);
if int(cmd) < 2560:
return getDisplay()['display1']
return getDisplay()['display0']
def getDisplay():
root_dir = os.path.dirname(os.path.realpath(__file__))
json_path = os.path.join(root_dir, 'settings.json')
with open(json_path, 'r') as json_data:
settings = json.load(json_data)
json_data.close()
return settings
if len(sys.argv) <= 1:
print("Usage: {} [+|-]".format(sys.argv[0]))
sys.exit(1)
else:
main()

58
main.go Normal file
View File

@ -0,0 +1,58 @@
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"
}
}