diff options
author | Tulir Asokan <tulir@maunium.net> | 2020-07-30 14:34:49 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2020-07-30 14:34:49 +0300 |
commit | 0d12947b1f70745d8023c89e22432f2a1353dfbb (patch) | |
tree | 048d98d1484851caaffc3c5463619442d8fee734 /ui/command-processor.go | |
parent | ecdd4f08cb262c8f0c988209e6296c068e1d4cf3 (diff) | |
parent | 2f5f0674b600f129204958c810843d998f6a2f6a (diff) |
Merge branch 'verification'
Fixes #160
Fixes #161
Diffstat (limited to 'ui/command-processor.go')
-rw-r--r-- | ui/command-processor.go | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/ui/command-processor.go b/ui/command-processor.go index b8f41a2..514d67b 100644 --- a/ui/command-processor.go +++ b/ui/command-processor.go @@ -20,6 +20,8 @@ import ( "fmt" "strings" + "github.com/mattn/go-runewidth" + "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/interface" @@ -45,6 +47,8 @@ type Command struct { OrigText string } +type CommandAutocomplete Command + func (cmd *Command) Reply(message string, args ...interface{}) { cmd.Room.AddServiceMessage(fmt.Sprintf(message, args...)) cmd.UI.Render() @@ -60,12 +64,15 @@ func (alias *Alias) Process(cmd *Command) *Command { } type CommandHandler func(cmd *Command) +type CommandAutocompleter func(cmd *CommandAutocomplete) (completions []string, newText string) type CommandProcessor struct { gomuksPointerContainer aliases map[string]*Alias commands map[string]CommandHandler + + autocompleters map[string]CommandAutocompleter } func NewCommandProcessor(parent *MainView) *CommandProcessor { @@ -97,6 +104,12 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor { "dl": {"download"}, "o": {"open"}, }, + autocompleters: map[string]CommandAutocompleter{ + "devices": autocompleteDevice, + "device": autocompleteDevice, + "verify": autocompleteDevice, + "unverify": autocompleteDevice, + }, commands: map[string]CommandHandler{ "unknown-command": cmdUnknownCommand, @@ -139,7 +152,13 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor { "cprof": cmdCPUProfile, "trace": cmdTrace, - "fingerprint": cmdFingerprint, + "fingerprint": cmdFingerprint, + "devices": cmdDevices, + "verify": cmdVerify, + "device": cmdDevice, + "unverify": cmdUnverify, + "blacklist": cmdBlacklist, + "reset-session": cmdResetSession, }, } } @@ -169,6 +188,47 @@ func (ch *CommandProcessor) ParseCommand(roomView *RoomView, text string) *Comma } } +func (ch *CommandProcessor) Autocomplete(roomView *RoomView, text string, cursorOffset int) ([]string, string, bool) { + var completions []string + if cmd := (*CommandAutocomplete)(ch.ParseCommand(roomView, text)); cmd == nil { + return completions, text, false + } else if handler, ok := ch.autocompleters[cmd.Command]; !ok { + return completions, text, false + } else if cursorOffset != runewidth.StringWidth(text) { + return completions, text, false + } else { + completions, newText := handler(cmd) + if newText == "" { + newText = text + } + return completions, newText, true + } +} + +func (ch *CommandProcessor) AutocompleteCommand(word string) (completions []string) { + if word[0] != '/' { + return + } + word = word[1:] + for alias := range ch.aliases { + if alias == word { + return []string{"/" + alias} + } + if strings.HasPrefix(alias, word) { + completions = append(completions, "/"+alias) + } + } + for command := range ch.commands { + if command == word { + return []string{"/" + command} + } + if strings.HasPrefix(command, word) { + completions = append(completions, "/"+command) + } + } + return +} + func (ch *CommandProcessor) HandleCommand(cmd *Command) { defer debug.Recover() if cmd == nil { |