Merge pull request #41 from StalkR/nick

state/nick: canonicalise the order of channels returned
This commit is contained in:
Alex Bee 2014-05-20 20:43:02 +01:00
commit 9c5890c91a
1 changed files with 15 additions and 6 deletions

View File

@ -2,7 +2,9 @@ package state
import ( import (
"github.com/fluffle/goirc/logging" "github.com/fluffle/goirc/logging"
"reflect" "reflect"
"sort"
) )
// A struct representing an IRC nick // A struct representing an IRC nick
@ -111,22 +113,29 @@ func (nk *Nick) ParseModes(modes string) {
} }
} }
// Channels returns a list of *Channel the nick is on. type byName []*Channel
func (b byName) Len() int { return len(b) }
func (b byName) Less(i, j int) bool { return b[i].Name < b[j].Name }
func (b byName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// Channels returns a list of *Channel the nick is on, sorted by name.
func (nk *Nick) Channels() []*Channel { func (nk *Nick) Channels() []*Channel {
channels := make([]*Channel, 0, len(nk.lookup)) channels := make([]*Channel, 0, len(nk.lookup))
for _, channel := range nk.lookup { for _, channel := range nk.lookup {
channels = append(channels, channel) channels = append(channels, channel)
} }
sort.Sort(byName(channels))
return channels return channels
} }
// ChannelsStr returns a list of channel strings the nick is on. // ChannelsStr returns a list of channel strings the nick is on, sorted by name.
func (nk *Nick) ChannelsStr() []string { func (nk *Nick) ChannelsStr() []string {
channels := make([]string, 0, len(nk.lookup)) var names []string
for _, channel := range nk.lookup { for _, channel := range nk.Channels() {
channels = append(channels, channel.Name) names = append(names, channel.Name)
} }
return channels return names
} }
// Returns a string representing the nick. Looks like: // Returns a string representing the nick. Looks like: