feat: add --limit cli arg to list command

This commit is contained in:
2026-02-03 15:58:13 +02:00
parent 1497e49a8e
commit 5e7f239619
2 changed files with 67 additions and 13 deletions

View File

@@ -12,12 +12,14 @@ add and list expenses directly from your terminal without opening the web interf
## Features
- **Add** and **list** expenses in Cospend projects via the **REST API**
- **Add**, **list**, and **delete** expenses in Cospend projects via the **REST API**
- **List projects** you have access to
- **Filter** expenses by payer, owed members, amount, name, category, or payment method
- Resolve categories, payment methods, and members by **name or ID**
- **Case-insensitive** matching for all lookups
- **Currency code support** (e.g., `usd`, `eur`, `gbp`) with automatic symbol resolution
- **Local caching** of project data with 1-hour TTL for faster subsequent calls
- **Global project flag** - set `-p` before the command for easy shell aliases
- Cross-platform support: **macOS**, **Linux**, and **Windows**
---
@@ -121,6 +123,24 @@ export NEXTCLOUD_PASSWORD="your-app-password"
## Usage
### Global Flags
The `-p`/`--project` flag can be used before or after the command, making it easy to create shell
aliases:
```bash
# These are equivalent:
cospend add "Groceries" 25.50 -p myproject
cospend -p myproject add "Groceries" 25.50
# Create an alias for a specific project:
alias cospend-home="cospend -p homeproject"
cospend-home add "Groceries" 25.50
cospend-home list
```
---
### Adding Expenses
```bash
@@ -193,16 +213,17 @@ cospend list -p myproject -b alice -c restaurant --amount ">=20"
#### List Command Flags
| Short | Long | Description |
| ----- | ------------ | --------------------------------------------------- |
| `-p` | `--project` | Project ID (required) |
| `-b` | `--by` | Filter by paying member username |
| `-f` | `--for` | Filter by owed member username (repeatable) |
| Short | Long | Description |
| ----- | ------------ | ---------------------------------------------------- |
| `-p` | `--project` | Project ID (required) |
| `-b` | `--by` | Filter by paying member username |
| `-f` | `--for` | Filter by owed member username (repeatable) |
| `-a` | `--amount` | Filter by amount (e.g., `50`, `>30`, `<=100`, `=25`) |
| `-n` | `--name` | Filter by name (case-insensitive, contains) |
| `-c` | `--category` | Filter by category name or ID |
| `-m` | `--method` | Filter by payment method name or ID |
| `-h` | `--help` | Display help information |
| `-n` | `--name` | Filter by name (case-insensitive, contains) |
| `-c` | `--category` | Filter by category name or ID |
| `-m` | `--method` | Filter by payment method name or ID |
| `-l` | `--limit` | Limit number of results (0 = no limit) |
| `-h` | `--help` | Display help information |
The output includes the bill ID for each expense, which can be used with the delete command.
@@ -224,13 +245,39 @@ cospend delete 123 -p myproject
#### Delete Command Flags
| Short | Long | Description |
| ----- | ----------- | --------------------- |
| `-p` | `--project` | Project ID (required) |
| Short | Long | Description |
| ----- | ----------- | ------------------------ |
| `-p` | `--project` | Project ID (required) |
| `-h` | `--help` | Display help information |
---
### Listing Projects
```bash
cospend projects [flags]
cospend proj [flags] # alias
```
#### Examples
```bash
# List all active projects
cospend projects
# Include archived projects
cospend projects --all
```
#### Projects Command Flags
| Short | Long | Description |
| ----- | -------- | ------------------------------------ |
| `-a` | `--all` | Show all projects including archived |
| `-h` | `--help` | Display help information |
---
## Caching
Project data (members, categories, payment methods, currencies) is cached locally to avoid repeated

View File

@@ -20,6 +20,7 @@ var (
listName string
listPaymentMethod string
listCategory string
listLimit int
)
// amountFilter holds parsed amount filter criteria
@@ -51,6 +52,7 @@ Examples:
cmd.Flags().StringVarP(&listName, "name", "n", "", "Filter by name (case-insensitive, contains)")
cmd.Flags().StringVarP(&listPaymentMethod, "method", "m", "", "Filter by payment method")
cmd.Flags().StringVarP(&listCategory, "category", "c", "", "Filter by category")
cmd.Flags().IntVarP(&listLimit, "limit", "l", 0, "Limit number of results (0 = no limit)")
return cmd
}
@@ -270,6 +272,11 @@ func printBillsTable(cmd *cobra.Command, project *api.Project, bills []api.BillR
return bills[i].Timestamp > bills[j].Timestamp
})
// Apply limit if set
if listLimit > 0 && len(bills) > listLimit {
bills = bills[:listLimit]
}
// Build lookup maps for names
memberNames := make(map[int]string)
for _, m := range project.Members {