From 528cbb6200eaa6226282d805946e9f675d17d9a7 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 23 Nov 2023 12:49:06 +0000 Subject: [PATCH] added brightnurrs - needs testing on laptop --- src/dispatcher.nim | 3 + src/model/brightness.nim | 6 ++ src/model/tool.nim | 3 +- src/parser.nim | 26 +++++++ src/util/brightnurrs.nim | 143 +++++++++++++++++---------------------- 5 files changed, 99 insertions(+), 82 deletions(-) create mode 100644 src/model/brightness.nim diff --git a/src/dispatcher.nim b/src/dispatcher.nim index 3435421..530038b 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -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" diff --git a/src/model/brightness.nim b/src/model/brightness.nim new file mode 100644 index 0000000..e42445a --- /dev/null +++ b/src/model/brightness.nim @@ -0,0 +1,6 @@ + +type + BrightnessArg* = enum + None, + BrightUp, + BrightDown diff --git a/src/model/tool.nim b/src/model/tool.nim index a879016..7cb0002 100644 --- a/src/model/tool.nim +++ b/src/model/tool.nim @@ -14,4 +14,5 @@ type PasswurrdGeneraturr, Temperaturr, Screenshurrt, - Calculaturr + Calculaturr, + Brightnurrs diff --git a/src/parser.nim b/src/parser.nim index f3caec8..6f21bc0 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -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() diff --git a/src/util/brightnurrs.nim b/src/util/brightnurrs.nim index 8fb4393..df15760 100644 --- a/src/util/brightnurrs.nim +++ b/src/util/brightnurrs.nim @@ -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"