summaryrefslogtreecommitdiffstats
path: root/widgets/temp.go
blob: f5632dfde11eabde2fa2a9a83fbe1e290bfb7a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package widgets

import (
	"fmt"
	"image"
	"sort"
	"time"

	"github.com/VictoriaMetrics/metrics"
	ui "github.com/gizak/termui/v3"

	"github.com/xxxserxxx/gotop/v4/devices"
	"github.com/xxxserxxx/gotop/v4/utils"
)

type TempScale rune

const (
	Celsius    TempScale = 'C'
	Fahrenheit           = 'F'
)

type TempWidget struct {
	*ui.Block      // inherits from Block instead of a premade Widget
	updateInterval time.Duration
	Data           map[string]int
	TempThreshold  int
	TempLowColor   ui.Color
	TempHighColor  ui.Color
	TempScale      TempScale
	temps          map[string]float64
}

func NewTempWidget(tempScale TempScale, filter []string) *TempWidget {
	self := &TempWidget{
		Block:          ui.NewBlock(),
		updateInterval: time.Second * 5,
		Data:           make(map[string]int),
		TempThreshold:  80,
		TempScale:      tempScale,
	}
	self.Title = tr.Value("widget.label.temp")
	if len(filter) > 0 {
		for _, t := range filter {
			self.Data[t] = 0
		}
	} else {
		for _, t := range devices.Devices(devices.Temperatures, false) {
			self.Data[t] = 0
		}
	}

	if tempScale == Fahrenheit {
		self.TempThreshold = utils.CelsiusToFahrenheit(self.TempThreshold)
	}

	self.update()

	go func() {
		for range time.NewTicker(self.updateInterval).C {
			self.Lock()
			self.update()
			self.Unlock()
		}
	}()

	return self
}

func (temp *TempWidget) EnableMetric() {
	temp.temps = make(map[string]float64)
	for k, _ := range temp.Data {
		kc := k
		metrics.NewGauge(makeName("temp", k), func() float64 {
			return float64(temp.Data[kc])
		})
	}
}

// Custom Draw method instead of inheriting from a generic Widget.
func (temp *TempWidget) Draw(buf *ui.Buffer) {
	temp.Block.Draw(buf)

	var keys []string
	for key := range temp.Data {
		keys = append(keys, key)
	}
	sort.Strings(keys)

	for y, key := range keys {
		if y+1 > temp.Inner.Dy() {
			break
		}

		var fg ui.Color
		if temp.Data[key] < temp.TempThreshold {
			fg = temp.TempLowColor
		} else {
			fg = temp.TempHighColor
		}

		s := ui.TrimString(key, (temp.Inner.Dx() - 4))
		buf.SetString(s,
			ui.Theme.Default,
			image.Pt(temp.Inner.Min.X, temp.Inner.Min.Y+y),
		)

		temperature := fmt.Sprintf("%3d°%c", temp.Data[key], temp.TempScale)

		buf.SetString(
			temperature,
			ui.NewStyle(fg),
			image.Pt(temp.Inner.Max.X-(len(temperature)-1), temp.Inner.Min.Y+y),
		)
	}
}

func (temp *TempWidget) update() {
	devices.UpdateTemps(temp.Data)
	for name, val := range temp.Data {
		if temp.TempScale == Fahrenheit {
			temp.Data[name] = utils.CelsiusToFahrenheit(val)
		} else {
			temp.Data[name] = val
		}
	}
}