Add a rules screen that finds and removes dead rules
Nothing showed whether a rule was still earning its place. The new screen, on 5, lists every rule in file order with the number of transactions it claims, marking those that claim none. The count comes from Engine.Usage, which counts by first match, so a rule shadowed by an earlier one reports zero even though its glob matches. That is the case worth catching: such a rule looks correct in isolation and can never fire. d removes the selected rule and p removes every unused one, each behind a y/n confirmation since this rewrites a hand-maintained file. config.DeleteRules edits rules.toml textually rather than re-serialising the parsed rules, so comments and layout survive; a comment directly above a rule goes with it, while one separated by a blank line is left as a heading. The result is re-parsed before it replaces the file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+242
-2
@@ -29,8 +29,20 @@ const (
|
||||
viewTxns
|
||||
viewReport
|
||||
viewRules
|
||||
viewRuleList
|
||||
|
||||
viewCount = 4
|
||||
viewCount = 5
|
||||
)
|
||||
|
||||
// confirmation is a pending destructive action awaiting a y/n answer.
|
||||
// Deleting rewrites a file the user maintains by hand, so it is never done on
|
||||
// a single keypress.
|
||||
type confirmation int
|
||||
|
||||
const (
|
||||
confirmNone confirmation = iota
|
||||
confirmDeleteRule
|
||||
confirmPruneRules
|
||||
)
|
||||
|
||||
// input is the modal state: the transaction list is read-only until the user
|
||||
@@ -76,6 +88,12 @@ type Model struct {
|
||||
untagged []descGroup
|
||||
ruleMatches int // untagged descriptions the current glob matches
|
||||
|
||||
// Rule list: every rule with the number of transactions it actually
|
||||
// claims, so dead ones can be found and removed.
|
||||
ruleListTable table.Model
|
||||
ruleUsage []int
|
||||
confirm confirmation
|
||||
|
||||
filter store.Filter
|
||||
onlyUntagged bool
|
||||
|
||||
@@ -174,6 +192,15 @@ func New(root string, db *store.DB, accounts []*config.Account, engine *rules.En
|
||||
{Title: "Untagged description", Width: 44},
|
||||
{Title: "N", Width: 4},
|
||||
}),
|
||||
ruleListTable: newTable([]table.Column{
|
||||
{Title: "#", Width: 3},
|
||||
{Title: " ", Width: 1},
|
||||
{Title: "Pattern", Width: 34},
|
||||
{Title: "Account", Width: 12},
|
||||
{Title: "Tag", Width: 14},
|
||||
{Title: "T", Width: 1},
|
||||
{Title: "Txns", Width: 6},
|
||||
}),
|
||||
reportTable: newTable([]table.Column{
|
||||
{Title: "Tag", Width: 20},
|
||||
{Title: "Cur", Width: 4},
|
||||
@@ -435,6 +462,190 @@ func (m *Model) knownAccount(slug string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// reloadRuleList counts, for every rule, how many transactions it actually
|
||||
// claims. A rule can match nothing because no description fits it, or because
|
||||
// an earlier rule already took everything it would have caught; both show up
|
||||
// here as a zero.
|
||||
func (m *Model) reloadRuleList() error {
|
||||
txns, err := m.db.Transactions(store.Filter{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rs := m.engine.Rules()
|
||||
m.ruleUsage = m.engine.Usage(txns)
|
||||
|
||||
rows := make([]table.Row, 0, len(rs))
|
||||
for i, r := range rs {
|
||||
marker := " "
|
||||
if m.ruleUsage[i] == 0 {
|
||||
marker = "✗"
|
||||
}
|
||||
transfer := ""
|
||||
if r.Transfer {
|
||||
transfer = "T"
|
||||
}
|
||||
account := r.Account
|
||||
if account == "" {
|
||||
account = "(all)"
|
||||
}
|
||||
rows = append(rows, table.Row{
|
||||
fmt.Sprintf("%d", i+1), marker, rulePattern(r), account, r.Tag, transfer,
|
||||
fmt.Sprintf("%d", m.ruleUsage[i]),
|
||||
})
|
||||
}
|
||||
|
||||
cursor := m.ruleListTable.Cursor()
|
||||
m.ruleListTable.SetRows(rows)
|
||||
if cursor >= len(rows) {
|
||||
cursor = len(rows) - 1
|
||||
}
|
||||
if cursor < 0 {
|
||||
cursor = 0
|
||||
}
|
||||
m.ruleListTable.SetCursor(cursor)
|
||||
return nil
|
||||
}
|
||||
|
||||
// rulePattern renders whichever patterns a rule sets, labelled so a
|
||||
// counterparty or type rule is not mistaken for a description one.
|
||||
func rulePattern(r config.Rule) string {
|
||||
var parts []string
|
||||
if r.Match != "" {
|
||||
parts = append(parts, r.Match)
|
||||
}
|
||||
if r.Counterparty != "" {
|
||||
parts = append(parts, "counterparty:"+r.Counterparty)
|
||||
}
|
||||
if r.Type != "" {
|
||||
parts = append(parts, "type:"+r.Type)
|
||||
}
|
||||
return strings.Join(parts, " + ")
|
||||
}
|
||||
|
||||
// unusedRules lists the positions of every rule claiming no transactions.
|
||||
func (m *Model) unusedRules() []int {
|
||||
var out []int
|
||||
for i, n := range m.ruleUsage {
|
||||
if n == 0 {
|
||||
out = append(out, i)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// deleteRules removes rules from rules.toml, then reloads and retags so the
|
||||
// counts on screen reflect the new file.
|
||||
func (m *Model) deleteRules(positions []int) error {
|
||||
n, err := config.DeleteRules(m.root, positions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loaded, err := config.LoadRules(m.root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rules deleted, but re-reading rules.toml failed: %w", err)
|
||||
}
|
||||
m.engine = rules.New(loaded)
|
||||
|
||||
retagged, err := m.engine.Retag(m.db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.status = fmt.Sprintf("deleted %d rule(s), %d transactions retagged", n, retagged)
|
||||
|
||||
if err := m.reload(); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.reloadRuleList()
|
||||
}
|
||||
|
||||
// openRuleList switches to the rule list, remembering where to return to.
|
||||
func (m *Model) openRuleList() {
|
||||
if m.view != viewRuleList {
|
||||
m.ruleReturn = m.view
|
||||
}
|
||||
m.view = viewRuleList
|
||||
m.confirm = confirmNone
|
||||
if err := m.reloadRuleList(); err != nil {
|
||||
m.err = err
|
||||
}
|
||||
}
|
||||
|
||||
// updateRuleList drives the rule list, including the delete confirmations.
|
||||
func (m *Model) updateRuleList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
if m.confirm != confirmNone {
|
||||
pending := m.confirm
|
||||
m.confirm = confirmNone
|
||||
if msg.String() != "y" {
|
||||
m.status = "cancelled"
|
||||
return m, nil
|
||||
}
|
||||
m.err = nil
|
||||
switch pending {
|
||||
case confirmDeleteRule:
|
||||
if i := m.ruleListTable.Cursor(); i >= 0 && i < len(m.ruleUsage) {
|
||||
if err := m.deleteRules([]int{i}); err != nil {
|
||||
m.err = err
|
||||
}
|
||||
}
|
||||
case confirmPruneRules:
|
||||
if err := m.deleteRules(m.unusedRules()); err != nil {
|
||||
m.err = err
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
switch msg.String() {
|
||||
case "q", "ctrl+c":
|
||||
return m, tea.Quit
|
||||
case "esc":
|
||||
m.view = m.ruleReturn
|
||||
return m, nil
|
||||
case "1":
|
||||
m.view = viewAccounts
|
||||
return m, nil
|
||||
case "2":
|
||||
m.view = viewTxns
|
||||
return m, nil
|
||||
case "3":
|
||||
m.view = viewReport
|
||||
return m, nil
|
||||
case "4":
|
||||
return m, m.openRuleBuilder()
|
||||
|
||||
case "d":
|
||||
i := m.ruleListTable.Cursor()
|
||||
rs := m.engine.Rules()
|
||||
if i < 0 || i >= len(rs) {
|
||||
return m, nil
|
||||
}
|
||||
m.confirm = confirmDeleteRule
|
||||
m.status = fmt.Sprintf("delete rule %d (%s → %s), used by %d transactions? y/n",
|
||||
i+1, rulePattern(rs[i]), rs[i].Tag, m.ruleUsage[i])
|
||||
return m, nil
|
||||
|
||||
case "p":
|
||||
unused := m.unusedRules()
|
||||
if len(unused) == 0 {
|
||||
m.status = "no unused rules to prune"
|
||||
return m, nil
|
||||
}
|
||||
m.confirm = confirmPruneRules
|
||||
m.status = fmt.Sprintf("delete all %d rules that match nothing? y/n", len(unused))
|
||||
return m, nil
|
||||
|
||||
case "r":
|
||||
m.err = m.reloadRuleList()
|
||||
m.status = "counts refreshed"
|
||||
return m, nil
|
||||
}
|
||||
|
||||
var cmd tea.Cmd
|
||||
m.ruleListTable, cmd = m.ruleListTable.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
|
||||
// openRuleBuilder switches to the rule builder, remembering where to return to
|
||||
// and seeding the account field from whatever account is being browsed.
|
||||
func (m *Model) openRuleBuilder() tea.Cmd {
|
||||
@@ -525,6 +736,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
if m.view == viewRules {
|
||||
return m.updateRules(msg)
|
||||
}
|
||||
if m.view == viewRuleList {
|
||||
return m.updateRuleList(msg)
|
||||
}
|
||||
return m.updateNormal(msg)
|
||||
}
|
||||
return m, nil
|
||||
@@ -620,6 +834,7 @@ func (m *Model) resize() {
|
||||
m.txnTable.SetHeight(h)
|
||||
m.reportTable.SetHeight(h)
|
||||
m.ruleTable.SetHeight(h)
|
||||
m.ruleListTable.SetHeight(h)
|
||||
|
||||
// The preview list gets whatever the form does not use.
|
||||
if m.width > 0 {
|
||||
@@ -711,10 +926,16 @@ func (m *Model) updateNormal(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
return m, nil
|
||||
case "4":
|
||||
return m, m.openRuleBuilder()
|
||||
case "5":
|
||||
m.openRuleList()
|
||||
return m, nil
|
||||
case "tab":
|
||||
m.view = (m.view + 1) % viewCount
|
||||
if m.view == viewRules {
|
||||
switch m.view {
|
||||
case viewRules:
|
||||
return m, m.openRuleBuilder()
|
||||
case viewRuleList:
|
||||
m.openRuleList()
|
||||
}
|
||||
return m, nil
|
||||
|
||||
@@ -871,6 +1092,8 @@ func (m *Model) View() string {
|
||||
b.WriteString(m.reportTable.View())
|
||||
case viewRules:
|
||||
b.WriteString(m.rulesView())
|
||||
case viewRuleList:
|
||||
b.WriteString(m.ruleListTable.View())
|
||||
}
|
||||
}
|
||||
b.WriteString("\n")
|
||||
@@ -971,6 +1194,12 @@ func (m *Model) emptyMessage() string {
|
||||
return ""
|
||||
}
|
||||
return "Nothing to report yet.\n\nPress i to import some statements first."
|
||||
|
||||
case viewRuleList:
|
||||
if len(m.ruleListTable.Rows()) > 0 {
|
||||
return ""
|
||||
}
|
||||
return "No rules yet.\n\nPress 4 to build one, or write rules.toml by hand."
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -992,6 +1221,12 @@ func (m *Model) title() string {
|
||||
return "money · accounts"
|
||||
case viewRules:
|
||||
return "money · rule builder · writes to rules.toml"
|
||||
case viewRuleList:
|
||||
unused := len(m.unusedRules())
|
||||
if unused == 0 {
|
||||
return fmt.Sprintf("money · rules · %d rules, all in use", len(m.engine.Rules()))
|
||||
}
|
||||
return fmt.Sprintf("money · rules · %d rules · %d match nothing", len(m.engine.Rules()), unused)
|
||||
case viewReport:
|
||||
return "money · report · " + scope + " · transfers excluded"
|
||||
default:
|
||||
@@ -1020,6 +1255,11 @@ func (m *Model) help() string {
|
||||
return "enter open · 2 transactions · 3 report · 4 rules · i import · r retag · q quit"
|
||||
case viewRules:
|
||||
return "tab/↑↓ field · pgup/pgdn scroll list · enter save rule · esc back · ctrl+c quit"
|
||||
case viewRuleList:
|
||||
if m.confirm != confirmNone {
|
||||
return "y confirm · any other key cancels"
|
||||
}
|
||||
return "d delete rule · p prune all unused · r refresh counts · 4 rule builder · esc back · q quit"
|
||||
case viewReport:
|
||||
return "1 accounts · 2 transactions · 4 rules · u untagged · a all accounts · q quit"
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user