-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (165 loc) · 4.07 KB
/
main.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
package main
/*
* A shiny status page.
*
* Want it to combine my existing idle page and tiny-care-terminal.
*
* Things to include:
* - some twitter accounts
* - @tinycarebot, @selfcare_bot and @magicrealismbot. Maybe that boat one instead of magic realism.
* - weather
* - recent git commits
* - system status:
* - User/hostname
* - Kerberos ticket status
* - Current time
* - Uptime
* - Battery and time left
* - Audio status and volume
* - Network
* - Local, docker, wireless
* - Disk
* - Mounts, free/used/total/percentage w/ color
* - CPU
* - Load average w/ color
* - Percentage
* - Top processes?
* - Status of git repos
*
* Minimum terminal size to support:
* - 189x77 (half monitor with some stacks)
* - 104x56ish? (half the laptop screen with some stacks)
* - 100x50? (nice and round)
* - 80x40? (my default putty)
*
*/
import (
"io/ioutil"
"log"
"os"
"time"
ui "github.com/gizak/termui"
)
//
// Rendering loop
//
func loop(widgets []CAHWidget, header *HeaderWidget) {
render := func() {
ui.Body.Align()
ui.Clear()
ui.Render(header.widget, ui.Body)
}
//
// Activate
//
render()
firstTimeResize := false
ticker := time.NewTicker(5 * time.Second)
for {
// Call all resize funcs (only the first time)
if !firstTimeResize {
firstTimeResize = true
for _, w := range widgets {
w.resize()
w.update()
}
render()
}
select {
case e := <-ui.PollEvents():
switch e.ID {
case "q", "<C-c>":
return
case "<Resize>":
payload := e.Payload.(ui.Resize)
// Re-layout on resize
ui.Body.Width = payload.Width - 2
// Call all resize funcs
for _, w := range widgets {
w.resize()
}
// Re-render
render()
}
case <-ticker.C:
for _, w := range widgets {
w.update()
}
render()
}
}
}
////////////////////////////////////////////
// Where the real stuff happens
////////////////////////////////////////////
func main() {
// Set up logging?
if LogToFile() {
logFile, logErr := os.OpenFile("go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
if logErr != nil {
panic(logErr)
}
defer logFile.Close()
log.SetOutput(logFile)
} else {
// Disable logging
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
}
// Set up the console UI
uiErr := ui.Init()
if uiErr != nil {
panic(uiErr)
}
defer ui.Close()
//
// Create the widgets
//
widgets := make([]CAHWidget, 0)
header := NewHeaderWidget()
widgets = append(widgets, header)
hostInfo := NewHostInfoWidget()
widgets = append(widgets, hostInfo)
network := NewNetworkWidget()
widgets = append(widgets, network)
battery := NewBatteryWidget()
widgets = append(widgets, battery)
audio := NewAudioWidget()
widgets = append(widgets, audio)
disk := NewDiskColumn(6, 0)
widgets = append(widgets, disk)
cpu := NewCPUWidget()
widgets = append(widgets, cpu)
repo := NewGitRepoWidget()
widgets = append(widgets, repo)
twitter1 := NewTwitterWidget(GetTwitterAccount1(), ui.ColorBlue|ui.AttrBold)
widgets = append(widgets, twitter1)
twitter2 := NewTwitterWidget(GetTwitterAccount2(), ui.ColorCyan)
widgets = append(widgets, twitter2)
twitter3 := NewTwitterWidget(GetTwitterAccount3(), ui.ColorMagenta)
widgets = append(widgets, twitter3)
weather := NewWeatherWidget(GetWeatherLocation())
widgets = append(widgets, weather)
//
// Create the layout
//
// Give space around the ui.Body for the header box to wrap all around
ui.Body.Width = ui.TermWidth() - 2
ui.Body.X = 1
ui.Body.Y = 1
ui.Body.AddRows(
ui.NewRow(
ui.NewCol(6, 0, hostInfo.getGridWidget(), battery.getGridWidget(), audio.getGridWidget(), weather.getGridWidget()),
ui.NewCol(6, 0, cpu.getGridWidget())),
ui.NewRow(
disk.getColumn(),
ui.NewCol(6, 0, network.getGridWidget())),
ui.NewRow(
ui.NewCol(12, 0, repo.getGridWidget())),
ui.NewRow(
ui.NewCol(4, 0, twitter1.getGridWidget()),
ui.NewCol(4, 0, twitter2.getGridWidget()),
ui.NewCol(4, 0, twitter3.getGridWidget())))
ui.Body.Align()
loop(widgets, header)
}