2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-05-04 17:17:02 +02:00
|
|
|
import std/[os,strutils,osproc,math]
|
2022-02-06 21:10:06 +01:00
|
|
|
|
2022-05-04 17:17:02 +02: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 =
|
2022-05-04 17:17:02 +02:00
|
|
|
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
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getBrightness*(run_once: bool = false) =
|
2022-02-12 14:20:21 +01:00
|
|
|
var last_pcnt: float = 0
|
|
|
|
while true:
|
2022-05-04 17:17:02 +02:00
|
|
|
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)
|
2022-05-04 17:17:02 +02:00
|
|
|
var data = newInfo("Brightness")
|
2022-02-27 15:57:40 +01:00
|
|
|
data.full_text = text
|
2022-05-04 17:17:02 +02:00
|
|
|
data.selected_bg = default_fg
|
|
|
|
data.selected_fg = default_bg
|
|
|
|
# i3bar stuff
|
|
|
|
data.border = default_fg
|
2022-04-28 16:52:45 +02:00
|
|
|
let args = @["up", "down"]
|
2022-05-04 17:17:02 +02:00
|
|
|
let option = outputData(data,args)
|
2022-05-01 19:09:07 +02:00
|
|
|
if option in args:
|
|
|
|
case option:
|
|
|
|
of "up":
|
2022-05-04 17:17:02 +02:00
|
|
|
let cmd = replace(backlight_up,"%v",default_value)
|
|
|
|
discard execCmd(cmd)
|
|
|
|
getBrightness(true)
|
2022-05-01 19:09:07 +02:00
|
|
|
of "down":
|
2022-05-04 17:17:02 +02:00
|
|
|
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)
|
2022-05-04 17:17:02 +02:00
|
|
|
let cmd = replace(backlight_set,"%v",$i)
|
|
|
|
discard execCmd(cmd)
|
|
|
|
getBrightness(true)
|
2022-05-01 19:09:07 +02:00
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
2022-04-28 16:52:45 +02:00
|
|
|
|
2022-02-12 14:20:21 +01:00
|
|
|
if run_once:
|
|
|
|
break
|
2022-04-28 16:52:45 +02:00
|
|
|
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() =
|
2022-05-04 17:17:02 +02:00
|
|
|
if limit == -1:
|
|
|
|
switchTwmMode()
|
|
|
|
return
|
|
|
|
getBrightness()
|
2022-02-06 21:10:06 +01:00
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|