2018-02-19 15:25:02 +08:00
|
|
|
package widgets
|
|
|
|
|
2018-02-25 13:35:57 +08:00
|
|
|
// Temp is too customized to inherit from a generic widget so we create a customized one here.
|
|
|
|
// Temp defines its own Buffer method directly.
|
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
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"
|
|
|
|
|
2019-01-01 08:55:50 +08:00
|
|
|
ui "github.com/gizak/termui"
|
2018-02-19 15:25:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Temp struct {
|
2018-02-25 13:35:57 +08:00
|
|
|
*ui.Block
|
2018-11-14 01:29:09 +08:00
|
|
|
interval time.Duration
|
|
|
|
Data map[string]int
|
|
|
|
Threshold int
|
2019-01-01 08:55:50 +08:00
|
|
|
TempLow ui.Attribute
|
|
|
|
TempHigh ui.Attribute
|
2018-11-14 01:29:09 +08:00
|
|
|
Fahrenheit bool
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
|
|
|
|
2018-11-14 01:29:09 +08:00
|
|
|
func NewTemp(fahrenheit bool) *Temp {
|
2018-03-28 05:27:23 +08:00
|
|
|
self := &Temp{
|
2018-02-25 13:35:57 +08:00
|
|
|
Block: ui.NewBlock(),
|
|
|
|
interval: time.Second * 5,
|
2018-03-04 09:05:52 +08:00
|
|
|
Data: make(map[string]int),
|
|
|
|
Threshold: 80, // temp at which color should change
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
self.Title = " Temperatures "
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-14 01:29:09 +08:00
|
|
|
if fahrenheit {
|
|
|
|
self.Fahrenheit = true
|
|
|
|
self.Threshold = int(self.Threshold*9/5 + 32)
|
|
|
|
}
|
|
|
|
|
2018-04-13 11:00:34 +08:00
|
|
|
self.update()
|
|
|
|
|
2018-02-19 15:25:02 +08:00
|
|
|
go func() {
|
2018-11-30 10:17:13 +08:00
|
|
|
ticker := time.NewTicker(self.interval)
|
2018-02-19 15:25:02 +08:00
|
|
|
for range ticker.C {
|
2018-03-28 05:27:23 +08:00
|
|
|
self.update()
|
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
|
|
|
}
|
|
|
|
|
2019-01-01 08:55:50 +08:00
|
|
|
func (self *Temp) Draw(buf *ui.Buffer) {
|
|
|
|
self.Block.Draw(buf)
|
2018-02-25 13:35:57 +08:00
|
|
|
|
2018-03-04 09:05:52 +08:00
|
|
|
var keys []string
|
2018-05-11 11:32:23 +08:00
|
|
|
for key := range self.Data {
|
|
|
|
keys = append(keys, key)
|
2018-03-04 09:05:52 +08:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for y, key := range keys {
|
2019-01-01 08:55:50 +08:00
|
|
|
if y+1 > self.Inner.Dy() {
|
2018-02-25 13:35:57 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-03-28 05:27:23 +08:00
|
|
|
fg := self.TempLow
|
|
|
|
if self.Data[key] >= self.Threshold {
|
|
|
|
fg = self.TempHigh
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:55:50 +08:00
|
|
|
s := ui.TrimString(key, (self.Inner.Dx() - 4))
|
|
|
|
buf.SetString(s,
|
|
|
|
image.Pt(self.Inner.Min.X, self.Inner.Min.Y+y),
|
|
|
|
ui.Theme.Default,
|
|
|
|
)
|
2018-11-14 01:29:09 +08:00
|
|
|
if self.Fahrenheit {
|
2019-01-01 08:55:50 +08:00
|
|
|
buf.SetString(
|
|
|
|
fmt.Sprintf("%3dF", self.Data[key]),
|
|
|
|
image.Pt(self.Inner.Dx()-3, y+1),
|
|
|
|
ui.AttrPair{fg, -1},
|
|
|
|
)
|
2018-11-14 01:29:09 +08:00
|
|
|
} else {
|
2019-01-01 08:55:50 +08:00
|
|
|
buf.SetString(
|
|
|
|
fmt.Sprintf("%3dC", self.Data[key]),
|
|
|
|
image.Pt(self.Inner.Max.X-4, self.Inner.Min.Y+y),
|
|
|
|
ui.AttrPair{fg, -1},
|
|
|
|
)
|
2018-11-14 01:29:09 +08:00
|
|
|
}
|
2018-02-25 13:35:57 +08:00
|
|
|
}
|
|
|
|
}
|