2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2018-02-25 13:35:57 +08:00
|
|
|
"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"
|
|
|
|
|
2020-05-13 07:42:45 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2020-01-24 06:17:14 +08:00
|
|
|
type TempScale rune
|
2019-03-01 08:29:52 +08:00
|
|
|
|
|
|
|
const (
|
2020-02-26 04:26:52 +08:00
|
|
|
Celsius TempScale = 'C'
|
2020-01-24 06:17:14 +08:00
|
|
|
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
|
2020-05-13 07:42:45 +08:00
|
|
|
temps map[string]float64
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2020-04-23 23:41:20 +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,
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
2020-06-19 08:51:01 +08:00
|
|
|
self.Title = tr.Value("widget.label.temp")
|
2020-04-23 23:41:20 +08:00
|
|
|
if len(filter) > 0 {
|
|
|
|
for _, t := range filter {
|
|
|
|
self.Data[t] = 0
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-24 02:01:13 +08:00
|
|
|
for _, t := range devices.Devices(devices.Temperatures, false) {
|
2020-04-23 23:41:20 +08:00
|
|
|
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-11-14 01:29:09 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-03-08 15:43:10 +08:00
|
|
|
self.Lock()
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
2019-03-08 15:43:10 +08:00
|
|
|
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() {
|
2020-05-13 07:42:45 +08:00
|
|
|
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])
|
2020-02-18 23:44:29 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-25 13:35:57 +08:00
|
|
|
|
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() {
|
2018-02-25 13:35:57 +08:00
|
|
|
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
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2020-01-24 06:17:14 +08:00
|
|
|
|
|
|
|
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-01-24 06:17:14 +08:00
|
|
|
)
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-28 08:51:28 +08:00
|
|
|
|
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)
|
2020-02-28 08:51:28 +08:00
|
|
|
} else {
|
2020-04-28 09:33:41 +08:00
|
|
|
temp.Data[name] = val
|
2020-02-28 08:51:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|