added brightnurrs - needs testing on laptop

This commit is contained in:
Paul Wilde 2023-11-23 12:49:06 +00:00
parent ff21191f2d
commit 528cbb6200
5 changed files with 99 additions and 82 deletions

View file

@ -13,6 +13,7 @@ import util/pw_generaturr
import util/temperaturr
import util/screenshurrt
import util/calculaturr
import util/brightnurrs
proc dispatch*(cfg: Config) =
case cfg.run
@ -42,5 +43,7 @@ proc dispatch*(cfg: Config) =
screenshurrt.go()
of Calculaturr:
calculaturr.go()
of Brightnurrs:
brightnurrs.go()
else:
echo "No valid run command given"

6
src/model/brightness.nim Normal file
View file

@ -0,0 +1,6 @@
type
BrightnessArg* = enum
None,
BrightUp,
BrightDown

View file

@ -14,4 +14,5 @@ type
PasswurrdGeneraturr,
Temperaturr,
Screenshurrt,
Calculaturr
Calculaturr,
Brightnurrs

View file

@ -5,6 +5,7 @@ import argparse
import common
import model/pwgen
import model/volume
import model/brightness
import model/screenshot
proc parseArgs*() =
@ -42,6 +43,8 @@ proc parseArgs*() =
myConfig.run = Screenshurrt
of "calculaturr", "calculator", "calc":
myConfig.run = Calculaturr
of "brightnurrs", "brightness", "bright":
myConfig.run = Brightnurrs
else:
echo p.help
quit(1)
@ -78,6 +81,29 @@ proc parseVolArgs*(): VolArg =
quit(1)
return arg
proc parseBrightnessArgs*(): BrightnessArg =
var arg: BrightnessArg
let params = commandLineParams()
var p = newParser:
help("Args for volurrme")
arg("brightnurrs",help="can only ever be 'brightnurrs' as you won't have gotten this far otherwise")
arg("adjust",help="up, down, or mute",default=some(""))
try:
var opts = p.parse(params)
case opts.adjust
of "up":
arg = BrightUp
of "down":
arg = BrightDown
except ShortCircuit as err:
if err.flag == "argparse_help":
echo err.help
quit(1)
except UsageError:
stderr.writeLine getCurrentExceptionMsg()
quit(1)
return arg
proc parsePWGenArgs*(): PWGen =
var gen = newPWGen()
let params = commandLineParams()

View file

@ -1,32 +1,36 @@
import ../../globurrl
import std/[os,strutils,osproc,math]
import os
import strutils
import osproc
import math
const backlight = "intel_backlight"
const default_bg = yellow
const default_fg = black
import ../common
import ../parser
import ../model/brightness
import ../output
#const backlight = "intel_backlight"
const BACKLIGHT_CMD = "xbacklight"
const UP_X = BACKLIGHT_CMD & " -inc %v" # %v is amount by
const DOWN_X = BACKLIGHT_CMD & " -dec %v" # %v is amount by
const SET_X = BACKLIGHT_CMD & " -set %v" # %v is amount by
const BACKLIGHT_CMD_WL = "brightnessctl"
const UP_WL = BACKLIGHT_CMD_WL & " set %v%+"
const DOWN_WL = BACKLIGHT_CMD_WL & " set %v%-"
const SET_WL = BACKLIGHT_CMD_WL & " set %v%"
var CMD = BACKLIGHT_CMD
var UP = UP_X
var DOWN = DOWN_X
var SET = SET_X
const UP = BACKLIGHT_CMD & " -inc %v" # %v is amount by
const DOWN = BACKLIGHT_CMD & " -dec %v" # %v is amount by
const SET = BACKLIGHT_CMD & " -set %v" # %v is amount by
const default_value = "5"
proc getLimit(): int =
proc getBacklight(): string =
for dir in walkDir("/sys/class/backlight"):
echo dir.path
var bl = dir.path.replace("/sys/class/backlight/","")
echo bl
return bl
proc getLimit(backlight: string): int =
try:
let back_l = readFile("/sys/class/backlight/" & backlight & "/max_brightness")
return parseInt(strip(back_l))
if backlight != "":
let back_l = readFile("/sys/class/backlight/" & backlight & "/max_brightness")
return parseInt(strip(back_l))
except:
echo "Error getting backlight max : ", getCurrentExceptionMsg()
return -1
let limit = getLimit()
proc getDesign(pcnt: float): string =
var icon = "🌑"
@ -52,65 +56,42 @@ proc brightnessDown() =
let cmd = replace(DOWN,"%v",default_value)
discard execCmd(cmd)
proc getBrightness*(run_once: bool = false) =
var last_pcnt: float = 0
while true:
let current = parseInt(strip(readFile("/sys/class/backlight/" & backlight & "/actual_brightness")))
let pcnt = (current/limit)*100
if pcnt != last_pcnt:
let text = getDesign(pcnt)
var data = newInfo("Brightnurrs")
data.full_text = text
data.selected_bg = default_fg
data.selected_fg = default_bg
# i3bar stuff
data.border = default_fg
let args = @["up", "down"]
let option = outputData(data,args)
if option in args:
case option:
of "up":
brightnessUp()
getBrightness(true)
of "down":
brightnessDown()
getBrightness(true)
proc getBrightness*(backlight: string) =
let limit = getLimit(backlight)
let current = parseInt(strip(readFile("/sys/class/backlight/" & backlight & "/actual_brightness")))
let pcnt = (current/limit)*100
let text = getDesign(pcnt)
var data = newInfo("Brightnurrs")
data.full_text = text
let args = @["up", "down"]
let option = outputData(data,args)
if option in args:
case option:
of "up":
brightnessUp()
backlight.getBrightness()
of "down":
brightnessDown()
backlight.getBrightness()
else:
try:
let i = parseInt(option)
let cmd = replace(SET,"%v",$i)
discard execCmd(cmd)
backlight.getBrightness()
except:
echo getCurrentExceptionMsg()
proc go*() =
let backlight = getBacklight()
if backlight != "":
let barg = parseBrightnessArgs()
case barg:
of BrightUp:
brightnessUp()
of BrightDown:
brightnessDown()
else:
try:
let i = parseInt(option)
let cmd = replace(SET,"%v",$i)
discard execCmd(cmd)
getBrightness(true)
except:
echo getCurrentExceptionMsg()
if run_once:
break
if stoploop:
break
last_pcnt = pcnt
sleep(1000)
proc main() =
if limit == -1:
switchTwmMode()
return
getBrightness()
if isMainModule:
block start:
if wayland:
CMD = BACKLIGHT_CMD_WL
UP = UP_WL
DOWN = DOWN_WL
SET = SET_WL
for arg in args:
case arg:
of "up":
brightnessUp()
break start
of "down":
brightnessDown()
break start
main()
backlight.getBrightness()
else:
echo "No Backlight Found"