in autocomplete list moves your name to last item in the list of sorted current users

This commit is contained in:
Pavel Zaitsev 2020-07-24 10:16:29 -04:00
parent 208a6a4712
commit 5ff2ea085b
2 changed files with 12 additions and 4 deletions

View File

@ -226,7 +226,7 @@ func (r *Room) SetTopic(s string) {
// NamesPrefix lists all members' names with a given prefix, used to query
// for autocompletion purposes. Sorted by which user was last active.
func (r *Room) NamesPrefix(prefix string) []string {
func (r *Room) NamesPrefix(prefix string, current_user *message.User) []string {
items := r.Members.ListPrefix(prefix)
// Sort results by recently active
@ -235,6 +235,14 @@ func (r *Room) NamesPrefix(prefix string) []string {
users = append(users, item.Value().(*Member).User)
}
sort.Sort(message.RecentActiveUsers(users))
for i, user := range users {
if user.Name() == current_user.Name() {
// move it to the end. one user in the list?
save := users[0]
copy(users[i:], users[i+1:])
users[len(users)-1] = save
}
}
// Pull out names
names := make([]string, 0, len(items))

View File

@ -243,8 +243,8 @@ func (h *Host) Serve() {
h.listener.Serve()
}
func (h *Host) completeName(partial string) string {
names := h.NamesPrefix(partial)
func (h *Host) completeName(partial string, current_user *message.User) string {
names := h.NamesPrefix(partial, current_user)
if len(names) == 0 {
// Didn't find anything
return ""
@ -300,7 +300,7 @@ func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int,
}
} else {
// Name
completed = h.completeName(partial)
completed = h.completeName(partial, u)
if completed == "" {
return
}