This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
222 lines (188 loc) · 5.59 KB
/
main.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
package main
import (
"fmt"
"github.com/mattn/go-runewidth"
"github.com/rivo/uniseg"
"log"
"math/rand"
"time"
irc "github.com/fluffle/goirc/client"
"github.com/nsf/termbox-go"
"github.com/sosodev/twitchChatCLI/state"
"github.com/sosodev/twitchChatCLI/twitch"
"os"
"strings"
)
var (
userInput string
)
func main() {
// setup the debugging log if requested
if os.Getenv("debug") == "true" {
logFile, err := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("failed to open log file: %s", err))
}
defer logFile.Close()
log.SetOutput(logFile)
}
// seed the random number generator
rand.Seed(time.Now().UTC().UnixNano())
// grab the channel name from the input args
if len(os.Args) < 2 {
fmt.Println("usage: twitchChatCli [channel-name]")
os.Exit(1)
}
channel := "#" + strings.ToLower(os.Args[1])
// initalize termbox (the thing that renders stuff in the terminal)
if err := termbox.Init(); err != nil {
panic(fmt.Sprintf("failed to initialize termbox: %s", err))
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
// creating the twitch object actually handles the Twitch authentication stuff
twitch, err := twitch.New()
if err != nil {
panic(fmt.Sprintf("failed to get authorization from Twitch: %s", err))
}
username, err := twitch.Username()
if err != nil {
panic(fmt.Sprintf("failed to get username from Twitch: %s", err))
}
username = strings.ToLower(username)
// get the IRC config
cfg, err := twitch.IrcConfig()
if err != nil {
panic(fmt.Sprintf("failed to initialize IRC configuration: %s", err))
}
// create the IRC client
client := irc.Client(cfg)
client.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) {
conn.Join(channel)
})
// tbPrint prints a string with termbox
tbPrint := func(x int, y int, foreground termbox.Attribute, background termbox.Attribute, message string) {
condition := runewidth.NewCondition()
condition.StrictEmojiNeutral = false
graphemes := uniseg.NewGraphemes(message)
for graphemes.Next() {
for _, graphemeRune := range graphemes.Runes() {
termbox.SetCell(x, y, graphemeRune, foreground, background)
w := runewidth.RuneWidth(graphemeRune)
if w == 0 || (w == 2 && runewidth.IsAmbiguousWidth(graphemeRune)) {
w = 1
}
x += w
}
}
}
// drawLine draws a state.ChatLine on the given y line
drawLine := func(line state.ChatLine, y int) {
// the nick portion of the line can either be "nick: " or indented " "
nick := func() string {
if line.ShowNick {
return fmt.Sprintf("%s: ", line.Nick)
}
emptySpace := " "
for range line.Nick {
emptySpace += " "
}
return emptySpace
}()
// draw the nick portion of the line
tbPrint(1, y, line.NickColor, termbox.ColorDefault, nick)
var foreground termbox.Attribute
background := termbox.ColorDefault
if strings.Contains(line.Line, "@"+username) {
foreground = termbox.ColorBlack
background = termbox.ColorWhite
} else {
foreground = termbox.ColorWhite
}
// draw the body of the line
tbPrint(len(nick)+1, y, foreground, background, line.Line)
}
// drawClient is a function that... draws the client
drawClient := func() {
// clear the terminal
err = termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
if err != nil {
panic(fmt.Sprintf("failed to clear termbox buffer: %s\n", err))
}
err = termbox.Flush()
if err != nil {
panic(fmt.Sprintf("failed to flush termbox buffer: %s\n", err))
}
width, height := termbox.Size()
// draw the escape message
if os.Getenv("CLEAN") != "true" {
tbPrint(width-18, 0, termbox.ColorLightBlue, termbox.ColorDefault, "Press ESC to quit")
}
// iterate through all of the lines in reverse (to draw them newest to oldest)
state.ReverseEachLine(func(position int, line state.ChatLine) {
subtract := 2
if os.Getenv("CLEAN") == "true" {
subtract = 1
}
// no pointless iteration here
if position > height-subtract {
return
}
drawLine(line, height-subtract-position)
})
// draw the user's input at the bottom of the terminal
if os.Getenv("CLEAN") != "true" {
tbPrint(1, height-1, state.NickColor(username), termbox.ColorDefault, fmt.Sprintf("%s: %s", username, userInput))
}
// flush the termbox buffer to the terminal
err = termbox.Flush()
if err != nil {
panic(fmt.Sprintf("failed to flush termbox: %s\n", err))
}
}
// handle incoming messages by adding them to the state and redrawing the client
client.HandleFunc(irc.PRIVMSG, func(conn *irc.Conn, line *irc.Line) {
state.NewMessage(line.Nick, true, line.Args[1])
drawClient()
})
// startup ye olde IRC client
if err := client.Connect(); err != nil {
panic(fmt.Sprintf("failed to connect to irc: %s", err))
}
// do an inital drawing
drawClient()
// big event handler loop
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
os.Exit(0)
case termbox.KeySpace:
userInput += " "
case termbox.KeyEnter:
if len(userInput) > 2 {
state.NewMessage(username, true, userInput)
client.Privmsg(channel, userInput)
userInput = ""
}
case termbox.KeyBackspace2:
if len(userInput) > 0 {
userInput = userInput[0 : len(userInput)-1]
}
case termbox.KeyBackspace:
if len(userInput) > 0 {
userInput = userInput[0 : len(userInput)-1]
}
default:
if ev.Ch != 0 {
userInput += string(ev.Ch)
}
}
case termbox.EventError:
panic(fmt.Sprintf("termbox event error: %s", ev.Err.Error()))
}
// draw the client after every handled event
drawClient()
}
}