commit fcccab1

delthas  ·  2026-03-11 08:05:47 +0000 UTC
parent a7b00a0
Sort completions and fix duplicates for /msg

Sort /msg completions alphabetically by nick, and command completions
alphabetically by name. Other completion sources were already sorted
(channel members by power level, emoji alphabetically, uploads by name).

Skip channel member completions when input starts with /msg, since
completionsMsg already covers all known users. This fixes duplicate
completions showing up for /msg.

Co-authored-by: Hubert Hirtz <hubert@hirtz.pm>
1 files changed,  +9, -2
+9, -2
 1@@ -2,8 +2,10 @@ package senpai
 2 
 3 import (
 4 	"fmt"
 5+	"maps"
 6 	"os"
 7 	"path/filepath"
 8+	"slices"
 9 	"sort"
10 	"strings"
11 
12@@ -31,6 +33,9 @@ func (m members) Swap(i, j int) {
13 type completionAsync func(e irc.Event) []ui.Completion
14 
15 func (app *App) completionsChannelMembers(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
16+	if hasPrefix(text, []rune("/msg ")) {
17+		return cs
18+	}
19 	var start int
20 	for start = cursorIdx - 1; 0 <= start; start-- {
21 		if text[start] == ' ' {
22@@ -158,7 +163,9 @@ func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) [
23 	if word == "" {
24 		return cs
25 	}
26-	for _, user := range s.Users() {
27+	users := s.Users()
28+	sort.Strings(users)
29+	for _, user := range users {
30 		if strings.HasPrefix(s.Casemap(user), word) {
31 			nickComp := append([]rune(user), ' ')
32 			c := make([]rune, len(text)+5+len(nickComp)-cursorIdx)
33@@ -269,7 +276,7 @@ func (app *App) completionsCommands(cs []ui.Completion, cursorIdx int, text []ru
34 	}
35 
36 	uText := strings.ToUpper(string(text[1:cursorIdx]))
37-	for name := range commands {
38+	for _, name := range slices.Sorted(maps.Keys(commands)) {
39 		if strings.HasPrefix(name, uText) {
40 			c := make([]rune, len(text)+len(name)-len(uText))
41 			copy(c[:1], []rune("/"))