-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.go
285 lines (254 loc) · 7.1 KB
/
history.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
package main
import (
"fmt"
"io"
"log"
"strings"
"time"
messages "github.com/arborchat/arbor-go"
vs "github.com/arborchat/pergola/view_state"
"github.com/jroimartin/gocui"
wrap "github.com/mitchellh/go-wordwrap"
)
const ReplyView = "reply-view"
type History struct {
vs.ThreadView
ViewIDs map[string]struct{}
Query chan<- string
Outbound chan<- *messages.ChatMessage
}
// NewList creates a new History that uses the provided Tree
// to manage message history. This History acts as a layout manager
// for the gocui layout package. The method returns a History, a readonly
// channel of queries, and a readonly channel of new messages to be sent
// to the sever. The queries are message UUIDs that
// the local store has requested the message contents for.
func NewList(store *Tree) (*History, chan string, <-chan *messages.ChatMessage) {
queryChan := make(chan string)
outChan := make(chan *messages.ChatMessage)
return &History{
ThreadView: vs.New(store),
ViewIDs: make(map[string]struct{}),
Query: queryChan,
Outbound: outChan,
}, queryChan, outChan
}
func (h *History) destroyOldViews(ui *gocui.Gui) {
// destroy old views
for id := range h.ViewIDs {
ui.DeleteView(id)
}
// reset ids
h.ViewIDs = make(map[string]struct{})
}
// Layout builds a message history in the provided UI
func (m *History) Layout(ui *gocui.Gui) error {
m.destroyOldViews(ui)
maxX, maxY := ui.Size()
// get the latest history
query := m.Refresh()
if query != "" {
m.Query <- query
}
totalY := maxY // how much vertical space is left for drawing messages
cursorY := (totalY - 2) / 2
cursorX := 0
cursorId := m.Cursor()
if cursorId == "" {
return nil
}
err, cursorHeight := m.drawView(cursorX, cursorY, maxX-1, down, true, cursorId, ui) //draw the cursor message
if err != nil {
log.Println("error drawing cursor view: ", err)
return err
}
thread := m.Ancestry()
currentIdxBelow := vs.IndexOfMessageId(cursorId, thread)
currentIdxAbove := currentIdxBelow
lowerBound := cursorY + cursorHeight
replyY := lowerBound
for currentIdxBelow--; currentIdxBelow >= 0 && lowerBound < maxY; currentIdxBelow-- {
err, msgHeight := m.drawView(0, lowerBound, maxX-1, down, false, thread[currentIdxBelow].UUID, ui) //draw the cursor message
if err != nil {
log.Println("error drawing view: ", err)
return err
}
lowerBound += msgHeight
}
upperBound := cursorY - 1
for currentIdxAbove++; currentIdxAbove < len(thread) && upperBound >= 0; currentIdxAbove++ {
err, msgHeight := m.drawView(0, upperBound, maxX-1, up, false, thread[currentIdxAbove].UUID, ui) //draw the cursor message
if err != nil {
log.Println("error drawing view: ", err)
return err
}
upperBound -= msgHeight
}
if m.IsReplying() {
m.drawReplyView(0, replyY, maxX-1, 5, ui)
}
return nil
}
type Direction int
const up Direction = 0
const down Direction = 1
func (h *History) drawView(x, y, w int, dir Direction, isCursor bool, id string, ui *gocui.Gui) (error, int) {
const borderHeight = 2
const gutterWidth = 4
msg := h.ThreadView.Get(id)
if msg == nil {
log.Println("accessed nil message with id:", id)
}
seen := h.Seen(id)
numSiblings := len(h.Children(msg.Parent)) - 1
contents := wrap.WrapString(msg.Content, uint(w-gutterWidth-1))
height := strings.Count(contents, "\n") + borderHeight
var upperLeftX, upperLeftY, lowerRightX, lowerRightY int
if dir == up {
upperLeftX = x + gutterWidth
upperLeftY = y - height
lowerRightX = x + w
lowerRightY = y
} else if dir == down {
upperLeftX = x + gutterWidth
upperLeftY = y
lowerRightX = x + w
lowerRightY = y + height
}
log.Printf("message at (%d,%d) -> (%d,%d)\n", upperLeftX, upperLeftY, lowerRightX, lowerRightY)
if numSiblings > 0 {
name := id + "sib"
if v, err := ui.SetView(name, x, upperLeftY, x+gutterWidth, lowerRightY); err != nil {
if err != gocui.ErrUnknownView {
log.Println(err)
return err, 0
}
fmt.Fprintf(v, "%d", numSiblings)
h.ViewIDs[name] = struct{}{}
}
}
if v, err := ui.SetView(id, upperLeftX, upperLeftY, lowerRightX, lowerRightY); err != nil {
if err != gocui.ErrUnknownView {
log.Println(err)
return err, 0
}
v.Title = id
v.Wrap = true
fmt.Fprint(v, contents)
if isCursor {
ui.SetCurrentView(id)
h.MarkSeen(id)
seen = true
}
if !seen {
v.BgColor = gocui.ColorWhite
v.FgColor = gocui.ColorBlack
}
h.ViewIDs[id] = struct{}{}
}
return nil, height + 1
}
func (his *History) drawReplyView(x, y, w, h int, ui *gocui.Gui) error {
if v, err := ui.SetView(ReplyView, x, y, x+w, y+h); err != nil {
if err != gocui.ErrUnknownView {
log.Println(err)
return err
}
v.Title = "Reply to " + his.GetReplyId()
v.Editable = true
v.Wrap = true
ui.SetKeybinding(ReplyView, gocui.KeyEnter, gocui.ModNone, his.SendReply)
}
ui.SetCurrentView(ReplyView)
ui.SetViewOnTop(ReplyView)
return nil
}
func (m *History) BeginReply(g *gocui.Gui, v *gocui.View) error {
m.ReplyTo(m.Cursor())
return nil
}
func (m *History) SendReply(g *gocui.Gui, v *gocui.View) error {
if !m.IsReplying() {
return nil
}
data := make([]byte, 1024)
n, err := v.Read(data)
if err != nil && err != io.EOF {
log.Println("Err reading composed message", err)
return err
}
id := m.GetReplyId()
g.DeleteKeybindings(ReplyView) // apparently, deleting the view doesn't do this
g.DeleteView(ReplyView)
m.ClearReply()
msg := &messages.ChatMessage{
Username: "pergola",
Timestamp: time.Now().Unix(),
Parent: id,
Content: string(data[:n]),
}
log.Printf("Sending reply to %s: %s\n", id, string(data))
m.Outbound <- msg
return nil
}
func (m *History) CursorUp(g *gocui.Gui, v *gocui.View) error {
if m.IsReplying() {
return nil
}
m.MoveCursorTowardRoot()
return nil
}
func (m *History) CursorRight(g *gocui.Gui, v *gocui.View) error {
return m.cursorSide(right, g, v)
}
func (m *History) CursorLeft(g *gocui.Gui, v *gocui.View) error {
return m.cursorSide(left, g, v)
}
type side int
const left side = 0
const right side = 1
func (m *History) cursorSide(s side, g *gocui.Gui, v *gocui.View) error {
if m.IsReplying() {
return nil
}
id := m.Cursor()
msg := m.ThreadView.Get(id)
if msg == nil {
log.Println("Error fetching cursor message: %s", id)
return nil
} else if msg.Parent == "" {
log.Println("Cannot move cursor up, nil parent for message: %v", msg)
return nil
} else if len(m.Children(msg.Parent)) < 2 {
log.Println("Refusing to move cursor onto nonexistent sibling", msg.Parent)
return nil
} else {
siblings := m.Children(msg.Parent)
index := indexOf(id, siblings)
if s == right {
index = (index + len(siblings) + 1) % len(siblings)
} else {
index = (index + len(siblings) - 1) % len(siblings)
}
newCursor := siblings[index]
log.Printf("Selecting new cursor (old %s) as %s from %v\n", id, newCursor, siblings)
m.ViewSubtreeOf(newCursor)
log.Println("Selected leaf :", m.LeafID)
return nil
}
}
func (m *History) CursorDown(g *gocui.Gui, v *gocui.View) error {
if m.IsReplying() {
return nil
}
m.MoveCursorTowardLeaf()
return nil
}
func indexOf(element string, inContainer []string) int {
for i, e := range inContainer {
if e == element {
return i
}
}
return -1
}