wmtools/temperature.nim

73 lines
1.7 KiB
Nim
Raw Normal View History

2022-04-28 21:58:40 +02:00
import base
import std/[os,re,math,strutils]
2022-02-06 21:10:06 +01:00
const default_bg = green
const default_fg = black
const alert_bg = yellow
const alert_fg = black
const warning_bg = red
const warning_fg = black
2022-02-06 21:10:06 +01:00
proc getThermalZones(): seq[string] =
var zones: seq[string] = @[]
let dirname = re("thermal_zone[\\d]+")
for file in walkDir("/sys/class/thermal/"):
if contains(file.path,dirname):
let state = readFile(file.path & "/mode")
if contains(state,re"enabled"):
zones.add(file.path)
return zones
proc getTemp(zone: string): int =
let temp = strip(readFile(zone & "/temp"))
return parseInt(temp)
proc getAverageTemp(zones: seq[string]): int =
var temps: int = 0
for zone in zones:
let temp = getTemp(zone)
temps += temp
let avgtemp = ceil((temps / len(zones))/1000)
return toInt(round(avgtemp,0))
2022-04-28 21:58:40 +02:00
proc getObject(temp: int): Info =
2022-02-06 21:10:06 +01:00
var icon = ""
var bg_col = default_bg
var fg_col = default_fg
2022-02-06 21:10:06 +01:00
case temp:
of 40..59:
bg_col = alert_bg
fg_col = alert_fg
2022-02-06 21:10:06 +01:00
icon = ""
of 60.. 200:
bg_col = warning_bg
fg_col = warning_fg
2022-02-06 21:10:06 +01:00
icon = ""
else:
bg_col = default_bg
fg_col = default_fg
2022-02-06 21:10:06 +01:00
icon = ""
let text = "<span foreground='" & bg_col & "'>" & icon & "</span> " & $temp & "°C"
let main_text = icon & " " & $temp & "°C"
var data = newInfo("Temperature")
data.full_text = main_text
data.selected_bg = bg_col
data.selected_fg = fg_col
# i3bar stuff
data.html_text = text
data.color = bg_col
data.border = bg_col
2022-02-06 21:10:06 +01:00
return data
proc main() =
let zones = getThermalZones()
let temp = getAverageTemp(zones)
let data = getObject(temp)
let option = outputData(data)
if option == data.full_text:
# Refresh
main()
2022-02-06 21:10:06 +01:00
if isMainModule:
main()