62 lines
1.4 KiB
Nim
Executable file
62 lines
1.4 KiB
Nim
Executable file
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()
|
|
var last_temp = 0
|
|
while true:
|
|
let temp = getAverageTemp(zones)
|
|
if temp != last_temp:
|
|
let data = getObject(temp)
|
|
outputJSON(data)
|
|
last_temp = temp
|
|
sleep(10000)
|
|
|
|
main()
|