summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2025-06-01 17:15:44 -0500
committercitrons <citrons@mondecitronne.com>2025-06-01 17:15:44 -0500
commitccea376f5750629c2a82b1f24251e52756876f5d (patch)
tree0f47b09325d56625bbae4596ace2a5f63bb5985e /client
parent397b8de9c2a590f98c1a3255a6dbe7570a7b70b8 (diff)
help command
Diffstat (limited to 'client')
-rw-r--r--client/application.go1
-rw-r--r--client/cmd_window.go3
-rw-r--r--client/command.go43
-rw-r--r--client/help.go75
4 files changed, 118 insertions, 4 deletions
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", "<name>", "change your username"},
+ helpText {"join", "<name>", "join the channel with this name"},
+ helpText {"leave", "", "leave the currently shown channel"},
+ helpText {"create", "<name>", "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
+}