2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-05-04 17:17:02 +02:00
|
|
|
import std/[os,re,math,strutils]
|
2022-02-06 21:10:06 +01:00
|
|
|
|
2022-05-04 17:17:02 +02: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 = ""
|
2022-05-04 17:17:02 +02:00
|
|
|
var bg_col = default_bg
|
|
|
|
var fg_col = default_fg
|
2022-02-06 21:10:06 +01:00
|
|
|
case temp:
|
|
|
|
of 40..59:
|
2022-05-04 17:17:02 +02:00
|
|
|
bg_col = alert_bg
|
|
|
|
fg_col = alert_fg
|
2022-02-06 21:10:06 +01:00
|
|
|
icon = ""
|
|
|
|
of 60.. 200:
|
2022-05-04 17:17:02 +02:00
|
|
|
bg_col = warning_bg
|
|
|
|
fg_col = warning_fg
|
2022-02-06 21:10:06 +01:00
|
|
|
icon = ""
|
|
|
|
else:
|
2022-05-04 17:17:02 +02:00
|
|
|
bg_col = default_bg
|
|
|
|
fg_col = default_fg
|
2022-02-06 21:10:06 +01:00
|
|
|
icon = ""
|
2022-05-04 17:17:02 +02:00
|
|
|
let text = "<span foreground='" & bg_col & "'>" & icon & "</span> " & $temp & "°C"
|
2022-04-28 16:52:45 +02:00
|
|
|
let main_text = icon & " " & $temp & "°C"
|
2022-05-04 17:17:02 +02:00
|
|
|
var data = newInfo("Temperature")
|
2022-04-28 16:52:45 +02:00
|
|
|
data.full_text = main_text
|
2022-05-04 17:17:02 +02:00
|
|
|
data.selected_bg = bg_col
|
|
|
|
data.selected_fg = fg_col
|
|
|
|
# i3bar stuff
|
2022-04-28 16:52:45 +02:00
|
|
|
data.html_text = text
|
2022-05-04 17:17:02 +02:00
|
|
|
data.color = bg_col
|
|
|
|
data.border = bg_col
|
2022-02-06 21:10:06 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
proc main() =
|
|
|
|
let zones = getThermalZones()
|
2022-05-04 17:17:02 +02:00
|
|
|
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
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|