xmtop/widgets/temp.go

128 lines
2.5 KiB
Go
Raw Normal View History

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