Sort conversations properly in `lmcli ls`

This commit is contained in:
Matt Low 2023-11-13 06:35:57 +00:00
parent 9a1aae83da
commit e66016aedd
1 changed files with 15 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"fmt" "fmt"
"slices"
"strings" "strings"
"time" "time"
@ -51,6 +52,11 @@ var lsCmd = &cobra.Command{
// 4lk3 - 4 months ago - Local events and meetups // 4lk3 - 4 months ago - Local events and meetups
// 43jn - 6 months ago - Mobile photography techniques // 43jn - 6 months ago - Mobile photography techniques
type ConversationLine struct {
timeSinceReply time.Duration
formatted string
}
now := time.Now() now := time.Now()
categories := []string{ categories := []string{
"recent", "recent",
@ -61,7 +67,7 @@ var lsCmd = &cobra.Command{
"last 6 months", "last 6 months",
"older", "older",
} }
categorized := map[string][]string{} categorized := map[string][]ConversationLine{}
for _, conversation := range conversations { for _, conversation := range conversations {
lastMessage, err := store.LastMessage(&conversation) lastMessage, err := store.LastMessage(&conversation)
@ -94,7 +100,10 @@ var lsCmd = &cobra.Command{
humanTimeElapsedSince(messageAge), humanTimeElapsedSince(messageAge),
conversation.Title, conversation.Title,
) )
categorized[category] = append(categorized[category], formatted) categorized[category] = append(
categorized[category],
ConversationLine{messageAge, formatted},
)
} }
for _, category := range categories { for _, category := range categories {
@ -102,9 +111,12 @@ var lsCmd = &cobra.Command{
if !ok { if !ok {
continue continue
} }
slices.SortFunc(conversations, func(a, b ConversationLine) int {
return int(a.timeSinceReply - b.timeSinceReply)
})
fmt.Printf("%s:\n", category) fmt.Printf("%s:\n", category)
for _, conv := range conversations { for _, conv := range conversations {
fmt.Printf(" %s\n", conv) fmt.Printf(" %s\n", conv.formatted)
} }
} }
}, },