diff --git a/internal/commands/list.go b/internal/commands/list.go index bd65c30b..2e0a906f 100644 --- a/internal/commands/list.go +++ b/internal/commands/list.go @@ -37,6 +37,7 @@ var ( listQuiet bool listSort string listFull bool + listLimit int ) var listCmd = &cobra.Command{ @@ -121,6 +122,11 @@ Search Syntax (--search/-S): // Sort beans sortBeans(beans, listSort, cfg) + // Apply limit + if listLimit > 0 && len(beans) > listLimit { + beans = beans[:listLimit] + } + // JSON output (flat list) if listJSON { if !listFull { @@ -311,5 +317,6 @@ func RegisterListCmd(root *cobra.Command) { listCmd.Flags().BoolVarP(&listQuiet, "quiet", "q", false, "Only output IDs (one per line)") listCmd.Flags().StringVar(&listSort, "sort", "", "Sort by: created, updated, status, priority, id (default: status, priority, type, title)") listCmd.Flags().BoolVar(&listFull, "full", false, "Include bean body in JSON output") + listCmd.Flags().IntVar(&listLimit, "limit", 0, "Limit the number of beans returned") root.AddCommand(listCmd) } diff --git a/internal/commands/list_test.go b/internal/commands/list_test.go index 610f52d6..8063c96e 100644 --- a/internal/commands/list_test.go +++ b/internal/commands/list_test.go @@ -165,3 +165,36 @@ func TestTruncate(t *testing.T) { } } + +func TestListLimit(t *testing.T) { + beans := []*bean.Bean{ + {ID: "1"}, + {ID: "2"}, + {ID: "3"}, + {ID: "4"}, + {ID: "5"}, + } + + tests := []struct { + name string + limit int + want int + }{ + {"no limit", 0, 5}, + {"limit less than total", 3, 3}, + {"limit exactly total", 5, 5}, + {"limit greater than total", 10, 5}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := beans + if tt.limit > 0 && len(out) > tt.limit { + out = out[:tt.limit] + } + if len(out) != tt.want { + t.Errorf("limit=%d: got len=%d, want %d", tt.limit, len(out), tt.want) + } + }) + } +}