forked from theepicsnail/goIrcClient
/
main.go
82 lines (72 loc) · 2.06 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
package main
import (
gc "code.google.com/p/goncurses"
"fmt"
"time"
)
const (
ChatAreaColor = iota
InputAreaColor
)
func main() {
scr,_ := gc.Init()
defer gc.End()
rows, cols := scr.Maxyx()
gc.InitPair(ChatAreaColor, gc.C_WHITE, gc.C_BLACK)
gc.InitPair(InputAreaColor, gc.C_BLACK, gc.C_WHITE)
chatArea := scr.Derived(rows-1, cols, 0, 0)
chatArea.SetBackground(gc.Character(' ' | gc.ColorPair(ChatAreaColor)))
inputArea := scr.Derived(1, cols, rows-1, 0)
chatArea.SetBackground(gc.Character(' ' | gc.ColorPair(InputAreaColor)))
chat := make(chan string)
go func() {
for msg := range(chat) {
msg = fmt.Sprintf("%v| %v", time.Now().Format("15:04:05"), msg)
chatArea.Scroll(1)
chatArea.MovePrint(rows - 2, 0, msg)
chatArea.Refresh()
}
}()
defer close(chat)
userInputChan := make(chan string)
go func() {
for msg := range(userInputChan) {
if len(msg) >0 && msg[0] == '/' {
chat <- fmt.Sprintf("Command: %s", msg[1:])
} else {
chat <- msg
}
}
}()
defer close(userInputChan)
gc.Echo(false)
gc.CBreak(true)
gc.Raw(true)
chatArea.ScrollOk(true)
scr.Keypad(true)
chat <- "Welcome to snails shitty chat thing."
chat <- "Press esc to quit, it may or may not break stuff. "
chat <- "If it does, do a 'reset' to fix it."
buffer := ""
for {
chatArea.Refresh()
key := inputArea.GetChar()
switch key {
case gc.Key(27):
return
case gc.KEY_RETURN:
userInputChan <- buffer
buffer = ""
chatArea.Refresh()
case gc.Key(127)://backspace
l := len(buffer)
if l > 0 {
buffer = buffer[:l-1]
}
default:
buffer = fmt.Sprintf("%s%c", buffer, key)
}
inputArea.Clear()
inputArea.MovePrint(0, 0, buffer)
}
}