added temperaturr

This commit is contained in:
Paul Wilde 2023-11-23 10:13:59 +00:00
parent 1ff912edff
commit 3074f55973
6 changed files with 61 additions and 86 deletions

View file

@ -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"

View file

@ -10,4 +10,5 @@ type
Emurrji,
Calendurr,
Remminurr,
Passwurrd
Passwurrd,
Temperaturr

View file

@ -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)

54
src/util/temperaturr.nim Normal file
View file

@ -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()

View file

@ -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 = "<span foreground='" & bg_col & "'>" & icon & "</span> " & $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()

View file

@ -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"