From ccea376f5750629c2a82b1f24251e52756876f5d Mon Sep 17 00:00:00 2001 From: citrons Date: Sun, 1 Jun 2025 17:15:44 -0500 Subject: help command --- client/application.go | 1 + client/cmd_window.go | 3 +++ client/command.go | 43 ++++++++++++++++++++++++++--- client/help.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 client/help.go (limited to 'client') diff --git a/client/application.go b/client/application.go index b1c5865..232d61c 100644 --- a/client/application.go +++ b/client/application.go @@ -27,6 +27,7 @@ func newApplication(serverAddress string) *application { app.cache = object.NewCache(&app) app.windowCache = window.NewCache() + app.cmdWindow.info("welcome! type /help for help.") app.cmdWindow.info("connecting to %s", app.Client.Address) return &app } diff --git a/client/cmd_window.go b/client/cmd_window.go index d468385..4a1b664 100644 --- a/client/cmd_window.go +++ b/client/cmd_window.go @@ -87,6 +87,9 @@ func (w *cmdWindow) Send(text string) { } }) } + if text == ":wq" { + globalApp.quit = true + } } func (w *cmdWindow) ShowStatusLine() { diff --git a/client/command.go b/client/command.go index 6727114..4d0d5c1 100644 --- a/client/command.go +++ b/client/command.go @@ -50,23 +50,29 @@ func (a *application) doCommand(command string, args []string, text string) { if !a.connected { return } - argN := func(n int) { + argN := func(n int) bool { if len(args) != n { a.cmdWindow.err( "%s: expected %d arguments, was %d", command, n, len(args), ) + return false } + return true } switch command { case "nick": a.setNick(text) case "join": - argN(1) + if !argN(1) { + break + } if a.authenticated { a.join(args[0]) } case "leave": - argN(0) + if !argN(0) { + break + } win := a.windowCache.Get(a.currentWindow) switch win.(type) { case *channelWindow: @@ -74,10 +80,39 @@ func (a *application) doCommand(command string, args []string, text string) { } a.currentWindow = cmdWindowLocation {} case "create": - argN(1) + if !argN(1) { + break + } if a.authenticated { a.createChannel(args[0]) } + case "help": + var ( + hm helpMsg + ok bool + cmd string + ) + if len(args) == 0 { + hm, ok = getHelp("") + } else { + if !argN(1) { + break + } + cmd = args[0] + if cmd[0] == '/' { + if len(cmd) > 1 { + cmd = cmd[1:] + } else { + cmd = "" + } + } + hm, ok = getHelp(cmd) + } + if !ok { + a.cmdWindow.err("unknown command: /" + cmd) + } else { + a.cmdWindow.buf.Add(hm) + } case "quit": a.quit = true default: diff --git a/client/help.go b/client/help.go new file mode 100644 index 0000000..55509b4 --- /dev/null +++ b/client/help.go @@ -0,0 +1,75 @@ +package main + +import ( + "citrons.xyz/talk/tui" + "strconv" +) + +type helpText struct { + command string + usage string + description string +} +var helpTexts = []helpText { + helpText {"help", "[command]", "view/list command descriptions"}, + helpText {"nick", "", "change your username"}, + helpText {"join", "", "join the channel with this name"}, + helpText {"leave", "", "leave the currently shown channel"}, + helpText {"create", "", "create a channel with this name"}, + helpText {"quit", "", "exit the application"}, +} + +type helpMsg struct { + index int + help []helpText +} + +func (h helpMsg) Id() string { + return "help." + strconv.Itoa(h.index) +} + +func (h helpMsg) Show(odd bool) { + style := &tui.Style {Bg: colorCmd[odd], Fg: tui.White} + tui.Push("", tui.Box { + Width: tui.Fill, Height: tui.Children, Style: style, Dir: tui.Right, + }) + + tui.Push("", tui.Box {Width: tui.TextSize, Height: tui.TextSize}) + tui.Text("* ", nil) + tui.Pop() + + tui.Push("", tui.Box {Width: tui.Fill, Height: tui.Children}) + for _, h := range h.help { + tui.Push("", tui.Box {Width: tui.Fill, Height: tui.TextSize}) + tui.Text("/", nil) + tui.Text(h.command, nil) + tui.Text(" ", nil) + tui.Text(h.usage, nil) + tui.Pop() + + tui.Push("", tui.Box { + Width: tui.Fill, Height: tui.TextSize, + Margins: [4]int {2, 0, 0, 0}, + }) + tui.Text(h.description, nil) + tui.Pop() + } + tui.Pop() + + tui.Pop() +} + + +func getHelp(command string) (helpMsg, bool) { + lastIndex++ + if command == "" { + return helpMsg {lastIndex, helpTexts}, true + } else { + for _, h := range helpTexts { + if h.command == command { + return helpMsg {lastIndex, []helpText {h}}, true + } + } + } + return helpMsg{}, false +} -- cgit v1.2.3