-
Notifications
You must be signed in to change notification settings - Fork 0
/
tui.go
316 lines (274 loc) · 7.9 KB
/
tui.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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/azillion/chord/internal/getconfig"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
"github.com/marcusolsson/tui-go"
"github.com/sirupsen/logrus"
)
type post struct {
ID int
username string
message string
time string
}
// ByID implements sort.Interface for []post based on
// the ID field.
type ByID []post
func (id ByID) Len() int { return len(id) }
func (id ByID) Swap(i, j int) { id[i], id[j] = id[j], id[i] }
func (id ByID) Less(i, j int) bool { return id[i].ID < id[j].ID }
func (p *post) messageToPost(message *discordgo.Message) error {
p.username = message.Author.String()
p.message = message.ContentWithMentionsReplaced()
parsedTime, err := message.Timestamp.Parse()
if err != nil {
return err
}
p.time = parsedTime.Format("15:04")
return nil
}
// Channel Channel being displayed by TUI
var (
channel *discordgo.Channel
posts []post
tuiDS *discordgo.Session
history = tui.NewVBox()
historyScroll = tui.NewScrollArea(history)
historyBox = tui.NewVBox(historyScroll)
input = tui.NewEntry()
inputBox = tui.NewHBox(input)
chat = tui.NewVBox(historyBox, inputBox)
root = tui.NewHBox(chat)
ui tui.UI
postCount = 0
lastAck = new(discordgo.Ack)
)
const (
tuiHelp = `TUI for sending and receiving Discord DMs`
// messagesFetchLimit is the limit that discordgo returns of old messages. Max is 100
messagesFetchLimit = 100
)
func init() {
localUI, err := tui.New(root)
if err != nil {
panic(err, tuiDS)
}
ui = localUI
}
func (cmd *tuiCommand) Name() string { return "tui" }
func (cmd *tuiCommand) Args() string { return "[OPTIONS]" }
func (cmd *tuiCommand) ShortHelp() string { return tuiHelp }
func (cmd *tuiCommand) LongHelp() string { return tuiHelp }
func (cmd *tuiCommand) Hidden() bool { return false }
func (cmd *tuiCommand) Register(fs *flag.FlagSet) {
fs.IntVar(&cmd.cSel, "c", -1, "specify the channel to start the tui in")
}
type tuiCommand struct {
cSel int
}
func (cmd *tuiCommand) Run(ctx context.Context, args []string) error {
StartTUI(cmd.cSel)
return nil
}
// Error Handler
func panic(err error, ds *discordgo.Session) {
fmt.Printf("%v\n", err)
tuiDS.Close()
os.Exit(1)
}
// StartTUI Start the TUI display
func StartTUI(cSel int) {
// sidebar := tui.NewVBox(
// tui.NewLabel("CHANNELS"),
// tui.NewLabel("general"),
// tui.NewLabel("random"),
// tui.NewLabel(""),
// tui.NewLabel("DIRECT MESSAGES"),
// tui.NewLabel("slackbot"),
// tui.NewSpacer(),
// )
// sidebar.SetBorder(true)
tuiDS, err := createDiscordSession(getconfig.AuthConfig{})
if err != nil {
logrus.Debugf("Session Failed \n%v\nexiting.", spew.Sdump(tuiDS))
err = fmt.Errorf("You may need to login from a browser first or check your credentials\n%v", err)
panic(err, tuiDS)
}
// Get a list of DM channels
channels, err := tuiDS.UserChannels()
logrus.Debugf("Retrieved Channels\n%v\n", spew.Sdump(channels))
if err != nil {
panic(err, tuiDS)
}
channelSel := cSel
if channelSel <= -1 {
// Display available channels
fmt.Println("Available Private Channels:")
for i, chann := range channels {
logrus.Debugf("Channel %d\n%v\n", i, spew.Sdump(chann))
// Switch of supported channel types
switch chanType := chann.Type; chanType {
case 1: // Direct Messages
var flatRecipients string
recipients := chann.Recipients
logrus.Debugf("Channel %d recipients\n%v\n", i, spew.Sdump(recipients))
for _, recipient := range recipients {
flatRecipients = fmt.Sprintf("%s %s", flatRecipients, recipient.Username)
}
fmt.Printf("\t%d) DM to %s\n", i, strings.TrimSpace(flatRecipients))
default:
fmt.Println("No available channels")
os.Exit(0)
}
}
// Get channel selection
reader := bufio.NewReader(os.Stdin)
fmt.Print("Select a channel to switch to: ")
channelSelS, err := reader.ReadString('\n')
if err != nil {
panic(err, tuiDS)
}
channelSel, err = strconv.Atoi(strings.TrimSpace(channelSelS))
if err != nil {
panic(err, tuiDS)
}
}
// Set Channel to selected channel
if (len(channels) - 1) < channelSel {
err := fmt.Errorf("Channel selection outside of available selection range\n%d < %d", (len(channels) - 1), channelSel)
panic(err, tuiDS)
}
channel = channels[channelSel]
logrus.Debugf("Channel selected %d\n%v\n", channelSel, spew.Sdump(channel))
// Get Channel messages
messages, err := tuiDS.ChannelMessages(channel.ID, messagesFetchLimit, "", "", "")
if err != nil {
panic(err, tuiDS)
}
logrus.Debugf("Channel messages\n%v\n", spew.Sdump(messages))
// spew.Dump(messages[0])
// Convert messages to posts
posts, err := convertToTUIPosts(messages)
if err != nil {
panic(err, tuiDS)
}
// // sort posts by id
// sort.Sort(ByID(posts))
// logrus.Debugf("# of Channel messages\n%d\n", len(posts))
// history defined above as the location to display messages
// history = tui.NewVBox()
// Add posts to history box
addPostsToDisplay(posts)
// historyScroll := tui.NewScrollArea(history)
historyScroll.SetAutoscrollToBottom(true)
// historyBox := tui.NewVBox(historyScroll)
historyBox.SetBorder(true)
// input := tui.NewEntry()
input.SetFocused(true)
input.SetSizePolicy(tui.Expanding, tui.Maximum)
// inputBox := tui.NewHBox(input)
inputBox.SetBorder(true)
inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)
// chat := tui.NewVBox(historyBox, inputBox)
chat.SetSizePolicy(tui.Expanding, tui.Expanding)
input.OnSubmit(func(e *tui.Entry) {
_, err := tuiDS.ChannelMessageSend(channel.ID, e.Text())
if err != nil {
panic(err, tuiDS)
}
// p, err := convertToTUIPost(message)
// if err != nil {
// panic(err, tuiDS)
// }
// addPostToDisplay(p)
input.SetText("")
})
// root := tui.NewHBox(chat)
// ui, err := tui.New(root)
// if err != nil {
// panic(err, tuiDS)
// }
ui.SetKeybinding("Esc", func() { ui.Quit() })
// Register the messageCreate func as a callback for MessageCreate events.
tuiDS.AddHandler(messageCreate)
// Open a websocket connection to Discord and begin listening.
err = tuiDS.Open()
if err != nil {
err = fmt.Errorf("error opening connection, %v", err)
panic(err, tuiDS)
}
if err := ui.Run(); err != nil {
panic(err, tuiDS)
}
// Cleanly close down the Discord session.
tuiDS.Close()
}
// Add posts to the history box (defined above)
func addPostsToDisplay(ps []post) {
// sort posts by id
sort.Sort(sort.Reverse(ByID(ps)))
logrus.Debugf("# of Channel messages\n%d\n", len(ps))
for _, p := range ps {
p.message = strconv.QuoteToASCII(p.message)
history.Append(tui.NewHBox(
tui.NewLabel(p.time),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("<%s>", p.username))),
tui.NewLabel(p.message),
tui.NewSpacer(),
))
}
}
func addPostToDisplay(p post) {
p.message = strconv.QuoteToASCII(p.message)
ui.Update(func() {
history.Append(tui.NewHBox(
tui.NewLabel(p.time),
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("<%s>", p.username))),
tui.NewLabel(p.message),
tui.NewSpacer(),
))
})
}
// Convert discordgo messages to posts
func convertToTUIPosts(messages []*discordgo.Message) ([]post, error) {
var ps []post
for _, message := range messages {
p, err := convertToTUIPost(message)
if err != nil {
return ps, err
}
ps = append(ps, p)
}
return ps, nil
}
// Convert discordgo messages to posts
func convertToTUIPost(message *discordgo.Message) (post, error) {
p := post{ID: postCount}
if err := p.messageToPost(message); err != nil {
return p, err
}
postCount++
return p, nil
}
// New message event handler
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
newAck, err := s.ChannelMessageAck(m.Message.ChannelID, m.Message.ID, lastAck.Token)
if err != nil {
panic(err, tuiDS)
}
lastAck = newAck
p, err := convertToTUIPost(m.Message)
if err != nil {
panic(err, tuiDS)
}
addPostToDisplay(p)
}