-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
text_widget.go
150 lines (116 loc) · 3.59 KB
/
text_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package wtf
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
)
type TextWidget struct {
bordered bool
commonSettings *cfg.Common
enabled bool
focusable bool
focusChar string
name string
refreshing bool
refreshInterval int
app *tview.Application
View *tview.TextView
}
func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable bool) TextWidget {
widget := TextWidget{
commonSettings: commonSettings,
app: app,
bordered: commonSettings.Bordered,
enabled: commonSettings.Enabled,
focusable: focusable,
focusChar: commonSettings.FocusChar(),
name: commonSettings.Name,
refreshing: false,
refreshInterval: commonSettings.RefreshInterval,
}
widget.View = widget.addView()
widget.View.SetBorder(widget.bordered)
return widget
}
/* -------------------- Exported Functions -------------------- */
// Bordered returns whether or not this widget should be drawn with a border
func (widget *TextWidget) Bordered() bool {
return widget.bordered
}
func (widget *TextWidget) BorderColor() string {
if widget.Focusable() {
return widget.commonSettings.Colors.BorderFocusable
}
return widget.commonSettings.Colors.BorderNormal
}
func (widget *TextWidget) CommonSettings() *cfg.Common {
return widget.commonSettings
}
func (widget *TextWidget) ConfigText() string {
return utils.HelpFromInterface(cfg.Common{})
}
func (widget *TextWidget) ContextualTitle(defaultStr string) string {
if widget.FocusChar() == "" {
return fmt.Sprintf(" %s ", defaultStr)
}
return fmt.Sprintf(" %s [darkgray::u]%s[::-][green] ", defaultStr, widget.FocusChar())
}
func (widget *TextWidget) Disable() {
widget.enabled = false
}
func (widget *TextWidget) Disabled() bool {
return !widget.Enabled()
}
func (widget *TextWidget) Enabled() bool {
return widget.enabled
}
func (widget *TextWidget) Focusable() bool {
return widget.enabled && widget.focusable
}
func (widget *TextWidget) FocusChar() string {
return widget.focusChar
}
func (widget *TextWidget) HelpText() string {
return fmt.Sprintf("\n There is no help available for widget %s", widget.commonSettings.Module.Type)
}
func (widget *TextWidget) Name() string {
return widget.name
}
// Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not
func (widget *TextWidget) Refreshing() bool {
return widget.refreshing
}
// RefreshInterval returns how often, in seconds, the widget will return its data
func (widget *TextWidget) RefreshInterval() int {
return widget.refreshInterval
}
func (widget *TextWidget) SetFocusChar(char string) {
widget.focusChar = char
}
func (widget *TextWidget) String() string {
return widget.name
}
func (widget *TextWidget) TextView() *tview.TextView {
return widget.View
}
func (widget *TextWidget) Redraw(title, text string, wrap bool) {
widget.app.QueueUpdateDraw(func() {
widget.View.Clear()
widget.View.SetWrap(wrap)
widget.View.SetTitle(widget.ContextualTitle(title))
widget.View.SetText(text)
})
}
/* -------------------- Unexported Functions -------------------- */
func (widget *TextWidget) addView() *tview.TextView {
view := tview.NewTextView()
view.SetBackgroundColor(ColorFor(widget.commonSettings.Colors.Background))
view.SetBorderColor(ColorFor(widget.BorderColor()))
view.SetTextColor(ColorFor(widget.commonSettings.Colors.Text))
view.SetTitleColor(ColorFor(widget.commonSettings.Colors.Title))
view.SetBorder(true)
view.SetDynamicColors(true)
view.SetWrap(false)
return view
}