-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathclient.go
123 lines (105 loc) · 2.29 KB
/
client.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
package main
import (
"bytes"
// "flag"
"fmt"
"github.com/nsf/termbox-go"
"log"
"os"
// "unicode/utf8"
// "strconv"
)
type ChatLog []string
type DeadChatClient struct {
ui_width int
ui_height int
ui_input bytes.Buffer
chatlog ChatLog
chatlog_page int
}
var client DeadChatClient
var logger *log.Logger
func init() {
}
func error_(err error, r int) {
fmt.Printf("Error: %v\n", err)
logger.Printf("Error: %v\n", err)
if r >= 0 {
os.Exit(r)
}
}
func ui_print(x, y int, msg string, fg, bg termbox.Attribute) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x += 1
}
termbox.SetCursor(x, y)
}
func ui_draw_banner(msg string) {
for i := 0; i < client.ui_width; i++ {
termbox.SetCell(i, client.ui_height-2, 0x2588, termbox.ColorCyan, termbox.ColorCyan)
}
ui_print(0, client.ui_height-2, msg, termbox.ColorBlack, termbox.ColorCyan)
}
func ui_draw_chatlog() {
for i, line := range client.chatlog {
ui_print(0, i, line, termbox.ColorWhite, termbox.ColorBlack)
}
}
func ui_draw() {
ui_draw_chatlog()
ui_draw_banner("deadchat")
ui_print(0, client.ui_height-1, ">> " + client.ui_input.String(), termbox.ColorWhite, termbox.ColorBlack)
}
func ui_keypress(ev *termbox.Event) {
switch ev.Key {
case termbox.KeyBackspace, termbox.KeyBackspace2:
len := client.ui_input.Len()
if len > 0 {
client.ui_input.Truncate(len-1)
}
case 10, 13:
line := client.ui_input.String()
client.chatlog = append(client.chatlog, line)
client.ui_input.Reset()
default:
client.ui_input.WriteRune(ev.Ch)
}
}
func ui_loop() {
loop:
for {
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
ui_draw()
termbox.Flush()
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
if ev.Key == termbox.KeyCtrlC {
break loop
}
ui_keypress(&ev)
case termbox.EventResize:
client.ui_width = ev.Width
client.ui_height = ev.Height
case termbox.EventError:
error_(ev.Err, -1)
}
}
}
func main() {
f, err := os.Create("deadchat.log")
if err != nil {
error_(err, -1)
}
logger = log.New(f, "", log.LstdFlags)
logger.Println("Init")
err = termbox.Init()
if err != nil {
error_(err, -1)
}
defer termbox.Close()
client.ui_width, client.ui_height = termbox.Size()
client.chatlog = make([]string, 0, client.ui_height)
client.chatlog_page = 0
ui_loop()
}