wmtools/i3bar_temperature.nim

60 lines
1.3 KiB
Nim
Raw Normal View History

2022-02-06 21:10:06 +01:00
import std/os
import std/re
import std/math
import strutils
import i3bar_base
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): i3barData =
var icon = ""
var colour = foreground
case temp:
of 40..59:
colour = yellow
icon = ""
of 60.. 200:
colour = red
icon = ""
else:
colour = green
icon = ""
let text = "<span foreground='" & colour & "'>" & icon & "</span> " & $temp & "°C"
let data = i3barData(
full_text: text,
color: foreground,
border: colour
)
return data
proc main() =
let zones = getThermalZones()
while true:
let temp = getAverageTemp(zones)
let data = getObject(temp)
outputJSON(data)
sleep(10000)
main()