xmtop/widgets/temp.go

128 lines
2.5 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package widgets
import (
"fmt"
2019-01-01 08:55:50 +08:00
"image"
2018-03-04 09:05:52 +08:00
"sort"
2018-02-19 15:25:02 +08:00
"time"
"github.com/VictoriaMetrics/metrics"
2019-03-08 15:15:41 +08:00
ui "github.com/gizak/termui/v3"
2019-03-01 08:29:52 +08:00
2020-04-24 03:07:08 +08:00
"github.com/xxxserxxx/gotop/v4/devices"
"github.com/xxxserxxx/gotop/v4/utils"
2019-03-01 08:29:52 +08:00
)
type TempScale rune
2019-03-01 08:29:52 +08:00
const (
Celsius TempScale = 'C'
Fahrenheit = 'F'
2018-02-19 15:25:02 +08:00
)
2019-03-01 08:29:52 +08:00
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
2018-02-19 15:25:02 +08:00
}
func NewTempWidget(tempScale TempScale, filter []string) *TempWidget {
2019-03-01 08:29:52 +08:00
self := &TempWidget{
Block: ui.NewBlock(),
updateInterval: time.Second * 5,
Data: make(map[string]int),
TempThreshold: 80,
TempScale: tempScale,
}
2020-06-19 08:51:01 +08:00
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
}
}
2018-02-19 15:25:02 +08:00
2019-03-01 08:29:52 +08:00
if tempScale == Fahrenheit {
self.TempThreshold = utils.CelsiusToFahrenheit(self.TempThreshold)
}
2018-04-13 11:00:34 +08:00
self.update()
2018-02-19 15:25:02 +08:00
go func() {
2019-03-01 08:29:52 +08:00
for range time.NewTicker(self.updateInterval).C {
self.Lock()
2018-03-28 05:27:23 +08:00
self.update()
self.Unlock()
2018-02-19 15:25:02 +08:00
}
}()
2018-03-28 05:27:23 +08:00
return self
2018-02-19 15:25:02 +08:00
}
2020-04-28 09:33:41 +08:00
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])
})
}
}
2019-03-01 08:29:52 +08:00
// Custom Draw method instead of inheriting from a generic Widget.
2020-04-28 09:33:41 +08:00
func (temp *TempWidget) Draw(buf *ui.Buffer) {
temp.Block.Draw(buf)
2018-03-04 09:05:52 +08:00
var keys []string
2020-04-28 09:33:41 +08:00
for key := range temp.Data {
2018-05-11 11:32:23 +08:00
keys = append(keys, key)
2018-03-04 09:05:52 +08:00
}
sort.Strings(keys)
for y, key := range keys {
2020-04-28 09:33:41 +08:00
if y+1 > temp.Inner.Dy() {
break
}
2019-03-01 08:29:52 +08:00
var fg ui.Color
2020-04-28 09:33:41 +08:00
if temp.Data[key] < temp.TempThreshold {
fg = temp.TempLowColor
2019-03-01 08:29:52 +08:00
} else {
2020-04-28 09:33:41 +08:00
fg = temp.TempHighColor
}
2020-04-28 09:33:41 +08:00
s := ui.TrimString(key, (temp.Inner.Dx() - 4))
2019-01-01 08:55:50 +08:00
buf.SetString(s,
ui.Theme.Default,
2020-04-28 09:33:41 +08:00
image.Pt(temp.Inner.Min.X, temp.Inner.Min.Y+y),
2019-01-01 08:55:50 +08:00
)
2019-03-01 08:29:52 +08:00
2020-04-28 09:33:41 +08:00
temperature := fmt.Sprintf("%3d°%c", temp.Data[key], temp.TempScale)
buf.SetString(
temperature,
ui.NewStyle(fg),
2020-04-28 09:33:41 +08:00
image.Pt(temp.Inner.Max.X-(len(temperature)-1), temp.Inner.Min.Y+y),
)
}
}
2020-04-28 09:33:41 +08:00
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 {
2020-04-28 09:33:41 +08:00
temp.Data[name] = val
}
}
}