2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-05-04 17:17:02 +02:00
|
|
|
import std/[strutils,os]
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
const battery = "BAT0"
|
|
|
|
const ok_fg = lightgreen
|
|
|
|
const default_fg = white
|
|
|
|
const default_bg = black
|
|
|
|
const warning_fg = black
|
|
|
|
const warning_bg = red
|
|
|
|
const low_bg = black
|
|
|
|
const low_fg = red
|
|
|
|
const alert_fg = black
|
|
|
|
const alert_bg = yellow
|
|
|
|
const med_fg = green
|
|
|
|
const med_bg = black
|
|
|
|
|
|
|
|
|
|
|
|
proc batteryExists(): bool =
|
|
|
|
try:
|
|
|
|
let state = strip(readFile("/sys/class/power_supply/" & battery & "/present"))
|
|
|
|
if state == "1":
|
|
|
|
return true
|
|
|
|
except:
|
|
|
|
echo "Error getting battery : " & getCurrentExceptionMsg()
|
2022-02-12 16:19:43 +01:00
|
|
|
return false
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc isCharging(): bool =
|
|
|
|
try:
|
|
|
|
let state = strip(readFile("/sys/class/power_supply/" & battery & "/status"))
|
|
|
|
if state == "Charging":
|
2022-02-12 16:19:43 +01:00
|
|
|
return true
|
2022-05-04 17:17:02 +02:00
|
|
|
except:
|
|
|
|
echo "Error getting charging status : " & getCurrentExceptionMsg()
|
|
|
|
return false
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getCharge(): int =
|
2022-02-12 16:19:43 +01:00
|
|
|
var charge = 0
|
2022-05-04 17:17:02 +02:00
|
|
|
try:
|
|
|
|
let chg = strip(readFile("/sys/class/power_supply/" & battery & "/capacity"))
|
|
|
|
if chg != "":
|
|
|
|
charge = parseInt(chg)
|
|
|
|
except:
|
|
|
|
echo "Error getting battery level : " & getCurrentExceptionMsg()
|
2022-02-12 16:19:43 +01:00
|
|
|
return charge
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getDesign(charge: int, state: bool): (string, string, string, string, string) =
|
2022-02-12 16:19:43 +01:00
|
|
|
var icon = " "
|
2022-05-04 17:17:02 +02:00
|
|
|
var icon_colour = ok_fg
|
|
|
|
var col = default_fg
|
|
|
|
var bg = default_bg
|
|
|
|
var border = ok_fg
|
|
|
|
if isCharging():
|
2022-02-12 16:19:43 +01:00
|
|
|
icon = " "
|
|
|
|
else:
|
|
|
|
case charge:
|
|
|
|
of 0..5:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = warning_fg
|
|
|
|
col = warning_fg
|
|
|
|
bg = warning_bg
|
2022-02-12 16:19:43 +01:00
|
|
|
of 6..19:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = low_fg
|
|
|
|
border = low_bg
|
|
|
|
bg = default_bg
|
2022-02-12 16:19:43 +01:00
|
|
|
of 20..39:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = alert_fg
|
|
|
|
border = alert_bg
|
2022-02-12 16:19:43 +01:00
|
|
|
icon = " "
|
|
|
|
of 40..59:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = med_fg
|
|
|
|
border= med_fg
|
2022-02-12 16:19:43 +01:00
|
|
|
icon = " "
|
|
|
|
of 60..79:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = med_fg
|
|
|
|
border = med_fg
|
2022-02-12 16:19:43 +01:00
|
|
|
icon = " "
|
|
|
|
of 80..100:
|
2022-05-04 17:17:02 +02:00
|
|
|
icon_colour = ok_fg
|
|
|
|
border = ok_fg
|
2022-02-12 16:19:43 +01:00
|
|
|
icon = " "
|
|
|
|
else:
|
|
|
|
icon = "x "
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
let main_text = icon & " " & $charge & "%"
|
2022-05-04 17:17:02 +02:00
|
|
|
# This next line is here for i3bar purposes
|
|
|
|
let html_text = "<span foreground=\"" & icon_colour & "\">" & icon & "</span>" & $charge & "%"
|
|
|
|
|
|
|
|
return (html_text,main_text, col, bg, border)
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getOutput(charge: int, state: bool): Info =
|
|
|
|
let (html_text,main_text,col,bg_col,highlight_col) = get_design(charge, state)
|
|
|
|
var data = newInfo("Battery")
|
|
|
|
data.full_text = main_text
|
|
|
|
data.selected_bg = highlight_col
|
|
|
|
data.selected_fg = col # may just want `black` here
|
|
|
|
# i3bar stuff
|
|
|
|
data.html_text = html_text
|
|
|
|
data.color = col
|
|
|
|
data.border = highlight_col
|
|
|
|
data.background = bg_col
|
2022-02-12 16:19:43 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getBatteryInfo() =
|
2022-04-28 13:34:09 +02:00
|
|
|
var last_charge = -1
|
2022-02-12 16:19:43 +01:00
|
|
|
var last_state = false
|
|
|
|
while true:
|
2022-05-04 17:17:02 +02:00
|
|
|
let charge = getCharge()
|
|
|
|
let state = isCharging()
|
2022-02-12 16:19:43 +01:00
|
|
|
if charge != last_charge or state != last_state:
|
2022-05-04 17:17:02 +02:00
|
|
|
let data = getoutput(charge, state)
|
|
|
|
outputData(data)
|
2022-02-12 16:19:43 +01:00
|
|
|
last_charge = charge
|
|
|
|
last_state = state
|
2022-04-28 16:52:45 +02:00
|
|
|
if stoploop:
|
|
|
|
break
|
2022-02-12 16:19:43 +01:00
|
|
|
sleep(1000)
|
|
|
|
|
|
|
|
proc main() =
|
2022-05-04 17:17:02 +02:00
|
|
|
if batteryExists():
|
|
|
|
getBatteryInfo()
|
|
|
|
else:
|
|
|
|
switchTwmMode()
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|
2022-02-12 16:19:43 +01:00
|
|
|
|