diff --git a/src/dispatcher.nim b/src/dispatcher.nim index ef6d149..7254f4e 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -9,6 +9,7 @@ import util/emurrji import util/calendurr import util/remminurr import util/passwurrd +import util/temperaturr proc dispatch*(cfg: Config) = case cfg.run @@ -30,5 +31,7 @@ proc dispatch*(cfg: Config) = remminurr.go() of Passwurrd: passwurrd.go() + of Temperaturr: + temperaturr.go() else: echo "No valid run command given" diff --git a/src/model/tool.nim b/src/model/tool.nim index 8996848..0acb749 100644 --- a/src/model/tool.nim +++ b/src/model/tool.nim @@ -10,4 +10,5 @@ type Emurrji, Calendurr, Remminurr, - Passwurrd + Passwurrd, + Temperaturr diff --git a/src/parser.nim b/src/parser.nim index 0db2fac..0da2994 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -31,6 +31,8 @@ proc parseArgs*() = myConfig.run = Remminurr of "passwurrd", "password", "pw": myConfig.run = Passwurrd + of "temperaturr", "temperature", "temp": + myConfig.run = Temperaturr else: echo p.help quit(1) diff --git a/src/util/temperaturr.nim b/src/util/temperaturr.nim new file mode 100644 index 0000000..75a96d3 --- /dev/null +++ b/src/util/temperaturr.nim @@ -0,0 +1,54 @@ +import os +import re +import math +import strutils + +import ../common +import ../output + +proc getThermalZones(): seq[string] = + var zones: seq[string] = @[] + let dirname_re = re("thermal_zone[\\d]+") + let enabled_re = re("enabled") + for file in walkDir("/sys/class/thermal/"): + if file.path.contains(dirname_re): + let state = readFile(file.path & "/mode") + if state.contains(enabled_re): + 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]): float = + var temps: int = 0 + for zone in zones: + let temp = getTemp(zone) + temps += temp + let avgtemp = ceil((temps / len(zones))/1000) + return round(avgtemp,2) + +proc getObject(temp: float): Info = + var icon = "" + case temp: + of 40..59: + icon = "" + of 60.. 200: + icon = "" + else: + icon = "" + let main_text = icon & " " & $temp & "°C" + var data = newInfo("Temperaturr") + data.full_text = main_text + return data + +proc go*() = + let zones = getThermalZones() + let temp = getAverageTemp(zones) + let data = getObject(temp) + let option = outputData(data) + if option == data.full_text: + # Refresh + go() + diff --git a/src/util/temperaturr/src/temperaturr.nim b/src/util/temperaturr/src/temperaturr.nim deleted file mode 100644 index a3d570b..0000000 --- a/src/util/temperaturr/src/temperaturr.nim +++ /dev/null @@ -1,72 +0,0 @@ -import ../../globurrl -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 = "" & icon & " " & $temp & "°C" - let main_text = icon & " " & $temp & "°C" - var data = newInfo("Temperaturr") - 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() diff --git a/src/util/temperaturr/temperaturr.nimble b/src/util/temperaturr/temperaturr.nimble deleted file mode 100644 index dccea68..0000000 --- a/src/util/temperaturr/temperaturr.nimble +++ /dev/null @@ -1,13 +0,0 @@ -# Package - -version = "0.1.0" -author = "Paul Wilde" -description = "Display temp in dmenu" -license = "GPL-3.0-or-later" -srcDir = "src" -bin = @["temperaturr"] - - -# Dependencies - -requires "nim >= 1.6.6"