Skip to content

Commit

Permalink
feat: make --delete repeatable
Browse files Browse the repository at this point in the history
closes #347
  • Loading branch information
caarlos0 committed Jan 14, 2025
1 parent d528dd5 commit 5e3e9f3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Check the [`./features.md`](./features.md) for more details.
- `-s`, `--show`: Show saved conversation for the given title or SHA-1.
- `-S`, `--show-last`: Show previous conversation.
- `--delete-older-than=<duration>`: Deletes conversations older than given duration (`10d`, `1mo`).
- `--delete`: Deletes the saved conversation for the given title or SHA-1.
- `--delete`: Deletes the saved conversations for the given titles or SHA-1s.
- `--no-cache`: Do not save conversations.

#### Advanced
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var help = map[string]string{
"no-cache": "Disables caching of the prompt/response.",
"title": "Saves the current conversation with the given title.",
"list": "Lists saved conversations.",
"delete": "Deletes a saved conversation with the given title or ID.",
"delete": "Deletes one or more saved conversations with the given titles or IDs.",
"delete-older-than": "Deletes all saved conversations older than the specified duration. Valid units are: " + strings.EnglishJoin(duration.ValidUnits(), true) + ".",
"show": "Show a saved conversation with the given title or ID.",
"theme": "Theme to use in the forms. Valid units are: 'charm', 'catppuccin', 'dracula', and 'base16'",
Expand Down Expand Up @@ -176,7 +176,7 @@ type Config struct {
Show string
List bool
ListRoles bool
Delete string
Delete []string
DeleteOlderThan time.Duration
User string

Expand Down
6 changes: 3 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,16 @@ func (c *convoDB) Find(in string) (*Conversation, error) {
err = c.findByIDOrTitle(&conversations, in)
}
if err != nil {
return nil, fmt.Errorf("Find: %w", err)
return nil, fmt.Errorf("Find %q: %w", in, err)
}

if len(conversations) > 1 {
return nil, errManyMatches
return nil, fmt.Errorf("%w: %s", errManyMatches, in)
}
if len(conversations) == 1 {
return &conversations[0], nil
}
return nil, errNoMatches
return nil, fmt.Errorf("%w: %s", errNoMatches, in)
}

func (c *convoDB) List() ([]Conversation, error) {
Expand Down
4 changes: 2 additions & 2 deletions features.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ You can also delete conversations by title or ID, same as `--show`, different
flag:

```bash
mods --delete='naturals'
mods --delete='a2e2'
mods --delete='naturals' --delete='a2e2'
```

Keep in mind that these operations are not reversible.
You can repeat the delete flag to delete multiple conversations at once.
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ var (
return listConversations(config.Raw)
}

if config.Delete != "" {
return deleteConversation()
if len(config.Delete) > 0 {
return deleteConversations()
}

if config.DeleteOlderThan > 0 {
Expand Down Expand Up @@ -232,7 +232,7 @@ func initFlags() {
flags.BoolVarP(&config.ContinueLast, "continue-last", "C", false, stdoutStyles().FlagDesc.Render(help["continue-last"]))
flags.BoolVarP(&config.List, "list", "l", config.List, stdoutStyles().FlagDesc.Render(help["list"]))
flags.StringVarP(&config.Title, "title", "t", config.Title, stdoutStyles().FlagDesc.Render(help["title"]))
flags.StringVarP(&config.Delete, "delete", "d", config.Delete, stdoutStyles().FlagDesc.Render(help["delete"]))
flags.StringArrayVarP(&config.Delete, "delete", "d", config.Delete, stdoutStyles().FlagDesc.Render(help["delete"]))
flags.Var(newDurationFlag(config.DeleteOlderThan, &config.DeleteOlderThan), "delete-older-than", stdoutStyles().FlagDesc.Render(help["delete-older-than"]))
flags.StringVarP(&config.Show, "show", "s", config.Show, stdoutStyles().FlagDesc.Render(help["show"]))
flags.BoolVarP(&config.ShowLast, "show-last", "S", false, stdoutStyles().FlagDesc.Render(help["show-last"]))
Expand Down Expand Up @@ -538,12 +538,20 @@ func deleteConversationOlderThan() error {
return nil
}

func deleteConversation() error {
convo, err := db.Find(config.Delete)
if err != nil {
return modsError{err, "Couldn't find conversation to delete."}
func deleteConversations() error {
for _, del := range config.Delete {
convo, err := db.Find(del)
if err != nil {
return modsError{err, "Couldn't find conversation to delete."}
}
if err := deleteConversation(convo); err != nil {
return err
}
}
return nil
}

func deleteConversation(convo *Conversation) error {
if err := db.Delete(convo.ID); err != nil {
return modsError{err, "Couldn't delete conversation."}
}
Expand Down Expand Up @@ -713,7 +721,7 @@ func isNoArgs() bool {
return config.Prefix == "" &&
config.Show == "" &&
!config.ShowLast &&
config.Delete == "" &&
len(config.Delete) == 0 &&
config.DeleteOlderThan == 0 &&
!config.ShowHelp &&
!config.List &&
Expand Down
2 changes: 1 addition & 1 deletion mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (m *Mods) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.quit
}
if m.Config.Dirs ||
m.Config.Delete != "" ||
len(m.Config.Delete) > 0 ||
m.Config.DeleteOlderThan != 0 ||
m.Config.ShowHelp ||
m.Config.List ||
Expand Down

0 comments on commit 5e3e9f3

Please sign in to comment.