~taiite/public-inbox

senpai: ui: add commands completion v1 PROPOSED

Kalyan Sriram: 1
 ui: add commands completion

 2 files changed, 21 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~taiite/public-inbox/patches/27401/mbox | git am -3
Learn more about email & git

[PATCH senpai] ui: add commands completion Export this patch

---
 app.go         |  1 +
 completions.go | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/app.go b/app.go
index 1144560..4706536 100644
--- a/app.go
+++ b/app.go
@@ -938,6 +938,7 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
		cs = app.completionsChannelMembers(cs, cursorIdx, text)
	}
	cs = app.completionsMsg(cs, cursorIdx, text)
	cs = app.completionsCommands(cs, cursorIdx, text)

	if cs != nil {
		cs = append(cs, ui.Completion{
diff --git a/completions.go b/completions.go
index 3e4a516..6f99a60 100644
--- a/completions.go
+++ b/completions.go
@@ -99,6 +99,26 @@ func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) [
	return cs
}

func (app *App) completionsCommands(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
	if !hasPrefix(text, []rune("/")) {
		return cs
	}
	uText := strings.ToUpper(string(text[1:]))
	for name, _ := range commands {
		if strings.HasPrefix(name, uText) {
			c := make([]rune, 1+len(name))
			copy(c[:1], []rune("/"))
			copy(c[1:], []rune(name))

			cs = append(cs, ui.Completion{
				Text:      c,
				CursorIdx: 1 + len(name),
			})
		}
	}
	return cs
}

func hasPrefix(s, prefix []rune) bool {
	return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)])
}
-- 
2.34.1
Thanks for the patch. It works well, just an edge case that needs to be 
handled:

1. type "/m #channel +b"
2. place cursor after "/m"
3. press tab

You'll need to take the first word of "text" only, and append the rest 
of "text" to each completion.