forked from wtfutil/wtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wtf.go
256 lines (219 loc) · 6.03 KB
/
wtf.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/radovskyb/watcher"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks"
"github.com/senorprogrammer/wtf/cmdrunner"
"github.com/senorprogrammer/wtf/cryptoexchanges/bittrex"
"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive"
"github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github"
"github.com/senorprogrammer/wtf/help"
"github.com/senorprogrammer/wtf/ipinfo"
"github.com/senorprogrammer/wtf/jira"
"github.com/senorprogrammer/wtf/newrelic"
"github.com/senorprogrammer/wtf/opsgenie"
"github.com/senorprogrammer/wtf/power"
"github.com/senorprogrammer/wtf/prettyweather"
"github.com/senorprogrammer/wtf/security"
"github.com/senorprogrammer/wtf/status"
"github.com/senorprogrammer/wtf/system"
"github.com/senorprogrammer/wtf/textfile"
"github.com/senorprogrammer/wtf/todo"
"github.com/senorprogrammer/wtf/weather"
"github.com/senorprogrammer/wtf/wtf"
)
/* -------------------- Functions -------------------- */
func addToGrid(grid *tview.Grid, widget wtf.Wtfable) {
if widget.Disabled() {
return
}
grid.AddItem(
widget.TextView(),
widget.Top(),
widget.Left(),
widget.Height(),
widget.Width(),
0,
0,
false,
)
}
// Grid stores all the widgets onscreen (like an HTML table)
func buildGrid(modules []wtf.Wtfable) *tview.Grid {
grid := tview.NewGrid()
grid.SetColumns(wtf.ToInts(Config.UList("wtf.grid.columns"))...)
grid.SetRows(wtf.ToInts(Config.UList("wtf.grid.rows"))...)
grid.SetBorder(false)
for _, module := range modules {
addToGrid(grid, module)
go wtf.Schedule(module)
}
return grid
}
func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlR:
refreshAllWidgets()
case tcell.KeyTab:
FocusTracker.Next()
case tcell.KeyBacktab:
FocusTracker.Prev()
case tcell.KeyEsc:
FocusTracker.None()
default:
return event
}
return event
}
// redrawApp redraws the rendered views to screen on a defined interval (set in config.yml)
// Use this because each textView widget can have it's own update interval, and I don't want to
// manage drawing co-ordination amongst them all. If you need to have a
// widget redraw on it's own schedule, use the view's SetChangedFunc() and pass it `app`.
func redrawApp(app *tview.Application) {
tick := time.NewTicker(time.Duration(Config.UInt("wtf.refreshInterval", 2)) * time.Second)
quit := make(chan struct{})
for {
select {
case <-tick.C:
app.Draw()
case <-quit:
tick.Stop()
return
}
}
}
func refreshAllWidgets() {
for _, widget := range Widgets {
go widget.Refresh()
}
}
func watchForConfigChanges(app *tview.Application, configFlag string, grid *tview.Grid, pages *tview.Pages) {
watch := watcher.New()
// notify write events.
watch.FilterOps(watcher.Write)
go func() {
for {
select {
case <-watch.Event:
loadConfig(configFlag)
makeWidgets(app, pages)
grid = buildGrid(Widgets)
pages.AddPage("grid", grid, true, true)
case err := <-watch.Error:
log.Fatalln(err)
case <-watch.Closed:
return
}
}
}()
// Watch config file for changes.
if err := watch.Add(configFlag); err != nil {
log.Fatalln(err)
}
// Start the watching process - it'll check for changes every 100ms.
if err := watch.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
}
/* -------------------- Main -------------------- */
var Config *config.Config
var FocusTracker wtf.FocusTracker
var Widgets []wtf.Wtfable
var (
commit = "dev"
date = "dev"
version = "dev"
)
func makeWidgets(app *tview.Application, pages *tview.Pages) {
// Always in alphabetical order
bamboohr.Config = Config
bittrex.Config = Config
clocks.Config = Config
cmdrunner.Config = Config
cryptolive.Config = Config
gcal.Config = Config
git.Config = Config
github.Config = Config
ipinfo.Config = Config
jira.Config = Config
newrelic.Config = Config
opsgenie.Config = Config
power.Config = Config
prettyweather.Config = Config
security.Config = Config
status.Config = Config
system.Config = Config
textfile.Config = Config
todo.Config = Config
weather.Config = Config
wtf.Config = Config
// Always in alphabetical order
Widgets = []wtf.Wtfable{
bamboohr.NewWidget(),
bittrex.NewWidget(),
clocks.NewWidget(),
cmdrunner.NewWidget(),
cryptolive.NewWidget(),
gcal.NewWidget(),
git.NewWidget(app, pages),
github.NewWidget(app, pages),
ipinfo.NewWidget(),
jira.NewWidget(),
newrelic.NewWidget(),
opsgenie.NewWidget(),
power.NewWidget(),
prettyweather.NewWidget(),
security.NewWidget(),
status.NewWidget(),
system.NewWidget(date, version),
textfile.NewWidget(app, pages),
todo.NewWidget(app, pages),
weather.NewWidget(app, pages),
}
FocusTracker = wtf.FocusTracker{
App: app,
Idx: -1,
Widgets: Widgets,
}
}
func loadConfig(configFlag string) {
Config = wtf.LoadConfigFile(configFlag)
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
cmdFlags := wtf.NewCommandFlags()
cmdFlags.Parse(version)
if cmdFlags.HasModule() {
help.DisplayModuleInfo(cmdFlags.Module)
}
/* -------------------- end flag parsing and handling -------------------- */
// Responsible for creating the configuration directory and default
// configuration file if they don't already exist
wtf.CreateConfigDir()
wtf.WriteConfigFile()
loadConfig(cmdFlags.Config)
os.Setenv("TERM", Config.UString("wtf.term", os.Getenv("TERM")))
app := tview.NewApplication()
pages := tview.NewPages()
makeWidgets(app, pages)
grid := buildGrid(Widgets)
pages.AddPage("grid", grid, true, true)
app.SetInputCapture(keyboardIntercept)
grid.SetBackgroundColor(wtf.ColorFor(Config.UString("wtf.colors.background", "black")))
// Loop in a routine to redraw the screen
go redrawApp(app)
go watchForConfigChanges(app, cmdFlags.Config, grid, pages)
if err := app.SetRoot(pages, true).Run(); err != nil {
fmt.Printf("An error occurred: %v\n", err)
os.Exit(1)
}
}