forked from jpbruinsslot/slack-term
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.go
274 lines (232 loc) · 6.01 KB
/
chat.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
package components
import (
"fmt"
"html"
"sort"
"strings"
"time"
"github.com/erroneousboat/termui"
"github.com/erroneousboat/slack-term/config"
)
type Message struct {
Time time.Time
Name string
Content string
StyleTime string
StyleName string
StyleText string
}
func (m Message) ToString() string {
if (m.Time != time.Time{} && m.Name != "") {
return html.UnescapeString(
fmt.Sprintf(
"[[%s]](%s) [<%s>](%s) [%s](%s)",
m.Time.Format("15:04"),
m.StyleTime,
m.Name,
m.StyleName,
m.Content,
m.StyleText,
),
)
} else {
return html.UnescapeString(
fmt.Sprintf("[%s](%s)", m.Content, m.StyleText),
)
}
}
// Chat is the definition of a Chat component
type Chat struct {
List *termui.List
Offset int
}
// CreateChat is the constructor for the Chat struct
func CreateChatComponent(inputHeight int) *Chat {
chat := &Chat{
List: termui.NewList(),
Offset: 0,
}
chat.List.Height = termui.TermHeight() - inputHeight
chat.List.Overflow = "wrap"
return chat
}
// Buffer implements interface termui.Bufferer
func (c *Chat) Buffer() termui.Buffer {
// Build cells, after every item put a newline
cells := termui.DefaultTxBuilder.Build(
strings.Join(c.List.Items, "\n"),
c.List.ItemFgColor, c.List.ItemBgColor,
)
// We will create an array of Line structs, this allows us
// to more easily render the items in a list. We will range
// over the cells we've created and create a Line within
// the bounds of the Chat pane
type Line struct {
cells []termui.Cell
}
lines := []Line{}
line := Line{}
x := 0
for _, cell := range cells {
if cell.Ch == '\n' {
lines = append(lines, line)
line = Line{}
x = 0
continue
}
if x+cell.Width() > c.List.InnerBounds().Dx() {
lines = append(lines, line)
line = Line{}
x = 0
}
line.cells = append(line.cells, cell)
x++
}
lines = append(lines, line)
// We will print lines bottom up, it will loop over the lines
// backwards and for every line it'll set the cell in that line.
// Offset is the number which allows us to begin printing the
// line above the last line.
buf := c.List.Buffer()
linesHeight := len(lines)
paneMinY := c.List.InnerBounds().Min.Y
paneMaxY := c.List.InnerBounds().Max.Y
currentY := paneMaxY - 1
for i := (linesHeight - 1) - c.Offset; i >= 0; i-- {
if currentY < paneMinY {
break
}
x := c.List.InnerBounds().Min.X
for _, cell := range lines[i].cells {
buf.Set(x, currentY, cell)
x += cell.Width()
}
// When we're not at the end of the pane, fill it up
// with empty characters
for x < c.List.InnerBounds().Max.X {
buf.Set(
x, currentY,
termui.Cell{
Ch: ' ',
Fg: c.List.ItemFgColor,
Bg: c.List.ItemBgColor,
},
)
x++
}
currentY--
}
// If the space above currentY is empty we need to fill
// it up with blank lines, otherwise the List object will
// render the items top down, and the result will mix.
for currentY >= paneMinY {
x := c.List.InnerBounds().Min.X
for x < c.List.InnerBounds().Max.X {
buf.Set(
x, currentY,
termui.Cell{
Ch: ' ',
Fg: c.List.ItemFgColor,
Bg: c.List.ItemBgColor,
},
)
x++
}
currentY--
}
return buf
}
// GetHeight implements interface termui.GridBufferer
func (c *Chat) GetHeight() int {
return c.List.Block.GetHeight()
}
// SetWidth implements interface termui.GridBufferer
func (c *Chat) SetWidth(w int) {
c.List.SetWidth(w)
}
// SetX implements interface termui.GridBufferer
func (c *Chat) SetX(x int) {
c.List.SetX(x)
}
// SetY implements interface termui.GridBufferer
func (c *Chat) SetY(y int) {
c.List.SetY(y)
}
// GetMaxItems return the maximal amount of items can fit in the Chat
// component
func (c *Chat) GetMaxItems() int {
return c.List.InnerBounds().Max.Y - c.List.InnerBounds().Min.Y
}
// SetMessages will put the provided messages into the Items field of the
// Chat view
func (c *Chat) SetMessages(messages []string) {
for _, msg := range messages {
c.List.Items = append(c.List.Items, html.UnescapeString(msg))
}
}
// AddMessage adds a single message to List.Items
func (c *Chat) AddMessage(message string) {
c.List.Items = append(c.List.Items, html.UnescapeString(message))
}
// ClearMessages clear the List.Items
func (c *Chat) ClearMessages() {
c.List.Items = []string{}
}
// ScrollUp will render the chat messages based on the Offset of the Chat
// pane.
//
// Offset is 0 when scrolled down. (we loop backwards over the array, so we
// start with rendering last item in the list at the maximum y of the Chat
// pane). Increasing the Offset will thus result in substracting the offset
// from the len(Chat.List.Items).
func (c *Chat) ScrollUp() {
c.Offset = c.Offset + 10
// Protect overscrolling
if c.Offset > len(c.List.Items)-1 {
c.Offset = len(c.List.Items) - 1
}
}
// ScrollDown will render the chat messages based on the Offset of the Chat
// pane.
//
// Offset is 0 when scrolled down. (we loop backwards over the array, so we
// start with rendering last item in the list at the maximum y of the Chat
// pane). Increasing the Offset will thus result in substracting the offset
// from the len(Chat.List.Items).
func (c *Chat) ScrollDown() {
c.Offset = c.Offset - 10
// Protect overscrolling
if c.Offset < 0 {
c.Offset = 0
}
}
// SetBorderLabel will set Label of the Chat pane to the specified string
func (c *Chat) SetBorderLabel(channelName string) {
c.List.BorderLabel = channelName
}
// Help shows the usage and key bindings in the chat pane
func (c *Chat) Help(cfg *config.Config) {
help := []string{
"slack-term - slack client for your terminal",
"",
"USAGE:",
" slack-term -config [path-to-config]",
"",
"KEY BINDINGS:",
"",
}
for mode, mapping := range cfg.KeyMap {
help = append(help, fmt.Sprintf(" %s", strings.ToUpper(mode)))
help = append(help, "")
var keys []string
for k := range mapping {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
help = append(help, fmt.Sprintf(" %-12s%-15s", k, mapping[k]))
}
help = append(help, "")
}
c.List.Items = help
}