commit 7b559b3

Kalyan Sriram  ·  2021-12-15 02:30:00 +0000 UTC
parent 01863cb
ui: add commands completion
2 files changed,  +31, -0
M app.go
M app.go
+1, -0
1@@ -951,6 +951,7 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
2 		cs = app.completionsChannelMembers(cs, cursorIdx, text)
3 	}
4 	cs = app.completionsMsg(cs, cursorIdx, text)
5+	cs = app.completionsCommands(cs, cursorIdx, text)
6 
7 	if cs != nil {
8 		cs = append(cs, ui.Completion{
+30, -0
 1@@ -99,6 +99,36 @@ func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) [
 2 	return cs
 3 }
 4 
 5+func (app *App) completionsCommands(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
 6+	if !hasPrefix(text, []rune("/")) {
 7+		return cs
 8+	}
 9+	for i := 0; i < cursorIdx; i++ {
10+		if text[i] == ' ' {
11+			return cs
12+		}
13+	}
14+	if cursorIdx < len(text) && text[cursorIdx] != ' ' {
15+		return cs
16+	}
17+
18+	uText := strings.ToUpper(string(text[1:cursorIdx]))
19+	for name, _ := range commands {
20+		if strings.HasPrefix(name, uText) {
21+			c := make([]rune, len(text)+len(name)-len(uText))
22+			copy(c[:1], []rune("/"))
23+			copy(c[1:], []rune(strings.ToLower(name)))
24+			copy(c[1+len(name):], text[cursorIdx:])
25+
26+			cs = append(cs, ui.Completion{
27+				Text:      c,
28+				CursorIdx: 1 + len(name),
29+			})
30+		}
31+	}
32+	return cs
33+}
34+
35 func hasPrefix(s, prefix []rune) bool {
36 	return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)])
37 }