commit b4aaf60

Hubert Hirtz  ·  2021-02-18 15:59:01 +0000 UTC
parent 511a95b
Recognize beginning of commands

allows to only type the beginning of a command, like "/t" for "/topic".
1 files changed,  +18, -2
+18, -2
 1@@ -71,7 +71,7 @@ func init() {
 2 			Desc:      "send raw protocol data",
 3 			Handle:    commandDoQuote,
 4 		},
 5-		"R": {
 6+		"REPLY": {
 7 			AllowHome: true,
 8 			MinArgs:   1,
 9 			Usage:     "<message>",
10@@ -314,11 +314,27 @@ func parseCommand(s string) (command, args string) {
11 func (app *App) handleInput(buffer, content string) error {
12 	cmdName, rawArgs := parseCommand(content)
13 
14-	cmd, ok := commands[cmdName]
15+	var chosenCMDName string
16+	var ok bool
17+	for key := range commands {
18+		if cmdName == "" && key != "" {
19+			continue
20+		}
21+		if !strings.HasPrefix(key, cmdName) {
22+			continue
23+		}
24+		if ok {
25+			return fmt.Errorf("ambiguous command %q (could mean %v or %v)", cmdName, chosenCMDName, key)
26+		}
27+		chosenCMDName = key
28+		ok = true
29+	}
30 	if !ok {
31 		return fmt.Errorf("command %q doesn't exist", cmdName)
32 	}
33 
34+	cmd := commands[chosenCMDName]
35+
36 	var args []string
37 	if rawArgs == "" {
38 		args = nil