From fad6126fb15a70396c34bad3b9604379cb78ce33 Mon Sep 17 00:00:00 2001 From: citrons Date: Mon, 2 Jun 2025 16:35:45 -0500 Subject: debugger --- client/client/client.go | 31 ++++++++++++++++++- client/command.go | 2 ++ client/debug_window.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 client/debug_window.go diff --git a/client/client/client.go b/client/client/client.go index bc18035..0b24175 100644 --- a/client/client/client.go +++ b/client/client/client.go @@ -14,6 +14,11 @@ type MessageHandler interface { OnResponse(requestId string, cmd proto.Command) } +type Debugger interface { + SetSendChan(chan<- proto.Line) + OnLine(proto.Line, bool) +} + type Message struct { action func(MessageHandler) } @@ -24,6 +29,8 @@ type Client struct { message chan Message send chan proto.Line activeRequests map[string]func(proto.Command) + debuggerAttach chan Debugger + debugger Debugger } func New(address string) Client { @@ -32,6 +39,7 @@ func New(address string) Client { stop: make(chan struct{}), message: make(chan Message), activeRequests: make(map[string]func(proto.Command)), + debuggerAttach: make(chan Debugger), } } @@ -83,9 +91,15 @@ func (c *Client) RunClient() { mh.OnConnect() }} + var debugger Debugger run: for { select { case line := <-recv: + if debugger != nil { + c.message <- Message {func(mh MessageHandler) { + debugger.OnLine(line, false) + }} + } switch line.Kind { case '*': c.message <- Message {func(mh MessageHandler) { @@ -101,6 +115,12 @@ func (c *Client) RunClient() { }} delete(c.activeRequests, line.RequestId) } + case debugger = <-c.debuggerAttach: + if debugger != nil { + c.message <- Message {func(mh MessageHandler) { + debugger.SetSendChan(c.send) + }} + } case err = <-recvErr: break run case err = <-sendErr: @@ -127,10 +147,19 @@ func (c *Client) Messages() <-chan Message { func (c *Client) Request(cmd proto.Command, callback func(proto.Command)) { id := proto.GenId() - c.send <- proto.Line {'?', id, cmd} + line := proto.Line {'?', id, cmd} + c.send <- line + if c.debugger != nil { + c.debugger.OnLine(line, true) + } c.activeRequests[id] = callback } func (m Message) Handle(mh MessageHandler) { m.action(mh) } + +func (c *Client) Debug(db Debugger) { + c.debugger = db + c.debuggerAttach <- db +} diff --git a/client/command.go b/client/command.go index 3122dc6..253f3d6 100644 --- a/client/command.go +++ b/client/command.go @@ -140,6 +140,8 @@ func (a *application) doCommand(command string, args []string, text string) { } else { a.cmdWindow.Buffer().Add(hm) } + case "debug": + a.currentWindow = debugWindowLocation{} case "quit": a.quit = true default: diff --git a/client/debug_window.go b/client/debug_window.go new file mode 100644 index 0000000..c45198d --- /dev/null +++ b/client/debug_window.go @@ -0,0 +1,82 @@ +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.sendChan <- line + lastIndex++ + } + dw.In.SetText("") +} + +func (dw *debugWindow) Kill() { + globalApp.Debug(nil) +} -- cgit v1.2.3