Repository navigation
Expand file tree
/
Copy pathbase.go
More file actions
183 lines (149 loc) · 4.21 KB
/
Copy pathbase.go
File metadata and controls
183 lines (149 loc) · 4.21 KB
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
<
8000
div data-line-number="34" class="react-line-number react-code-text" style="padding-right:16px">3435
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package view
import (
"fmt"
"sync"
"time"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/utils"
)
type Base struct {
bordered bool
commonSettings *cfg.Common
enabled bool
enabledMutex *sync.Mutex
focusChar string
focusable bool
helpTextFunc func() string
name string
pages *tview.Pages
quitChan chan bool
refreshInterval time.Duration
refreshing bool
tviewApp *tview.Application
view *tview.TextView
RedrawChan chan bool
}
// NewBase creates and returns an instance of the Base module, the lowest-level
// primitive module from which all others are derived
func NewBase(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, commonSettings *cfg.Common) *Base {
base := &Base{
commonSettings: commonSettings,
bordered: commonSettings.Bordered,
enabled: commonSettings.Enabled,
enabledMutex: &sync.Mutex{},
focusChar: commonSettings.FocusChar(),
focusable: commonSettings.Focusable,
name: commonSettings.Name,
pages: pages,
quitChan: make(chan bool),
refreshInterval: commonSettings.RefreshInterval,
refreshing: false,
tviewApp: tviewApp,
RedrawChan: redrawChan,
}
return base
}
/* -------------------- Exported Functions -------------------- */
// Bordered returns whether or not this widget should be drawn with a border
func (base *Base) Bordered() bool {
return base.bordered
}
// BorderColor returns the color that the border of this widget should be drawn in
func (base *Base) BorderColor() string {
if base.Focusable() {
return base.commonSettings.Colors.Focusable
}
9B34
return base.commonSettings.Colors.Unfocusable
}
func (base *Base) CommonSettings() *cfg.Common {
return base.commonSettings
}
func (base *Base) ConfigText() string {
return utils.HelpFromInterface(cfg.Common{})
}
func (base *Base) ContextualTitle(defaultStr string) string {
switch {
case defaultStr == "" && base.FocusChar() == "":
return ""
case defaultStr != "" && base.FocusChar() == "":
return fmt.Sprintf(" %s ", defaultStr)
case defaultStr == "" && base.FocusChar() != "":
return fmt.Sprintf(" [darkgray::u]%s[::-][white] ", base.FocusChar())
}
return fmt.Sprintf(" %s [darkgray::u]%s[::-][white] ", defaultStr, base.FocusChar())
}
func (base *Base) Disable() {
base.enabledMutex.Lock()
base.enabled = false
base.enabledMutex.Unlock()
}
func (base *Base) Disabled() bool {
base.enabledMutex.Lock()
result := !base.enabled
base.enabledMutex.Unlock()
return result
}
func (base *Base) Enabled() bool {
base.enabledMutex.Lock()
result := base.enabled
base.enabledMutex.Unlock()
return result
}
func (base *Base) Focusable() bool {
base.enabledMutex.Lock()
result := base.enabled && base.focusable
base.enabledMutex.Unlock()
return result
}
func (base *Base) FocusChar() string {
return base.focusChar
}
func (base *Base) Name() string {
return base.name
}
func (base *Base) QuitChan() chan bool {
return base.quitChan
}
// Refreshing returns TRUE if the base is currently refreshing its data, FALSE if it is not
func (base *Base) Refreshing() bool {
return base.refreshing
}
// RefreshInterval returns how often the base will return its data
func (base *Base) RefreshInterval() time.Duration {
return base.refreshInterval
}
func (base *Base) SetFocusChar(char string) {
base.focusChar = char
}
// SetView assigns the passed-in tview.TextView view to this widget
func (base *Base) SetView(view *tview.TextView) {
base.view = view
}
// ShowHelp displays the modal help dialog for a module
func (base *Base) ShowHelp() {
if base.pages == nil {
return
}
closeFunc := func() {
base.pages.RemovePage("help")
base.tviewApp.SetFocus(base.view)
}
modal := NewBillboardModal(base.helpTextFunc(), closeFunc)
base.pages.AddPage("help", modal, false, true)
base.tviewApp.SetFocus(modal)
// Tell the app to force redraw the screen (non-blocking to avoid deadlock)
select {
case base.RedrawChan <- true:
default:
}
}
func (base *Base) Stop() {
base.enabledMutex.Lock()
base.enabled = false
base.enabledMutex.Unlock()
base.quitChan <- true
}
func (base *Base) String() string {
return base.name
}