wmtools/i3bar_temperature.nim

66 lines
1.5 KiB
Nim

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 main_text = icon & " " & $temp & "°C"
var data = newi3barData()
data.title = "Temp : "
data.full_text = main_text
data.html_text = text
data.color = foreground
data.border = colour
return data
proc main() =
let zones = getThermalZones()
var last_temp = 0
while true:
let temp = getAverageTemp(zones)
if temp != last_temp:
let data = getObject(temp)
outputJSON(data)
last_temp = temp
if stoploop:
break
sleep(10000)
main()