mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-13 07:37:17 +03:00
updated tests, moved code closer to the caller.
* addded condition for zero time on lastMsg. * removed extra paramter in NamePrefix * moved code from NamePrefix to completeName * removed extra parameter in tests calling to NamePrefix
This commit is contained in:
parent
e5374b7111
commit
5885f7fbdd
@ -258,5 +258,11 @@ func (a RecentActiveUsers) Less(i, j int) bool {
|
||||
defer a[i].mu.Unlock()
|
||||
a[j].mu.Lock()
|
||||
defer a[j].mu.Unlock()
|
||||
return a[i].lastMsg.After(a[j].lastMsg)
|
||||
|
||||
if a[i].lastMsg.IsZero() {
|
||||
return a[i].joined.Before(a[j].joined)
|
||||
} else {
|
||||
return a[i].lastMsg.After(a[j].lastMsg)
|
||||
}
|
||||
|
||||
}
|
||||
|
10
chat/room.go
10
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, current_user *message.User) []string {
|
||||
func (r *Room) NamesPrefix(prefix string) []string {
|
||||
items := r.Members.ListPrefix(prefix)
|
||||
|
||||
// Sort results by recently active
|
||||
@ -235,14 +235,6 @@ func (r *Room) NamesPrefix(prefix string, current_user *message.User) []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))
|
||||
|
@ -394,20 +394,20 @@ func TestRoomNamesPrefix(t *testing.T) {
|
||||
members[3].HandleMsg(message.NewMsg("hi")) // foo
|
||||
members[1].HandleMsg(message.NewMsg("hi")) // aab
|
||||
|
||||
if got, want := r.NamesPrefix("a", members[3].User), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) {
|
||||
if got, want := r.NamesPrefix("a"), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got: %q; want: %q", got, want)
|
||||
}
|
||||
|
||||
members[2].HandleMsg(message.NewMsg("hi")) // aac
|
||||
if got, want := r.NamesPrefix("a", members[3].User), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) {
|
||||
if got, want := r.NamesPrefix("a"), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got: %q; want: %q", got, want)
|
||||
}
|
||||
|
||||
if got, want := r.NamesPrefix("f", members[0].User), []string{"foo"}; !reflect.DeepEqual(got, want) {
|
||||
if got, want := r.NamesPrefix("f"), []string{"foo"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got: %q; want: %q", got, want)
|
||||
}
|
||||
|
||||
if got, want := r.NamesPrefix("bar", members[3].User), []string{}; !reflect.DeepEqual(got, want) {
|
||||
if got, want := r.NamesPrefix("bar"), []string{}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got: %q; want: %q", got, want)
|
||||
}
|
||||
}
|
||||
|
15
host.go
15
host.go
@ -243,14 +243,19 @@ func (h *Host) Serve() {
|
||||
h.listener.Serve()
|
||||
}
|
||||
|
||||
func (h *Host) completeName(partial string, current_user *message.User) string {
|
||||
names := h.NamesPrefix(partial, current_user)
|
||||
func (h *Host) completeName(partial string, skipName string) string {
|
||||
names := h.NamesPrefix(partial)
|
||||
if len(names) == 0 {
|
||||
// Didn't find anything
|
||||
return ""
|
||||
} else if name := names[0]; name != skipName {
|
||||
// First name is not the skipName, great
|
||||
return name
|
||||
} else if len(names) > 1 {
|
||||
// Next candidate
|
||||
return names[1]
|
||||
}
|
||||
|
||||
return names[len(names)-1]
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *Host) completeCommand(partial string) string {
|
||||
@ -300,7 +305,7 @@ func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int,
|
||||
}
|
||||
} else {
|
||||
// Name
|
||||
completed = h.completeName(partial, u)
|
||||
completed = h.completeName(partial, u.Name())
|
||||
if completed == "" {
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user