-
Notifications
You must be signed in to change notification settings - Fork 6
/
guiwrapper.go
92 lines (77 loc) · 2.15 KB
/
guiwrapper.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
package main
import (
"fmt"
"strings"
"sync"
"time"
"github.com/awesome-gocui/gocui"
)
type guiwrapper struct {
gui *gocui.Gui
messages []*guimessage
maxlines int
timeformat string
sync.RWMutex
}
type guimessage struct {
ts time.Time
tag string // TODO assert len=3 ?
msg string
// TODO right now only set this when we want to modify tag/highlight/ignore/... later on... not on "system" messages; even though they technically have a sender too...
nick string
}
func (gw *guiwrapper) formatMessage(gm *guimessage) string {
formattedDate := gm.ts.Format(gw.timeformat)
return fmt.Sprintf("[%s]%s%s", formattedDate, gm.tag, gm.msg)
}
func (gw *guiwrapper) redraw() {
gw.gui.Update(func(g *gocui.Gui) error {
messageView, err := g.View("messages")
if err != nil {
return err
}
// If we have reached maxlines and the user is currently reading old lines,
// i.e. is in an scrolled-up state, do not redraw, because it makes reading
// messages very hard... Scroll function should make sure to call redraw()
// when the user is done reading old messages.
// This introduces some ugly side-effects, e.g. commands typed into chat like
// /tag will also not be instantly applied when in a scrolled-up state.
// Or if the user is scrolled up for a very long time, once scrolling down
// chat will make a big jump redrawing - All of that probably cannot be helped.
if !messageView.Autoscroll {
return nil
}
gw.Lock()
defer gw.Unlock()
// redraw everything
newbuf := ""
for _, msg := range gw.messages {
newbuf += gw.formatMessage(msg) + "\n"
}
messageView.Clear()
fmt.Fprint(messageView, newbuf)
return nil
})
}
func (gw *guiwrapper) addMessage(m guimessage) {
gw.Lock()
// only keep maxlines lines in memory
delta := len(gw.messages) - gw.maxlines
if delta > 0 {
gw.messages = append(gw.messages[delta:], &m)
} else {
gw.messages = append(gw.messages, &m)
}
gw.Unlock()
gw.redraw()
}
//currently not in use
func (gw *guiwrapper) applyTag(tag string, nick string) {
gw.Lock()
defer gw.Unlock()
for _, m := range gw.messages {
if m.nick != "" && strings.EqualFold(m.nick, nick) {
m.tag = tag
}
}
}