-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
141 lines (128 loc) · 3.33 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
package main
import (
"github.com/jroimartin/gocui"
)
func navigator(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
// Rules for navigating the panel history
switch {
case key == gocui.KeyArrowUp:
v.MoveCursor(0, -1, false)
case key == gocui.KeyArrowDown:
v.MoveCursor(0, 1, false)
}
}
// Generate the UI and its rules
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
// Log history, scrollable
if v, err := g.SetView("logs", 1, 1, maxX-1, maxY/2-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
if _, err := g.SetCurrentView("logs"); err != nil {
return err
}
v.Title = "Log history"
v.Autoscroll = true
v.Overwrite = false
// We use the navigator as an editor, but the text displayed will not change
v.Editor = gocui.EditorFunc(navigator)
v.Editable = true
v.Wrap = true
}
// Alert history, scrollable
if v, err := g.SetView("alerts", 1, maxY/2+1, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
if _, err := g.SetCurrentView("alerts"); err != nil {
return err
}
v.Title = "Alerts history"
v.Autoscroll = true
v.Overwrite = false
// We use the navigator as an editor, but the text displayed will not change
v.Editor = gocui.EditorFunc(navigator)
v.Editable = true
v.Wrap = true
}
// No error occured during any initialization
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func update(g *gocui.Gui) error {
return nil
}
// Display line in the corresponding view
func displayLine(g *gocui.Gui, viewName string, line string) {
g.Update(
func(g *gocui.Gui) error {
if line[len(line)-1:] != "\n" {
line = line + "\n"
}
byteMessage := []byte(line)
originalView := g.CurrentView()
v, err := g.SetCurrentView(viewName)
if err != nil {
return err
}
_, err = v.Write(byteMessage)
if err != nil {
return err
}
_, err = g.SetCurrentView(originalView.Name())
if err != nil {
return err
}
return nil
})
}
// Switch from one pane to the other
func switchView(g *gocui.Gui, v *gocui.View) error {
g.Update(
func(g *gocui.Gui) error {
currentView := g.CurrentView()
currentView.Autoscroll = true
originalViewName := currentView.Name()
var newViewName string
if originalViewName == "logs" {
newViewName = "alerts"
} else {
newViewName = "logs"
}
_, err := g.SetCurrentView(newViewName)
if err != nil {
return err
}
return nil
})
return nil
}
// Initialize the keybindings that depend only on the gui
func initKeyBindings(g *gocui.Gui) error {
// Ctrl-C leaves the application, whatever the focused view
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
// Switch to the next pane using left arrow key
if err := g.SetKeybinding("", gocui.KeyArrowLeft, gocui.ModNone, switchView); err != nil {
return err
}
// Switch to the next pane using right arrow key
if err := g.SetKeybinding("", gocui.KeyArrowRight, gocui.ModNone, switchView); err != nil {
return err
}
// Allow scrolling-up when pressing enter
if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
currentView := g.CurrentView()
currentView.Autoscroll = false
g.Update(update)
return nil
}); err != nil {
return err
}
return nil
}