-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.go
81 lines (63 loc) · 1.27 KB
/
widget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
/**
* My widget wraper(s).
*/
import (
"time"
ui "github.com/gizak/termui"
)
////////////////////////////////////////////
// Utility: Widgets
////////////////////////////////////////////
type CAHWidget interface {
getGridWidget() ui.GridBufferer
update()
resize()
}
type UpdateInterval interface {
getUpdateInterval() time.Duration
getLastUpdated() *time.Time
setLastUpdated(t time.Time)
}
func shouldUpdate(updater UpdateInterval) bool {
now := time.Now()
lastUpdated := updater.getLastUpdated()
if lastUpdated == nil {
// First time, execute it
updater.setLastUpdated(now)
return true
} else {
// Has enough time passed?
elapsed := now.Sub(*lastUpdated)
if elapsed.Nanoseconds() > updater.getUpdateInterval().Nanoseconds() {
updater.setLastUpdated(time.Now())
return true
}
}
return false
}
type TempWidget struct {
widget *ui.Paragraph
}
func NewTempWidget(l string) *TempWidget {
p := ui.NewParagraph(l)
p.Height = 5
p.BorderLabel = l
// Create our widget
w := &TempWidget{
widget: p,
}
// Invoke its functions
w.update()
w.resize()
return w
}
func (w *TempWidget) getGridWidget() ui.GridBufferer {
return w.widget
}
func (w *TempWidget) update() {
// Do nothing
}
func (w *TempWidget) resize() {
// Do nothing
}