Fix sorting list output
This commit is contained in:
parent
abffc0b54c
commit
3d92a98891
27
commands.go
27
commands.go
@ -545,19 +545,12 @@ func (handler *CommandHandler) CommandDeleteAllPortals(ce *CommandEvent) {
|
|||||||
|
|
||||||
const cmdListHelp = `list <contacts|groups> [page] [items per page] - Get a list of all contacts and groups.`
|
const cmdListHelp = `list <contacts|groups> [page] [items per page] - Get a list of all contacts and groups.`
|
||||||
|
|
||||||
func formatContacts(contacts bool, skip, max int, input map[string]whatsapp.Contact) (result []string, total int) {
|
func formatContacts(contacts bool, input map[string]whatsapp.Contact) (result []string) {
|
||||||
skipped := 0
|
|
||||||
for jid, contact := range input {
|
for jid, contact := range input {
|
||||||
if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts {
|
if strings.HasSuffix(jid, whatsappExt.NewUserSuffix) != contacts {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
total++
|
|
||||||
if skipped < skip || len(result) >= max {
|
|
||||||
skipped++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if contacts {
|
if contacts {
|
||||||
result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.Jid[:len(contact.Jid)-len(whatsappExt.NewUserSuffix)]))
|
result = append(result, fmt.Sprintf("* %s / %s - `%s`", contact.Name, contact.Notify, contact.Jid[:len(contact.Jid)-len(whatsappExt.NewUserSuffix)]))
|
||||||
} else {
|
} else {
|
||||||
@ -576,6 +569,7 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
|||||||
mode := strings.ToLower(ce.Args[0])
|
mode := strings.ToLower(ce.Args[0])
|
||||||
if mode[0] != 'g' && mode[0] != 'c' {
|
if mode[0] != 'g' && mode[0] != 'c' {
|
||||||
ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
|
ce.Reply("**Usage:** `list <contacts|groups> [page] [items per page]`")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
page := 1
|
page := 1
|
||||||
@ -601,8 +595,21 @@ func (handler *CommandHandler) CommandList(ce *CommandEvent) {
|
|||||||
if contacts {
|
if contacts {
|
||||||
typeName = "Contacts"
|
typeName = "Contacts"
|
||||||
}
|
}
|
||||||
result, totalItems := formatContacts(contacts, (page-1)*max, max, ce.User.Conn.Store.Contacts)
|
result := formatContacts(contacts, ce.User.Conn.Store.Contacts)
|
||||||
pages := int(math.Ceil(float64(totalItems) / float64(max)))
|
if len(result) == 0 {
|
||||||
|
ce.Reply("No %s found", strings.ToLower(typeName))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pages := int(math.Ceil(float64(len(result)) / float64(max)))
|
||||||
|
if (page - 1) * max >= len(result) {
|
||||||
|
if pages == 1 {
|
||||||
|
ce.Reply("There is only 1 page of %s", strings.ToLower(typeName))
|
||||||
|
} else {
|
||||||
|
ce.Reply("There are only %d pages of %s", pages, strings.ToLower(typeName))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result = result[(page-1)*max : page*max]
|
||||||
ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n"))
|
ce.Reply("### %s (page %d of %d)\n\n%s", typeName, page, pages, strings.Join(result, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user