wmtools/brightness.nim

89 lines
2.3 KiB
Nim
Raw Normal View History

2022-04-28 21:58:40 +02:00
import base
import std/[os,strutils,osproc,math]
2022-02-06 21:10:06 +01:00
const backlight = "intel_backlight"
const default_bg = yellow
const default_fg = black
const backlight_cmd = "xbacklight"
const backlight_up = backlight_cmd & " -inc %v" # %v is amount by
const backlight_down = backlight_cmd & " -dec %v" # %v is amount by
const backlight_set = backlight_cmd & " -set %v" # %v is amount by
const default_value = "5"
2022-02-06 21:10:06 +01:00
proc getLimit(): int =
try:
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()
2022-02-06 21:10:06 +01:00
proc getDesign(pcnt: float): string =
var icon = "🌑"
case pcnt:
of 85..100:
icon = "🌕"
of 75..85:
icon = "🌖"
of 50..75:
icon = "🌗"
of 35..50:
icon = "🌘"
else:
icon = "🌑"
let percent = toInt(round(pcnt,0))
let text = icon & " " & $percent & "%"
return text
proc getBrightness*(run_once: bool = false) =
2022-02-12 14:20:21 +01:00
var last_pcnt: float = 0
while true:
let current = parseInt(strip(readFile("/sys/class/backlight/" & backlight & "actual_brightness")))
2022-02-12 14:20:21 +01:00
let pcnt = (current/limit)*100
if pcnt != last_pcnt:
let text = getDesign(pcnt)
var data = newInfo("Brightness")
2022-02-27 15:57:40 +01:00
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)
2022-05-01 19:09:07 +02:00
if option in args:
case option:
of "up":
let cmd = replace(backlight_up,"%v",default_value)
discard execCmd(cmd)
getBrightness(true)
2022-05-01 19:09:07 +02:00
of "down":
let cmd = replace(backlight_down,"%v",default_value)
discard execCmd(cmd)
getBrightness(true)
2022-05-01 19:09:07 +02:00
else:
try:
let i = parseInt(option)
let cmd = replace(backlight_set,"%v",$i)
discard execCmd(cmd)
getBrightness(true)
2022-05-01 19:09:07 +02:00
except:
echo getCurrentExceptionMsg()
2022-02-12 14:20:21 +01:00
if run_once:
break
if stoploop:
break
2022-02-12 14:20:21 +01:00
last_pcnt = pcnt
sleep(1000)
2022-02-06 21:10:06 +01:00
proc main() =
if limit == -1:
switchTwmMode()
return
getBrightness()
2022-02-06 21:10:06 +01:00
if isMainModule:
main()