-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterminal_ui.go
475 lines (417 loc) · 13.1 KB
/
terminal_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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
Copyright (c) 2022 Murex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package cli
import (
"fmt"
"github.com/murex/tcr/desktop"
"github.com/murex/tcr/engine"
"github.com/murex/tcr/params"
"github.com/murex/tcr/report"
"github.com/murex/tcr/role"
"github.com/murex/tcr/runmode"
"github.com/murex/tcr/settings"
"github.com/murex/tcr/ui"
"github.com/murex/tcr/vcs/git"
"github.com/murex/tcr/vcs/p4"
"os"
)
// TerminalUI is the user interface implementation when using the Command Line Interface
type TerminalUI struct {
reportingChannel chan bool
tcr engine.TCRInterface
params params.Params
desktop *desktop.Desktop
mainMenu *menu
roleMenu *menu
}
const (
enterKey = 0x0a
)
const (
pullMenuHelper = "Pull from remote"
pushMenuHelper = "Push to remote"
syncMenuHelper = "Synchronize with depot"
enterDriverRoleMenuHelper = "Driver role"
enterNavigatorRoleMenuHelper = "Navigator role"
gitAutoPushMenuHelper = "Turn on/off git auto-push"
quitMenuHelper = "Quit"
optionsMenuHelper = "List available options"
timerStatusMenuHelper = "Timer status"
quitDriverRoleMenuHelper = "Quit Driver role"
quitNavigatorRoleMenuHelper = "Quit Navigator role"
)
// New creates a new instance of terminal
func New(p params.Params, tcr engine.TCRInterface) ui.UserInterface {
setLinePrefix("[" + settings.ApplicationName + "]")
var term = TerminalUI{params: p, tcr: tcr, desktop: desktop.NewDesktop(nil)}
term.mainMenu = term.initMainMenu()
term.roleMenu = term.initRoleMenu()
term.MuteDesktopNotifications(false)
term.StartReporting()
StartInterruptHandler()
return &term
}
// StartReporting tells the terminal to start reporting information
func (term *TerminalUI) StartReporting() {
term.reportingChannel = report.Subscribe(term)
}
// MuteDesktopNotifications allows preventing desktop Notification popups from being displayed.
// Used for test automation at the moment. Could be turned into a feature later if there is need for it.
func (term *TerminalUI) MuteDesktopNotifications(muted bool) {
if muted {
term.desktop.MuteNotifications()
} else {
term.desktop.UnmuteNotifications()
}
}
// StopReporting tells the terminal to stop reporting information
func (term *TerminalUI) StopReporting() {
if term.reportingChannel != nil {
report.Unsubscribe(term.reportingChannel)
}
}
// NotifyRoleStarting tells the user that TCR engine is starting with the provided role
func (term *TerminalUI) NotifyRoleStarting(r role.Role) {
term.ReportTitle(false, "Starting with ", r.LongName(), ". Press ? for options")
}
// NotifyRoleEnding tells the user that TCR engine is ending the provided role
func (term *TerminalUI) NotifyRoleEnding(r role.Role) {
term.ReportInfo(false, "Ending ", r.LongName())
}
// ReportSimple reports simple messages
func (*TerminalUI) ReportSimple(_ bool, a ...any) {
printUntouched(a...)
}
// ReportInfo reports info messages
func (*TerminalUI) ReportInfo(_ bool, a ...any) {
printInCyan(a...)
}
// ReportTitle reports title messages
func (*TerminalUI) ReportTitle(_ bool, a ...any) {
printHorizontalLine()
printInCyan(a...)
}
// ReportTimer reports timer messages
func (term *TerminalUI) ReportTimer(emphasis bool, a ...any) {
printInGreen(a...)
term.notifyOnEmphasis(emphasis, "⏳", a...)
}
// ReportSuccess reports success messages
func (term *TerminalUI) ReportSuccess(emphasis bool, a ...any) {
printInGreen(a...)
term.notifyOnEmphasis(emphasis, "🟢", a...)
}
// ReportWarning reports warning messages
func (term *TerminalUI) ReportWarning(emphasis bool, a ...any) {
printInYellow(a...)
term.notifyOnEmphasis(emphasis, "🔶", a...)
}
// ReportError reports error messages
func (term *TerminalUI) ReportError(emphasis bool, a ...any) {
printInRed(a...)
term.notifyOnEmphasis(emphasis, "🟥", a...)
}
func (term *TerminalUI) notifyOnEmphasis(emphasis bool, emoji string, a ...any) {
if emphasis {
err := term.desktop.ShowNotification(desktop.NormalLevel, emoji+" "+settings.ApplicationName, fmt.Sprint(a...))
if err != nil {
term.ReportWarning(false, "Failed to show desktop notification: ", err.Error())
}
}
}
func (term *TerminalUI) enterMainMenu() {
term.whatShallWeDo()
term.runMenuLoop(term.mainMenu)
}
func (term *TerminalUI) enterRole(r role.Role) {
// We ask first TCR engine to start...
if err := term.runTCR(r); err != nil {
term.ReportError(false, err)
return
}
// Then we enter the role menu loop, waiting for user input
term.runMenuLoop(term.roleMenu)
}
func (term *TerminalUI) runTCR(r role.Role) error {
switch r {
case role.Navigator{}:
term.tcr.RunAsNavigator()
case role.Driver{}:
term.tcr.RunAsDriver()
default:
return fmt.Errorf("no action defined for %s", r.LongName())
}
return nil
}
func (term *TerminalUI) runMenuLoop(m *menu) {
for {
input := term.readKeyboardInput()
matched, quit := m.matchAndRun(input)
if quit {
return
}
if !matched && input != enterKey {
term.keyNotRecognizedMessage()
term.listMenuOptions(m, "Please choose one of the following:")
}
}
}
func (term *TerminalUI) readKeyboardInput() byte {
keyboardInput := make([]byte, 1)
_, err := os.Stdin.Read(keyboardInput)
if err != nil {
term.ReportWarning(false, "Something went wrong while reading from stdin: ", err)
}
return keyboardInput[0]
}
func (term *TerminalUI) vcsPull() {
term.tcr.VCSPull()
}
func (term *TerminalUI) vcsPush() {
term.tcr.VCSPush()
}
func (term *TerminalUI) whatShallWeDo() {
term.listMenuOptions(term.mainMenu, "What shall we do?")
}
func (term *TerminalUI) keyNotRecognizedMessage() {
term.ReportWarning(false, "Key not recognized. Press ? for available options")
}
func (term *TerminalUI) showTimerStatus() {
if settings.EnableMobTimer {
if r := term.tcr.GetCurrentRole(); r != nil && r.RunsWithTimer() {
term.tcr.ReportMobTimerStatus()
} else {
term.keyNotRecognizedMessage()
}
}
}
// ShowRunningMode shows the current running mode
func (term *TerminalUI) ShowRunningMode(mode runmode.RunMode) {
term.ReportTitle(false, "Running in ", mode.Name(), " mode")
}
// ShowSessionInfo shows main information related to the current TCR session
func (term *TerminalUI) ShowSessionInfo() {
info := term.tcr.GetSessionInfo()
term.ReportTitle(false, "Base Directory: ", info.BaseDir)
term.ReportInfo(false, "Work Directory: ", info.WorkDir)
term.ReportInfo(false, "Language=", info.LanguageName, ", Toolchain=", info.ToolchainName)
term.reportVCSInfo(info)
}
func (term *TerminalUI) reportVCSInfo(info engine.SessionInfo) {
switch info.VCSName {
case git.Name:
autoPush := "disabled"
if info.GitAutoPush {
autoPush = "enabled"
}
term.ReportInfo(false, "Running on ", info.VCSSessionSummary, " with auto-push ", autoPush)
case p4.Name:
term.ReportInfo(false, "Running with ", info.VCSSessionSummary)
default:
term.ReportWarning(false, "VCS \"", info.VCSName, "\" is unknown")
}
}
// Confirm asks the user for confirmation
func (term *TerminalUI) Confirm(message string, defaultAnswer bool) bool {
_ = SetRaw()
defer Restore()
term.ReportWarning(false, message)
term.ReportWarning(false, "Do you want to proceed? ", yesOrNoAdvice(defaultAnswer))
keyboardInput := make([]byte, 1)
for {
_, _ = os.Stdin.Read(keyboardInput)
switch keyboardInput[0] {
case 'y', 'Y':
return true
case 'n', 'N':
return false
case enterKey:
return defaultAnswer
}
}
}
func yesOrNoAdvice(defaultAnswer bool) string {
if defaultAnswer {
return "[Y/n]"
}
return "[y/N]"
}
// Start runs the terminal session
func (term *TerminalUI) Start() {
term.initTCREngine()
if term.params.Mode.IsInteractive() {
_ = SetRaw()
defer Restore()
}
switch term.params.Mode {
case runmode.Solo{}:
// When running TCR in solo mode, there's no selection menu:
// we directly enter driver mode, and quit when done
term.enterRole(role.Driver{})
Restore()
term.tcr.Quit()
case runmode.Mob{}:
// When running TCR in mob mode, every participant
// is given the possibility to switch between
// driver and navigator modes
term.enterMainMenu()
case runmode.OneShot{}:
// When running TCR in one-shot mode, there's no selection menu:
// we directly ask TCR engine to run one cycle and quit when done
term.tcr.RunTCRCycle()
term.tcr.Quit()
case runmode.Check{}:
// When running TCR in check mode, there's no selection menu:
// we directly ask TCR engine to run a check and quit when done
term.tcr.RunCheck(term.params)
term.tcr.Quit()
case runmode.Log{}:
// When running TCR in log mode, there's no selection menu:
// we directly ask TCR engine to print the commit history and quit when done
term.tcr.PrintLog(term.params)
term.tcr.Quit()
case runmode.Stats{}:
// When running TCR in stats mode, there's no selection menu:
// we directly ask TCR engine to print the stats and quit when done
term.tcr.PrintStats(term.params)
term.tcr.Quit()
default:
term.ReportError(false, "Unknown run mode: ", term.params.Mode)
}
}
func (term *TerminalUI) initTCREngine() {
term.tcr.Init(term, term.params)
}
func (term *TerminalUI) listMenuOptions(m *menu, title string) {
term.ReportTitle(false, title)
for _, option := range m.getOptions() {
term.ReportInfo(false, (*option).toString())
}
}
func (term *TerminalUI) initMainMenu() *menu {
m := newMenu("Main menu")
m.addOptions(
newMenuOption('D', enterDriverRoleMenuHelper, nil,
term.enterRoleMenuAction(role.Driver{}), false),
newMenuOption('N', enterNavigatorRoleMenuHelper, nil,
term.enterRoleMenuAction(role.Navigator{}), false),
newMenuOption('P', gitAutoPushMenuHelper,
term.gitMenuEnabler(),
term.autoPushMenuAction(), false),
newMenuOption('L', pullMenuHelper,
term.gitMenuEnabler(),
term.vcsPullMenuAction(), false),
newMenuOption('S', pushMenuHelper,
term.gitMenuEnabler(),
term.vcsPushMenuAction(), false),
newMenuOption('Y', syncMenuHelper,
term.p4MenuEnabler(),
term.vcsPullMenuAction(), false),
newMenuOption('Q', quitMenuHelper, nil,
term.quitMenuAction(), true),
newMenuOption('?', optionsMenuHelper, nil,
term.optionsMenuAction(m), false),
)
return m
}
func (term *TerminalUI) initRoleMenu() *menu {
m := newMenu("Role menu")
m.addOptions(
newMenuOption('T', timerStatusMenuHelper,
term.timerStatusMenuEnabler(),
term.timerStatusMenuAction(), false),
newMenuOption('Q', quitDriverRoleMenuHelper,
term.quitRoleMenuEnabler(role.Driver{}),
term.quitRoleMenuAction(), true),
newMenuOption('Q', quitNavigatorRoleMenuHelper,
term.quitRoleMenuEnabler(role.Navigator{}),
term.quitRoleMenuAction(), true),
newMenuOption('?', optionsMenuHelper, nil,
term.optionsMenuAction(m), false),
)
return m
}
func (term *TerminalUI) enterRoleMenuAction(r role.Role) menuAction {
return func() {
term.enterRole(r)
term.whatShallWeDo()
}
}
func (term *TerminalUI) gitMenuEnabler() menuEnabler {
return func() bool {
return term.params.VCS == git.Name
}
}
func (term *TerminalUI) p4MenuEnabler() menuEnabler {
return func() bool {
return term.params.VCS == p4.Name
}
}
func (term *TerminalUI) autoPushMenuAction() menuAction {
return func() {
term.tcr.ToggleAutoPush()
term.ShowSessionInfo()
term.whatShallWeDo()
}
}
func (term *TerminalUI) vcsPullMenuAction() menuAction {
return func() {
term.vcsPull()
term.whatShallWeDo()
}
}
func (term *TerminalUI) vcsPushMenuAction() menuAction {
return func() {
term.vcsPush()
term.whatShallWeDo()
}
}
func (term *TerminalUI) optionsMenuAction(m *menu) menuAction {
return func() {
term.listMenuOptions(m, "Available Options:")
}
}
func (term *TerminalUI) quitMenuAction() menuAction {
return func() {
Restore()
term.tcr.Quit()
}
}
func (term *TerminalUI) timerStatusMenuEnabler() menuEnabler {
return func() bool {
r := term.tcr.GetCurrentRole()
return settings.EnableMobTimer && r != nil && r.RunsWithTimer()
}
}
func (term *TerminalUI) timerStatusMenuAction() menuAction {
return func() {
term.showTimerStatus()
}
}
func (term *TerminalUI) quitRoleMenuEnabler(r role.Role) menuEnabler {
return func() bool {
return term.tcr.GetCurrentRole() == r
}
}
func (term *TerminalUI) quitRoleMenuAction() menuAction {
return func() {
term.ReportWarning(false, "OK, I heard you")
term.tcr.Stop()
}
}