wmtools/temperature.nim

72 lines
1.7 KiB
Nim

import base
import std/[os,re,math,strutils]
const default_bg = green
const default_fg = black
const alert_bg = yellow
const alert_fg = black
const warning_bg = red
const warning_fg = black
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))
proc getObject(temp: int): Info =
var icon = ""
var bg_col = default_bg
var fg_col = default_fg
case temp:
of 40..59:
bg_col = alert_bg
fg_col = alert_fg
icon = ""
of 60.. 200:
bg_col = warning_bg
fg_col = warning_fg
icon = ""
else:
bg_col = default_bg
fg_col = default_fg
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
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()
if isMainModule:
main()