wmtools/brightness.nim

75 lines
1.7 KiB
Nim
Raw Normal View History

2022-02-06 21:10:06 +01:00
import std/os
import strutils
import std/osproc
import std/math
2022-04-28 21:58:40 +02:00
import base
2022-02-06 21:10:06 +01:00
proc getLimit(): int
let limit = getLimit()
proc getLimit(): int =
let limit = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/max_brightness")))
return limit
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 get_brightness*(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/intel_backlight/actual_brightness")))
let pcnt = (current/limit)*100
if pcnt != last_pcnt:
let text = getDesign(pcnt)
2022-04-28 21:58:40 +02:00
var data = newInfo()
data.title = "Brightness : "
2022-02-27 15:57:40 +01:00
data.full_text = text
data.border = yellow
2022-04-28 21:58:40 +02:00
data.selected_background = yellow
data.selected_color = black
let args = @["up", "down"]
let option = outputJSON(data,args)
2022-05-01 19:09:07 +02:00
if option in args:
case option:
of "up":
discard execCmd("xbacklight -inc 5")
get_brightness(true)
of "down":
discard execCmd("xbacklight -dec 5")
get_brightness(true)
else:
try:
let i = parseInt(option)
discard execCmd("xbacklight -set " & $i)
get_brightness(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() =
get_brightness()
2022-02-06 21:10:06 +01:00
if isMainModule:
main()