summaryrefslogtreecommitdiffstats
path: root/widgets/temp.go
diff options
context:
space:
mode:
authorSean E. Russell <seanerussell@gmail.com>2020-02-13 10:15:52 -0600
committerSean E. Russell <seanerussell@gmail.com>2020-02-13 10:15:52 -0600
commit7e5c0c969c223973335c6fae5432411afc3fb060 (patch)
tree7f785ba4f692b81806209cb5f0c959e86efdba70 /widgets/temp.go
parent4bfe0251a8893ed08654d59b8a8b8182958e907f (diff)
Fixes the directory structure.
Diffstat (limited to 'widgets/temp.go')
-rw-r--r--widgets/temp.go102
1 files changed, 102 insertions, 0 deletions
diff --git a/widgets/temp.go b/widgets/temp.go
new file mode 100644
index 0000000..dea5430
--- /dev/null
+++ b/widgets/temp.go
@@ -0,0 +1,102 @@
+package widgets
+
+import (
+ "fmt"
+ "image"
+ "sort"
+ "time"
+
+ ui "github.com/gizak/termui/v3"
+
+ "github.com/cjbassi/gotop/utils"
+)
+
+type TempScale int
+
+const (
+ Celcius TempScale = 0
+ Fahrenheit = 1
+ Disabled = 2
+)
+
+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
+}
+
+func NewTempWidget(tempScale TempScale) *TempWidget {
+ self := &TempWidget{
+ Block: ui.NewBlock(),
+ updateInterval: time.Second * 5,
+ Data: make(map[string]int),
+ TempThreshold: 80,
+ TempScale: tempScale,
+ }
+ self.Title = " Temperatures "
+
+ 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
+}
+
+// Custom Draw method instead of inheriting from a generic Widget.
+func (self *TempWidget) Draw(buf *ui.Buffer) {
+ self.Block.Draw(buf)
+
+ var keys []string
+ for key := range self.Data {
+ keys = append(keys, key)
+ }
+ sort.Strings(keys)
+
+ for y, key := range keys {
+ if y+1 > self.Inner.Dy() {
+ break
+ }
+
+ var fg ui.Color
+ if self.Data[key] < self.TempThreshold {
+ fg = self.TempLowColor
+ } else {
+ fg = self.TempHighColor
+ }
+
+ s := ui.TrimString(key, (self.Inner.Dx() - 4))
+ buf.SetString(s,
+ ui.Theme.Default,
+ image.Pt(self.Inner.Min.X, self.Inner.Min.Y+y),
+ )
+
+ switch self.TempScale {
+ case Fahrenheit:
+ buf.SetString(
+ fmt.Sprintf("%3dF", self.Data[key]),
+ ui.NewStyle(fg),
+ image.Pt(self.Inner.Max.X-4, self.Inner.Min.Y+y),
+ )
+ case Celcius:
+ buf.SetString(
+ fmt.Sprintf("%3dC", self.Data[key]),
+ ui.NewStyle(fg),
+ image.Pt(self.Inner.Max.X-4, self.Inner.Min.Y+y),
+ )
+ }
+ }
+}