From 5ff2ea085bba65aa3693ed3d2278cc494e8ddeff Mon Sep 17 00:00:00 2001
From: Pavel Zaitsev <pavel@arslogi.ca>
Date: Fri, 24 Jul 2020 10:16:29 -0400
Subject: [PATCH] in autocomplete list moves your name to last item in the list
 of sorted current users

---
 chat/room.go | 10 +++++++++-
 host.go      |  6 +++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/chat/room.go b/chat/room.go
index 5b34508..12f8ce8 100644
--- a/chat/room.go
+++ b/chat/room.go
@@ -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))
diff --git a/host.go b/host.go
index 1c615c0..808d9fc 100644
--- a/host.go
+++ b/host.go
@@ -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
 			}