From f60cdf2033931920816fa81b2b7e64fbdc4d279e Mon Sep 17 00:00:00 2001 From: Hadi <112569860+anotherhadi@users.noreply.github.com> Date: Thu, 21 May 2026 09:34:52 +0200 Subject: [PATCH] Delete all except flagged entries Signed-off-by: Hadi <112569860+anotherhadi@users.noreply.github.com> --- internal/db/entries.go | 15 +++++++++++++++ internal/ui/history/update.go | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/db/entries.go b/internal/db/entries.go index 74d983a..a1c718a 100644 --- a/internal/db/entries.go +++ b/internal/db/entries.go @@ -144,3 +144,18 @@ func (d *DB) DeleteAllEntries() error { _, err := d.conn.Exec(`DELETE FROM entries`) return err } + +// DeleteAllExceptFlagged deletes all unflagged entries. If there are no +// unflagged entries (only flagged ones remain), it deletes everything. +func (d *DB) DeleteAllExceptFlagged() error { + var count int + if err := d.conn.QueryRow(`SELECT COUNT(*) FROM entries WHERE flagged = 0`).Scan(&count); err != nil { + return err + } + if count > 0 { + _, err := d.conn.Exec(`DELETE FROM entries WHERE flagged = 0`) + return err + } + _, err := d.conn.Exec(`DELETE FROM entries`) + return err +} diff --git a/internal/ui/history/update.go b/internal/ui/history/update.go index 3fc1ef3..dea6f71 100644 --- a/internal/ui/history/update.go +++ b/internal/ui/history/update.go @@ -249,11 +249,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case key.Matches(msg, h.DeleteAll): if m.database != nil { if m.searchKind != searchKindOff { + hasUnflagged := false for _, e := range m.entries { + if !e.Flagged { + hasUnflagged = true + break + } + } + for _, e := range m.entries { + if hasUnflagged && e.Flagged { + continue + } m.database.DeleteEntry(e.ID) } } else { - m.database.DeleteAllEntries() + m.database.DeleteAllExceptFlagged() } } return m, m.clearSearch()