-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (61 loc) · 1.28 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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"log"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func main() {
app := tview.NewApplication()
textView := tview.NewTextView().
SetChangedFunc(func() {
app.Draw()
})
textView.SetBorder(true)
textView.SetTitle("ちゃっと")
inputField := tview.NewInputField()
inputField.SetTitle("にゅうりょく").
SetBorder(true)
cfg := &tls.Config{
InsecureSkipVerify: true,
}
conn, err := tls.Dial("tcp", "koukoku.shadan.open.ad.jp:992", cfg)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
go func() {
r := bufio.NewReader(conn)
for {
line, _, err := r.ReadLine()
if err != nil {
log.Fatal(err)
}
if strings.HasPrefix(string(line), ">>") {
fmt.Fprintf(textView, "%s\n", line)
}
}
}()
inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEnter:
_, err := fmt.Fprintf(conn, "%s\n", inputField.GetText())
if err != nil {
log.Fatal(err)
}
inputField.SetText("")
return nil
}
return event
})
flex := tview.NewFlex()
flex.SetDirection(tview.FlexRow).
AddItem(inputField, 3, 0, true).
AddItem(textView, 0, 1, false)
if err := app.SetRoot(flex, true).Run(); err != nil {
panic(err)
}
}