From b3913d00272f2a46e822133e12bbf661d0527567 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 3 Jan 2024 07:20:46 +0000 Subject: [PATCH] Add limit to number of conversations shown by default by `lmcli ls` --- pkg/cli/cmd.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/cli/cmd.go b/pkg/cli/cmd.go index 1fd8db6..c8f3dfa 100644 --- a/pkg/cli/cmd.go +++ b/pkg/cli/cmd.go @@ -17,6 +17,11 @@ var ( systemPromptFile string ) +const ( + // Limit to number of conversations shown with `ls`, without --all + LS_LIMIT int = 25 +) + func init() { inputCmds := []*cobra.Command{newCmd, promptCmd, replyCmd, retryCmd, continueCmd, editCmd} for _, cmd := range inputCmds { @@ -27,6 +32,7 @@ func init() { cmd.MarkFlagsMutuallyExclusive("system-prompt", "system-prompt-file") } + lsCmd.Flags().Bool("all", false, fmt.Sprintf("Show all conversations, by default only the last %d are shown", LS_LIMIT)) renameCmd.Flags().Bool("generate", false, "Generate a conversation title") rootCmd.AddCommand( @@ -203,6 +209,8 @@ var lsCmd = &cobra.Command{ } categorized := map[string][]ConversationLine{} + all, _ := cmd.Flags().GetBool("all") + for _, conversation := range conversations { lastMessage, err := store.LastMessage(&conversation) if lastMessage == nil || err != nil { @@ -232,17 +240,27 @@ var lsCmd = &cobra.Command{ ) } + var conversationsPrinted int + outer: for _, category := range categories { - conversations, ok := categorized[category] + conversations, ok := categorized[category.name] if !ok { 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.name) for _, conv := range conversations { + if conversationsPrinted >= LS_LIMIT && !all { + fmt.Printf("%d remaining message(s), use --all to view.\n", len(conversations)-conversationsPrinted) + break outer + } + fmt.Printf(" %s\n", conv.formatted) + conversationsPrinted++ } } },