Changed how conversations are grouped by age in `lmcli ls`

This commit is contained in:
Matt Low 2024-01-03 07:19:08 +00:00
parent a25d0d95e8
commit 1184f9aaae
1 changed files with 27 additions and 45 deletions

View File

@ -173,25 +173,10 @@ var lsCmd = &cobra.Command{
return return
} }
// Example output type Category struct {
// $ lmcli ls name string
// last hour: cutoff time.Duration
// 98sg - 12 minutes ago - Project discussion }
// last day:
// tj3l - 10 hours ago - Deep learning concepts
// last week:
// bwfm - 2 days ago - Machine learning study
// 8n3h - 3 days ago - Weekend plans
// f3n7 - 6 days ago - CLI development
// last month:
// 5hn2 - 8 days ago - Book club discussion
// b7ze - 20 days ago - Gardening tips and tricks
// last 6 months:
// 3jn2 - 30 days ago - Web development best practices
// 43jk - 2 months ago - Longboard maintenance
// g8d9 - 3 months ago - History book club
// 4lk3 - 4 months ago - Local events and meetups
// 43jn - 6 months ago - Mobile photography techniques
type ConversationLine struct { type ConversationLine struct {
timeSinceReply time.Duration timeSinceReply time.Duration
@ -199,15 +184,22 @@ var lsCmd = &cobra.Command{
} }
now := time.Now() now := time.Now()
categories := []string{
"recent", midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
"last hour", monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
"last 6 hours", dayOfWeek := int(now.Weekday())
"last day", categories := []Category{
"last week", {"today", now.Sub(midnight)},
"last month", {"yesterday", now.Sub(midnight.AddDate(0, 0, -1))},
"last 6 months", {"this week", now.Sub(midnight.AddDate(0, 0, -dayOfWeek))},
"older", {"last week", now.Sub(midnight.AddDate(0, 0, -(dayOfWeek + 7)))},
{"this month", now.Sub(monthStart)},
{"last month", now.Sub(monthStart.AddDate(0, -1, 0))},
{"2 months ago", now.Sub(monthStart.AddDate(0, -2, 0))},
{"3 months ago", now.Sub(monthStart.AddDate(0, -3, 0))},
{"4 months ago", now.Sub(monthStart.AddDate(0, -4, 0))},
{"5 months ago", now.Sub(monthStart.AddDate(0, -5, 0))},
{"older", now.Sub(time.Time{})},
} }
categorized := map[string][]ConversationLine{} categorized := map[string][]ConversationLine{}
@ -216,26 +208,15 @@ var lsCmd = &cobra.Command{
if lastMessage == nil || err != nil { if lastMessage == nil || err != nil {
continue continue
} }
messageAge := now.Sub(lastMessage.CreatedAt) messageAge := now.Sub(lastMessage.CreatedAt)
var category string var category string
switch { for _, c := range categories {
case messageAge <= 10*time.Minute: if messageAge < c.cutoff {
category = "recent" category = c.name
case messageAge <= time.Hour: break
category = "last hour" }
case messageAge <= 6*time.Hour:
category = "last 6 hours"
case messageAge <= 24*time.Hour:
category = "last day"
case messageAge <= 7*24*time.Hour:
category = "last week"
case messageAge <= 30*24*time.Hour:
category = "last month"
case messageAge <= 6*30*24*time.Hour: // Approximate as 6 months
category = "last 6 months"
default:
category = "older"
} }
formatted := fmt.Sprintf( formatted := fmt.Sprintf(
@ -244,6 +225,7 @@ var lsCmd = &cobra.Command{
humanTimeElapsedSince(messageAge), humanTimeElapsedSince(messageAge),
conversation.Title, conversation.Title,
) )
categorized[category] = append( categorized[category] = append(
categorized[category], categorized[category],
ConversationLine{messageAge, formatted}, ConversationLine{messageAge, formatted},