-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
219 lines (176 loc) · 3.75 KB
/
ui.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
package main
import (
"errors"
"log"
ui "github.com/gizak/termui"
)
const (
MaxRecordSize = 1000
)
var (
win = Window{}
)
// ScrollTable is a scrollable table
type ScrollTable struct {
ui.Table
Head []string
Body [][]string
currentLine int
currentIndex int
RowPerPage int
}
func (t *ScrollTable) moveUp() {
}
func (t *ScrollTable) moveDown() {
}
func (t *ScrollTable) prevPage() {
}
func (t *ScrollTable) nextPage() {
}
func (t *ScrollTable) update(rows [][]string) error {
if rows == nil || len(rows) == 0 {
if t.Body == nil {
t.Rows = [][]string{t.Head}
}
return errors.New("updating rows with empty data")
}
if len(rows) > MaxRecordSize {
t.Body = rows[:MaxRecordSize]
} else {
t.Body = rows
}
t.currentIndex = 0
t.currentLine = 0
rs := [][]string{}
if t.Head != nil && len(t.Head) > 0 {
rs = append(rs, t.Head)
}
currentPageSize := 12
if len(t.Body) < t.RowPerPage {
currentPageSize = len(t.Body)
}
rs = append(rs, t.Body[:currentPageSize]...)
t.Rows = rs
return nil
}
// HintPar stores the hints and status message
type HintPar struct {
ui.Par
Message string
}
// Window is the structure stores the whole ui widgets
type Window struct {
TicketTable ScrollTable
HintBar HintPar
inited bool
}
func (w *Window) Init() {
if w.inited {
return
}
w.renderWidgets()
w.registerEvents()
w.inited = true
}
func (w *Window) Refresh() {
if w.inited {
ui.Render(ui.Body)
}
}
func (w *Window) renderWidgets() {
w.TicketTable = ScrollTable{
Table: *ui.NewTable(),
Head: []string{"车次", "起始", "--->", "终止", "发车时间", "--", "到达时间", "历时", "座位信息", "", "", ""},
RowPerPage: 12,
}
w.TicketTable.X = 0
w.TicketTable.Y = 0
w.TicketTable.Width = 1
w.TicketTable.Height = 36
w.TicketTable.BorderLabel = "CN12306"
err := w.TicketTable.update(getRows())
if err != nil {
log.Println(err)
}
w.HintBar = HintPar{Par: *ui.NewPar("退出:Q 刷新:R 上:K 下:J")}
w.HintBar.Height = 3
w.HintBar.Width = 17
w.HintBar.X = 20
w.HintBar.BorderLabel = "帮助"
ui.Body.AddRows(
ui.NewRow(ui.NewCol(12, 0, &w.TicketTable.Table)),
ui.NewRow(ui.NewCol(12, 0, &w.HintBar.Par)),
)
ui.Body.Align()
ui.Clear()
ui.Render(ui.Body)
}
// exit, refresh (force trigger query), down/up (store current view list), timer, resize
// stop auto-refresh in 10s if you scroll to view the list
func (w *Window) registerEvents() {
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/sys/kbd/C-c", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/sys/kbd/r", func(ui.Event) {
w.TicketTable.update(getRows())
w.Refresh()
})
// prev page
ui.Handle("/sys/kbd/h", func(ui.Event) {
})
// scroll down
ui.Handle("/sys/kbd/j", func(ui.Event) {
// if index+12 < len(rows) {
// index++
// }
// end := index + 12
// if len(rows) < 12 {
// end = len(rows) - 1
// }
// w.TicketTable.Rows = rows[index:end]
// ui.Render(ui.Body)
})
// scroll up
ui.Handle("/sys/kbd/k", func(ui.Event) {
// if index > 1 {
// index--
// }
// end := index + 12
// if len(rows) < 12 {
// end = len(rows) - 1
// }
// w.TicketTable.Rows = rows[index:end]
// ui.Render(ui.Body)
})
// next page page
ui.Handle("/sys/kbd/l", func(ui.Event) {
})
// gg jumps to start
ui.Handle("/sys/kbd/g", func(e ui.Event) {
})
// GG jumps to end
ui.Handle("/sys/kbd/G", func(e ui.Event) {
})
ui.Handle("/timer/1s", func(e ui.Event) {
// table.Rows = getRows()
// ui.Render(ui.Body)
})
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ui.Body.Width = ui.TermWidth()
ui.Body.Align()
ui.Clear()
ui.Render(ui.Body)
})
}
// terminal UI
func startUI() {
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
win.Init()
ui.Loop()
}