From 47689ecf9b96d4a86a8960cee69ac8027a540597 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 3 Oct 2019 08:12:50 -0400 Subject: [PATCH] Add help command --- cmdHelp.go | 32 ++++++++++++++++++++++++++++++++ main.go | 27 ++++++++++++--------------- 2 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 cmdHelp.go diff --git a/cmdHelp.go b/cmdHelp.go new file mode 100644 index 0000000..cd3f247 --- /dev/null +++ b/cmdHelp.go @@ -0,0 +1,32 @@ +// +build !rm_basic_commands allcommands helpcmd + +package main + +import ( + "fmt" + "sort" + + "github.com/jroimartin/gocui" +) + +func init() { + command := Command{ + Cmd: []string{"help", "h"}, + Description: "Show information about avaailable commands", + Help: "", + Exec: cmdHelp, + } + + RegisterCommand(command) +} + +func cmdHelp(g *gocui.Gui, cmd []string) { + var helpText string + if len(cmd) == 1 { + sort.Strings(baseCommands) + for _, c := range baseCommands { + helpText = fmt.Sprintf("%s%s%s\t\t%s\n", helpText, cmdPrefix, c, commands[c].Description) + } + } + printToView(g, "Chat", helpText) +} diff --git a/main.go b/main.go index ed544b9..990f407 100644 --- a/main.go +++ b/main.go @@ -11,11 +11,6 @@ import ( "samhofi.us/x/keybase" ) -const cmdPrefix = "/" - -var commands = make(map[string]Command) -var baseCommands = make([]string, 0) - // Configurable section var downloadPath = "/tmp/" var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" @@ -28,6 +23,11 @@ var timeFormat = "15:04" // End configurable section +const cmdPrefix = "/" + +var commands = make(map[string]Command) +var baseCommands = make([]string, 0) + var k = keybase.NewKeybase() var channel keybase.Channel var channels []keybase.Channel @@ -47,7 +47,6 @@ func main() { defer kbtui.Close() kbtui.SetManagerFunc(layout) - printToView(kbtui, "Chat", fmt.Sprintf("Welcome %s!", k.Username)) go populateList(kbtui) go updateChatWindow(kbtui) if err := initKeybindings(kbtui); err != nil { @@ -214,15 +213,8 @@ func layout(g *gocui.Gui) error { } chatView.Autoscroll = true chatView.Wrap = true - fmt.Fprintf(chatView, "Your chats will appear here.\nSupported commands are as follows:\n") - fmt.Fprintln(chatView, "/j $username - Open your chat with $username") - fmt.Fprintln(chatView, "/j $team $channel - Open $channel from $team") - fmt.Fprintln(chatView, "/u $path $title - Uploads file $path with title $title") - fmt.Fprintln(chatView, "/d $msgId $downloadName - Downloads file from $msgId to $DownloadPath/$downloadName") - fmt.Fprintln(chatView, "/r $msgId $reaction - Reacts to $msgId with $reaction reaction can be emoji :+1:") - fmt.Fprintln(chatView, " Can also be used for STRING reactions") - fmt.Fprintln(chatView, "/s - Experimental: View all incoming messages from everywhere.") - fmt.Fprintln(chatView, "/q - Exit") + fmt.Fprintf(chatView, "Welcome %s!\n\nYour chats will appear here.\nSupported commands are as follows:\n\n", k.Username) + RunCommand(g, "help") } if inputView, err3 := g.SetView("Input", maxX/2-maxX/3, maxY-4, maxX-1, maxY-1); err3 != nil { if err3 != gocui.ErrUnknownView { @@ -399,3 +391,8 @@ func RegisterCommand(c Command) error { } return nil } + +// RunCommand calls a command as if it was run by the user +func RunCommand(g *gocui.Gui, c ...string) { + commands[c[0]].Exec(g, c) +}