summaryrefslogtreecommitdiff
path: root/client/debug_window.go
blob: 7be435f2cf496a7759057ce235c86a0297f98cbd (plain)
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
package main

import (
	"citrons.xyz/talk/client/window"
	"citrons.xyz/talk/tui"
	"citrons.xyz/talk/proto"
	"strings"
	"fmt"
	"bufio"
)

type debugWindow struct {
	window.DefaultWindow
	sendChan chan<- proto.Line
}

type debugWindowLocation struct{}

func (l debugWindowLocation) CreateWindow() window.Window {
	var dw debugWindow
	globalApp.Debug(&dw)
	return &dw
}

type debugLine struct {
	index int
	client bool
	line string
}

func (l debugLine) Id() string {
	return fmt.Sprintf("debug.%d", l.index)
}

func (l debugLine) Show(odd bool) {
	tui.Push("", tui.Box {
		Width: tui.Fill, Height: tui.Children, Dir: tui.Right,
	})

	tui.Push("", tui.Box {
		Width: tui.TextSize, Height: tui.TextSize, Dir: tui.Right,
		Style: &tui.Style {Fg: tui.BrightBlack, Bg: colorDefault[odd]},
	})
	if l.client {
		tui.Text("client: ", nil)
	} else {
		tui.Text("server: ", nil)
	}
	tui.Pop()

	tui.Push("", tui.Box {Width: tui.TextSize, Height: tui.TextSize})
	tui.Text(l.line, nil)
	tui.Pop()

	tui.Pop()
}

func (dw *debugWindow) SetSendChan(send chan<- proto.Line) {
	dw.sendChan = send
}

func (dw *debugWindow) OnLine(line proto.Line, client bool) {
	var sb strings.Builder
	proto.WriteLine(bufio.NewWriter(&sb), line)
	lastIndex++
	dw.Buf.Add(debugLine {lastIndex, client, strings.TrimSpace(sb.String())})
}

func (dw *debugWindow) Send(text string) {
	line, err := proto.ReadLine(bufio.NewReader(strings.NewReader(text + "\n")))
	if err != nil {
		globalApp.cmdWindow.err("that is not a valid protocol line!")
	} else {
		dw.OnLine(line, true)
		dw.sendChan <- line
		lastIndex++
	}
	dw.In.SetText("")
}

func (dw *debugWindow) ShowStatusLine() {
	tui.Text("enter protocol line:", &tui.Style {
		Bg: tui.White, Fg: tui.Black, Italic: true,
	})
}

func (dw *debugWindow) Kill() {
	globalApp.Debug(nil)
}