From ee80320dc3bb5cf4efc2442616109eb39b9b2587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kintzi?= Date: Sun, 12 Nov 2023 12:42:30 +0100 Subject: [PATCH] Add Temperature widget --- README.md | 17 ++++++++- widgets/temp.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++ widgets/window.go | 5 +-- 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 widgets/temp.go diff --git a/README.md b/README.md index 82086af..6c0349b 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,14 @@ for further customization: ``` - widget: Window excludeAppIDs: ["foot"] + titlePrefix: "\uf2a3 " + ellipsis: "\u2026" - widget: ExternalProgram command: /usr/bin/i3status - widget: SystemMonitor format: "\ue8b0 {cpu}% \ue8b2 {mem}% \ue8b3 {load}%" +- widget: TempMonitor + format: "\ue8a2 {temp}\u00B0C \ue8a1 {fan}" - widget: Volume scrollDownCommand: volumeup scrollUpCommand: volumedown @@ -150,6 +154,17 @@ necessary, and will display two digits for the fractional part. Please note that the width and precision specification should be followed by the 'f' suffix, but no '%' prefix should be included. +## TemperatureMonitor + +The TemperatureMonitor widget displays temperature and/or fan speed. It offers +the following settings: + +- `format`: Determines how the widget is displayed. The default + format is `{temp:%d} {fan:%d}`. +- `tempInput`: Specifies the file that keeps the current temperature. +- `fanInput`: Specifies the file that keeps the current fan speed. + + ### Volume The Volume widget shows and allows you to adjust the volume of an audio @@ -192,7 +207,7 @@ following options: it is trimmed and an ellipsis symbol is added (default: 80, set to -1 to disable trimming) - `ellipsis` - the symbol used as an ellipsis after trimming the window's - title (default: "\u2026") + title (default: "...") - `excludeAppIDs` - to make the title appear more standardized, the Window widget first removes the app ID from the title (because different applications place them in different locations) and then appends it back. diff --git a/widgets/temp.go b/widgets/temp.go new file mode 100644 index 0000000..a57cfe2 --- /dev/null +++ b/widgets/temp.go @@ -0,0 +1,95 @@ +package widgets + +import ( + "context" + "io" + "os" + "strconv" + "strings" + "time" + + "gobytes.dev/statusbar" + "gobytes.dev/statusbar/internal/registry" +) + +type TemperatureMonitor struct { + Format string `yaml:"format"` + TempInput string `yaml:"tempInput"` + FanInput string `yaml:"fanInput"` + + format Format + status []statusbar.Block + baseWidget +} + +func (w *TemperatureMonitor) WidgetName() string { return "TempMonitor" } +func (w *TemperatureMonitor) New() statusbar.Widget { return new(TemperatureMonitor) } + +func (w *TemperatureMonitor) Run(ctx context.Context, update statusbar.UpdateFunc, done func()) { + defer done() + w.init() + w.status = make([]statusbar.Block, 0, 2) + w.emitStatus(update) + t := time.NewTicker(1 * time.Second) + for { + select { + case <-ctx.Done(): + return + case <-t.C: + w.emitStatus(update) + } + } +} + +func (w *TemperatureMonitor) readInput(input string) (int, error) { + f, err := os.Open(input) + if err != nil { + return 0, err + } + defer f.Close() + bs, err := io.ReadAll(f) + if err != nil { + return 0, err + } + return strconv.Atoi(strings.TrimSpace(string(bs))) +} + +func (w *TemperatureMonitor) emitStatus(update statusbar.UpdateFunc) { + w.status = w.status[:0] + temp, err := w.readInput(w.TempInput) + if err != nil { + w.errorf(err.Error()) + temp = 0 + } + fan, err := w.readInput(w.FanInput) + if err != nil { + w.errorf(err.Error()) + fan = 0 + } + w.status = append(w.status, statusbar.Block{ + FullText: w.format.Format(temp/1000, fan), + Align: "left", + Name: "batmon", + Instance: "0", + Markup: "pango", + }) + update(w.status) +} + +func (w *TemperatureMonitor) init() { + w.prefix = "TempMonitor" + if w.Format == "" { + w.Format = "{temp:%d} {fan:%d}" + } + w.format = ParseFormat(w.Format, "temp:%d", "fan:%d") + if w.TempInput == "" { + w.TempInput = "/sys/class/hwmon/hwmon3/temp1_input" + } + if w.FanInput == "" { + w.FanInput = "/sys/class/hwmon/hwmon3/fan1_input" + } +} + +func init() { + registry.RegisterWidget(new(TemperatureMonitor)) +} diff --git a/widgets/window.go b/widgets/window.go index 3130613..1e2d1e5 100644 --- a/widgets/window.go +++ b/widgets/window.go @@ -213,10 +213,7 @@ func (w *Window) init() { w.MaxTitleLen = 80 } if w.Ellipsis == "" { - w.Ellipsis = "\u2026" - } - if w.TitlePrefix == "" { - w.TitlePrefix = "\uf2a3 " + w.Ellipsis = "..." } } -- 2.45.2