116 lines
2.8 KiB
Nim
116 lines
2.8 KiB
Nim
import base
|
|
import std/[os,strutils,osproc,math]
|
|
|
|
const backlight = "intel_backlight"
|
|
const default_bg = yellow
|
|
const default_fg = black
|
|
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 default_value = "5"
|
|
|
|
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()
|
|
|
|
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 brightnessUp() =
|
|
let cmd = replace(UP,"%v",default_value)
|
|
discard execCmd(cmd)
|
|
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("Brightness")
|
|
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)
|
|
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:
|
|
for arg in args:
|
|
case arg:
|
|
of "-w":
|
|
CMD = BACKLIGHT_CMD_WL
|
|
UP = UP_WL
|
|
DOWN = DOWN_WL
|
|
SET = SET_WL
|
|
of "up":
|
|
brightnessUp()
|
|
break start
|
|
of "down":
|
|
brightnessDown()
|
|
break start
|
|
|
|
main()
|