From 8c4e3c81890f26f056eff7b3344f23fed4c1d970 Mon Sep 17 00:00:00 2001 From: citrons Date: Mon, 2 Jun 2025 14:00:25 -0500 Subject: prompt system --- client/application.go | 4 +++- client/login_prompt.go | 37 +++++++++++++++++++++++++++++++++++++ client/ui.go | 39 ++++++++++++++++++++++++++++++++------- client/window/window.go | 4 ++++ 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 client/login_prompt.go (limited to 'client') diff --git a/client/application.go b/client/application.go index 232d61c..68a214d 100644 --- a/client/application.go +++ b/client/application.go @@ -18,6 +18,7 @@ type application struct { windowCache window.WindowCache currentWindow window.Location cmdWindow cmdWindow + prompts []window.Prompt } func newApplication(serverAddress string) *application { @@ -38,13 +39,14 @@ func (a *application) OnConnect() { a.cache = object.NewCache(a) a.windowCache = window.NewCache() a.cmdWindow.info("connected to %s", a.Client.Address) - a.cmdWindow.loginMode() + a.pushPrompt(&loginPrompt{}) } func (a *application) OnDisconnect(err error) { a.connected = false a.authenticated = false a.uid = "" + a.prompts = nil if !a.reconnecting { a.cmdWindow.err( "disconnected from %s: %s\nreconnecting...", a.Client.Address, err, diff --git a/client/login_prompt.go b/client/login_prompt.go new file mode 100644 index 0000000..7e38d30 --- /dev/null +++ b/client/login_prompt.go @@ -0,0 +1,37 @@ +package main + +import ( + "citrons.xyz/talk/tui" +) + +type loginPrompt struct { + input tui.TextInput + username string +} + +func (p *loginPrompt) Input() *tui.TextInput { + return &p.input +} + +func (p *loginPrompt) Send(text string) { + if p.username != "" { + return + } + p.username = text + globalApp.auth(text, func(success bool) { + if success { + globalApp.removePrompt(p) + } else { + p.username = "" + } + }) +} + +func (p *loginPrompt) ShowStatusLine() { + tui.Text("[", nil) + tui.Text("login", &tui.Style { + Bg: tui.White, Fg: tui.Blue, Bold: true, + }) + tui.Text("]", nil) + tui.Text(" username:", nil) +} diff --git a/client/ui.go b/client/ui.go index 023a1bf..740cc54 100644 --- a/client/ui.go +++ b/client/ui.go @@ -15,11 +15,33 @@ func (a *application) getWin() window.Window { return win } +func (a *application) pushPrompt(p window.Prompt) { + a.prompts = append(a.prompts, p) +} + +func (a *application) removePrompt(p window.Prompt) { + for i := len(a.prompts) - 1; i >= 0; i-- { + if i < len(a.prompts) - 1 { + a.prompts = append(a.prompts[:i], a.prompts[i + 1:]...) + } else { + a.prompts = a.prompts[:i] + } + } +} + +func (a *application) getPrompt() window.Prompt { + if len(a.prompts) > 0 { + return a.prompts[len(a.prompts) - 1] + } + return a.getWin() +} + func (a *application) onInput(ev tui.Event) { tui.Selected = "input" win := a.getWin() - win.Input().Update(ev) + prompt := a.getPrompt() + prompt.Input().Update(ev) buf := win.Buffer() buf.Scroll(-ev.Mouse.Scroll * 2) @@ -30,11 +52,11 @@ func (a *application) onInput(ev tui.Event) { case keys.PageDown: buf.Scroll(-scroll) case keys.Enter: - input := win.Input() + input := prompt.Input() if !input.IsEmpty() { is, text := isCommand(input.Text()) if !is { - win.Send(text) + prompt.Send(text) } else { a.processCommand(text) input.SetText("") @@ -68,15 +90,18 @@ func (a *application) show() { a.cmdWindow.showPreview() } + win := a.getWin() + prompt := a.getPrompt() + a.getWin().Buffer().Show("buffer") tui.Push("input border", tui.Box { Width: tui.Fill, Height: 1, Dir: tui.Left, Style: &tui.Style {Bg: tui.White, Fg: tui.Black}, }) - if a.getWin().Buffer().ScrollPos() > 0 { + if win.Buffer().ScrollPos() > 0 { tui.Push("", tui.Box {Width: tui.TextSize, Height: 1, NoWrap: true}) - if a.getWin().Buffer().AtTop() { + if win.Buffer().AtTop() { tui.Text("[TOP]", nil) } else { tui.Text("[MORE]", nil) @@ -87,7 +112,7 @@ func (a *application) show() { tui.Pop() tui.Push("status", tui.Box {Width: tui.TextSize, Height: 1, NoWrap: true}) - a.getWin().ShowStatusLine() + prompt.ShowStatusLine() tui.Pop() tui.Pop() @@ -96,7 +121,7 @@ func (a *application) show() { Width: tui.Fill, Height: tui.Children, Dir: tui.Right, }) a.showNickBox() - a.getWin().Input().Show("input") + prompt.Input().Show("input") tui.Pop() tui.Pop() diff --git a/client/window/window.go b/client/window/window.go index 5ed9a18..ba72026 100644 --- a/client/window/window.go +++ b/client/window/window.go @@ -10,9 +10,13 @@ type Location interface { } type Window interface { + Prompt Location() Location Kill() Buffer() *buffer.Buffer +} + +type Prompt interface { Input() *tui.TextInput Send(text string) ShowStatusLine() -- cgit v1.2.3