-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
329 lines (285 loc) · 6.33 KB
/
window.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
package squirssi
import (
"bytes"
"fmt"
"io"
"regexp"
"sort"
"strings"
"sync"
"time"
"code.dopame.me/veonik/squircy3/event"
)
type Window interface {
io.Writer
io.StringWriter
// Title of the Window.
Title() string
// Lines returns the contents of the Window.
Lines() []string
// CurrentLine returns the bottom-most visible line number, or negative to indicate
// the window is pinned to the end of input.
CurrentLine() int
// ScrollTo sets the current line to pos. Set to negative to pin to the end of input.
ScrollTo(pos int)
// AutoScroll returns true if automatic scrolling is currently active.
AutoScroll() bool
// Touch clears the activity indicator for the window, it it's set.
Touch()
// HasActivity returns true if the Window has new lines since the last touch.
HasActivity() bool
// Notice set the notice indicator for this Window.
Notice()
// HasNotice returns true if the Window has new lines considered important since last touch.
HasNotice() bool
// padding returns how many characters wide the left gutter of the window is.
padding() int
}
type WindowWithUserList interface {
Window
// UserList returns the styled list of users.
UserList() []string
// Users returns just the usernames in the window.
Users() []string
// HasUser returns true if the window contains the given user.
HasUser(name string) bool
// UpdateUser updates the user to a new name in the current window.
UpdateUser(name, newNew string) bool
// DeleteUser removes the user from the current window.
DeleteUser(name string) bool
}
type bufferedWindow struct {
name string
lines []string
current int
hasUnseen bool
hasNotice bool
autoScroll bool
events *event.Dispatcher
mu sync.RWMutex
}
func newBufferedWindow(name string, events *event.Dispatcher) bufferedWindow {
return bufferedWindow{
name: name,
events: events,
current: -1,
autoScroll: true,
}
}
func (c *bufferedWindow) padding() int {
return 10
}
func (c *bufferedWindow) Title() string {
return c.name
}
func (c *bufferedWindow) Write(p []byte) (n int, err error) {
c.mu.Lock()
defer c.events.Emit("ui.DIRTY", map[string]interface{}{
"name": c.name,
})
defer c.mu.Unlock()
lines := bytes.Split(p, []byte("\n"))
t := time.Now().Format("[15:04](fg:gray) ")
const padding = " "
firstWritten := false
for _, l := range lines {
if len(l) == 0 {
continue
}
if !firstWritten {
c.lines = append(c.lines, strings.TrimRight(t+string(l), "\n"))
firstWritten = true
} else {
c.lines = append(c.lines, strings.TrimRight(padding+string(l), "\n"))
}
}
c.hasUnseen = true
return len(p), nil
}
func (c *bufferedWindow) WriteString(p string) (n int, err error) {
return c.Write([]byte(p))
}
func (c *bufferedWindow) Touch() {
c.mu.Lock()
defer c.mu.Unlock()
c.hasUnseen = false
c.hasNotice = false
}
func (c *bufferedWindow) Notice() {
c.mu.Lock()
defer c.mu.Unlock()
c.hasNotice = true
}
func (c *bufferedWindow) HasNotice() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.hasNotice
}
func (c *bufferedWindow) HasActivity() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.hasUnseen && !c.hasNotice
}
func (c *bufferedWindow) AutoScroll() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.autoScroll
}
func (c *bufferedWindow) Lines() []string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.lines
}
func (c *bufferedWindow) CurrentLine() int {
c.mu.RLock()
defer c.mu.RUnlock()
if c.autoScroll {
return len(c.lines) - 1
}
return c.current
}
func (c *bufferedWindow) ScrollTo(pos int) {
c.mu.Lock()
defer c.mu.Unlock()
c.current = pos
if pos < 0 {
c.autoScroll = true
} else {
c.autoScroll = false
}
}
type StatusWindow struct {
bufferedWindow
}
func (c *StatusWindow) padding() int {
return 6
}
func (c *StatusWindow) Title() string {
return "status"
}
type User struct {
string
modes string
}
func SomeUser(c string) User {
u := User{}
u.string = strings.ReplaceAll(strings.ReplaceAll(c, "@", ""), "+", "")
r := regexp.MustCompile("[^@+%]")
u.modes = r.ReplaceAllString(c, "")
return u
}
func (u User) String() string {
m := ""
if u.modes == "@" {
m = "[@](fg:cyan)"
} else if u.modes == "+" {
m = "[+](fg:yellow)"
}
return fmt.Sprintf("%s%s", m, u.string)
}
type Channel struct {
bufferedWindow
topic string
modes string
users []User
}
func (c *Channel) Topic() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.topic
}
func (c *Channel) Modes() string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.modes
}
func (c *Channel) SetUsers(users []string) {
c.mu.Lock()
defer c.mu.Unlock()
r := make([]User, len(users))
for i, u := range users {
r[i] = SomeUser(u)
}
c.users = r
}
func (c *Channel) Users() []string {
c.mu.RLock()
defer c.mu.RUnlock()
t := make([]string, len(c.users))
for i, u := range c.users {
t[i] = u.string
}
return t
}
func (c *Channel) UserList() []string {
c.mu.RLock()
defer c.mu.RUnlock()
t := make([]User, len(c.users))
copy(t, c.users)
sort.SliceStable(t, func(i, j int) bool {
ui := t[i]
uj := t[j]
if ui.modes == "@" && uj.modes != "@" {
return true
} else if uj.modes == "@" && ui.modes != "@" {
return false
}
if ui.modes == "+" && uj.modes != "+" {
return true
} else if uj.modes == "+" && ui.modes != "+" {
return false
}
return strings.Compare(ui.string, uj.string) < 0
})
res := make([]string, len(c.users))
for i, u := range t {
res[i] = u.String()
}
return res
}
func (c *Channel) userIndex(name string) int {
for i := 0; i < len(c.users); i++ {
if c.users[i].string == name {
return i
}
}
return -1
}
func (c *Channel) AddUser(user User) {
c.mu.Lock()
defer c.mu.Unlock()
if idx := c.userIndex(user.string); idx >= 0 {
c.users[idx].modes = user.modes
return
}
c.users = append(c.users, user)
}
func (c *Channel) UpdateUser(name, newName string) bool {
c.mu.Lock()
defer c.mu.Unlock()
if idx := c.userIndex(name); idx >= 0 {
c.users[idx].string = newName
return true
}
return false
}
func (c *Channel) DeleteUser(name string) bool {
c.mu.Lock()
defer c.mu.Unlock()
if idx := c.userIndex(name); idx >= 0 {
c.users = append(c.users[:idx], c.users[idx+1:]...)
return true
}
return false
}
func (c *Channel) HasUser(name string) bool {
c.mu.RLock()
defer c.mu.RUnlock()
if idx := c.userIndex(name); idx >= 0 {
return true
}
return false
}
type DirectMessage struct {
bufferedWindow
}