-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.go
243 lines (215 loc) · 4.95 KB
/
gui.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
package main
import (
"context"
"fmt"
"strings"
"github.com/ably/ably-go/ably"
"github.com/jroimartin/gocui"
)
var colours []gocui.Attribute = []gocui.Attribute{gocui.ColorBlue, gocui.ColorCyan, gocui.ColorGreen, gocui.ColorMagenta, gocui.ColorRed}
type Layout struct {
Id string
FileName string
Log bool
Editable bool
Setup bool
Save *Save
Redraw bool
Editor *Editor
Code string
Members []*ably.PresenceMessage
Cursors map[string]Cursor
}
type Cursor struct {
X int `json:"x"`
Y int `json:"y"`
}
func updateBar(gui *gocui.Gui, code string, users int) {
bar, err := gui.View("bar")
if err == nil {
bar.Clear()
fmt.Fprintf(bar, "Users: %d Session: %s", users, code)
}
}
func initGui() (*gocui.Gui, error) {
gui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return nil, err
}
return gui, nil
}
func (l *Layout) Layout(gui *gocui.Gui) error {
var notify, editor, bar, keys, log, members *gocui.View
var err error
//gui.Mouse = true
gui.Cursor = true
maxX, maxY := gui.Size()
log, err = gui.SetView("log", maxX/2, maxY/2, maxX-1, maxY-3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
log.Autoscroll = true
}
editor, err = gui.SetView("editor", 0, 0, maxX-21, maxY-3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
//editor.Autoscroll = true
//editor.Wrap = true
}
if l.FileName != "" {
editor.Title = l.FileName
} else {
editor.Title = "Unsaved"
}
editor.Editable = l.Editable
editor.Editor = l.Editor
_, err = gui.SetCurrentView("editor")
if err != nil {
return err
}
notify, err = gui.SetView("notify", 0, maxY-3, maxX, maxY-1)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
notify.Frame = false
}
keys, err = gui.SetView("keys", maxX-42, maxY-2, maxX, maxY)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
keys.Frame = false
fmt.Fprint(keys, "C-x Exit C-n New C-s Save C-a Save As")
}
members, err = gui.SetView("members", maxX-20, 0, maxX-1, maxY-3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
members.Frame = true
}
members.Clear()
for i, user := range l.Members {
col := colours[i%len(colours)]
fmt.Fprintf(members, "\x1b[0;%dm%s\n", col+29, user.Message.Data.(string))
}
bar, err = gui.SetView("bar", 0, maxY-2, maxX-42, maxY)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
bar.Frame = false
}
bar.Clear()
fmt.Fprintf(bar, "Users: %d Session: %s", len(l.Members), l.Code)
if !l.Setup {
l.Setup = true
err = gui.SetKeybinding("", gocui.KeyCtrlX, gocui.ModNone, quit)
if err != nil {
return err
}
err = gui.SetKeybinding("", gocui.KeyCtrlL, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
if l.Log {
gui.SetViewOnBottom("log")
} else {
gui.SetViewOnTop("log")
}
l.Log = !l.Log
return nil
})
if err != nil {
return err
}
err = gui.SetKeybinding("editor", gocui.KeyCtrlA, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
l.Save = &Save{Editor: l.Editor, Force: true}
return nil
})
if err != nil {
return err
}
err = gui.SetKeybinding("editor", gocui.KeyCtrlS, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
l.Save = &Save{Editor: l.Editor}
return nil
})
if err != nil {
return err
}
err = gui.SetKeybinding("editor", gocui.KeyCtrlN, gocui.ModNone, func(gui *gocui.Gui, v *gocui.View) error {
err = l.Editor.Channel.Publish(context.Background(), "new", "")
if err == nil {
editor.Editable = false
} else {
l.Editor.Nodify(err.Error())
}
return nil
})
}
for _, view := range gui.Views() {
name := view.Name()
if strings.HasPrefix(name, "cursor-") {
found := false
for _, member := range l.Members {
if name == "cursor-"+member.ClientID {
found = true
break
}
if !found {
gui.DeleteView(name)
}
}
}
}
for i, member := range l.Members {
if member.ClientID == l.Id {
continue
}
pos, ok := l.Cursors[member.ClientID]
if !ok {
continue
}
xs, ys := editor.Size()
xo, yo := editor.Origin()
x := pos.X - xo + 1
y := pos.Y - yo + 1
if x < 1 || x > xs+1 || y < 1 || y > ys+1 {
gui.DeleteView("cursor-" + member.ClientID)
} else {
view, err := gui.SetView("cursor-"+member.ClientID, x-1, y-1, x+1, y+1)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
view.Frame = false
}
view.BgColor = colours[i%len(colours)]
view.Clear()
lines := editor.BufferLines()
if len(lines) > pos.Y && len(lines[pos.Y]) > pos.X {
view.Write([]byte{lines[pos.Y][pos.X]})
}
}
}
if l.Redraw {
l.Redraw = false
l.Editor.displyText()
}
if l.Save != nil {
if l.FileName == "" || l.Save.Force {
err = l.Save.Layout(l.Editor.Gui)
if err != nil {
return err
}
} else {
l.Save.Save()
}
}
//gui.SetViewOnTop("log")
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}