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

View File

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