~rkintzi/statusbar

ee80320dc3bb5cf4efc2442616109eb39b9b2587 — RadosÅ‚aw Kintzi 10 months ago 9a0ebfd
Add Temperature widget
3 files changed, 112 insertions(+), 5 deletions(-)

M README.md
A widgets/temp.go
M widgets/window.go
M README.md => README.md +16 -1
@@ 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.

A widgets/temp.go => widgets/temp.go +95 -0
@@ 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))
}

M widgets/window.go => widgets/window.go +1 -4
@@ 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 = "..."
	}
}