forked from wtfutil/wtf
/
multisource_widget.go
99 lines (79 loc) · 2.49 KB
/
multisource_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package view
import (
"github.com/doctorfree/wtf/cfg"
"github.com/doctorfree/wtf/utils"
)
// MultiSourceWidget is a widget that supports displaying data from multiple sources
type MultiSourceWidget struct {
moduleConfig *cfg.Common
singular string
plural string
DisplayFunction func()
Idx int
Sources []string
}
// NewMultiSourceWidget creates and returns an instance of MultiSourceWidget
func NewMultiSourceWidget(moduleConfig *cfg.Common, singular, plural string) MultiSourceWidget {
widget := MultiSourceWidget{
moduleConfig: moduleConfig,
singular: singular,
plural: plural,
}
widget.loadSources()
return widget
}
/* -------------------- Exported Functions -------------------- */
// CurrentSource returns the string representations of the currently-displayed source
func (widget *MultiSourceWidget) CurrentSource() string {
if widget.Idx >= len(widget.Sources) {
return ""
}
return widget.Sources[widget.Idx]
}
// NextSource displays the next source in the source list. If the current source is the last
// source it wraps around to the first source
func (widget *MultiSourceWidget) NextSource() {
widget.Idx++
if widget.Idx == len(widget.Sources) {
widget.Idx = 0
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
// PrevSource displays the previous source in the source list. If the current source is the first
// source, it wraps around to the last source
func (widget *MultiSourceWidget) PrevSource() {
widget.Idx--
if widget.Idx < 0 {
widget.Idx = len(widget.Sources) - 1
}
if widget.DisplayFunction != nil {
widget.DisplayFunction()
}
}
// SetDisplayFunction stores the function that should be called when the source is
// changed. This is typically called from within the initializer for the struct that
// embeds MultiSourceWidget
//
// Example:
//
// widget := Widget{
// MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "person", "people")
// }
//
// widget.SetDisplayFunction(widget.display)
func (widget *MultiSourceWidget) SetDisplayFunction(displayFunc func()) {
widget.DisplayFunction = displayFunc
}
/* -------------------- Unexported Functions -------------------- */
func (widget *MultiSourceWidget) loadSources() {
var empty []interface{}
single := widget.moduleConfig.Config.UString(widget.singular, "")
multiple := widget.moduleConfig.Config.UList(widget.plural, empty)
asStrs := utils.ToStrs(multiple)
if single != "" {
asStrs = append(asStrs, single)
}
widget.Sources = asStrs
}