wmtools/i3bar_brightness.nim

73 lines
1.5 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
import i3bar_base
2022-02-12 14:20:21 +01:00
import std/threadpool
2022-02-06 21:10:06 +01:00
proc getLimit(): int
let limit = getLimit()
let args = getArguments()
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
2022-02-12 14:20:21 +01:00
proc get_brightness(run_once: bool = false) =
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)
let data = i3barData(
full_text: text,
color: foreground,
border: yellow
)
outputJSON(data)
if run_once:
break
last_pcnt = pcnt
sleep(1000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 1,4:
let state = execCmd("xbacklight -inc 5")
get_brightness(true)
of 3,5:
let state = execCmd("xbacklight -dec 5")
get_brightness(true)
else:
let no = false
clearInput(2)
2022-02-06 21:10:06 +01:00
proc main() =
2022-02-12 14:20:21 +01:00
spawn get_brightness()
spawn await_click_info()
sync()
2022-02-06 21:10:06 +01:00
main()