#[derive(Debug, Clone)]
pub struct Command {
pub name: &'static str,
pub description: &'static str,
pub shortcut: Option<&'static str>,
pub action: CommandAction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandAction {
ViewAccounts,
ViewBudget,
ViewReports,
ViewRegister,
AddAccount,
EditAccount,
ArchiveAccount,
AddTransaction,
EditTransaction,
DeleteTransaction,
ClearTransaction,
MoveFunds,
AssignBudget,
NextPeriod,
PrevPeriod,
SetIncome,
AddCategory,
AddGroup,
EditCategory,
DeleteCategory,
EditGroup,
DeleteGroup,
Help,
Quit,
Refresh,
ToggleArchived,
AutoFillTargets,
}
pub static COMMANDS: &[Command] = &[
Command {
name: "accounts",
description: "View all accounts",
shortcut: Some("1"),
action: CommandAction::ViewAccounts,
},
Command {
name: "budget",
description: "View budget",
shortcut: Some("2"),
action: CommandAction::ViewBudget,
},
Command {
name: "reports",
description: "View reports",
shortcut: Some("3"),
action: CommandAction::ViewReports,
},
Command {
name: "register",
description: "View transactions for selected account",
shortcut: Some("Enter"),
action: CommandAction::ViewRegister,
},
Command {
name: "add-transaction",
description: "Add a new transaction",
shortcut: Some("a"),
action: CommandAction::AddTransaction,
},
Command {
name: "edit-transaction",
description: "Edit selected transaction",
shortcut: Some("e"),
action: CommandAction::EditTransaction,
},
Command {
name: "delete-transaction",
description: "Delete selected transaction",
shortcut: Some("Ctrl+d"),
action: CommandAction::DeleteTransaction,
},
Command {
name: "clear-transaction",
description: "Toggle transaction cleared status",
shortcut: Some("c"),
action: CommandAction::ClearTransaction,
},
Command {
name: "move-funds",
description: "Move funds between categories",
shortcut: Some("m"),
action: CommandAction::MoveFunds,
},
Command {
name: "assign",
description: "Assign funds to category",
shortcut: None,
action: CommandAction::AssignBudget,
},
Command {
name: "next-period",
description: "Go to next budget period",
shortcut: Some("]"),
action: CommandAction::NextPeriod,
},
Command {
name: "prev-period",
description: "Go to previous budget period",
shortcut: Some("["),
action: CommandAction::PrevPeriod,
},
Command {
name: "set-income",
description: "Set expected income for current period",
shortcut: Some("i"),
action: CommandAction::SetIncome,
},
Command {
name: "add-account",
description: "Create a new account",
shortcut: None,
action: CommandAction::AddAccount,
},
Command {
name: "edit-account",
description: "Edit selected account",
shortcut: None,
action: CommandAction::EditAccount,
},
Command {
name: "archive-account",
description: "Archive selected account",
shortcut: None,
action: CommandAction::ArchiveAccount,
},
Command {
name: "toggle-archived",
description: "Show/hide archived accounts",
shortcut: Some("A"),
action: CommandAction::ToggleArchived,
},
Command {
name: "add-category",
description: "Create a new category",
shortcut: Some("a"),
action: CommandAction::AddCategory,
},
Command {
name: "add-group",
description: "Create a new category group",
shortcut: Some("A"),
action: CommandAction::AddGroup,
},
Command {
name: "edit-category",
description: "Edit selected category",
shortcut: None,
action: CommandAction::EditCategory,
},
Command {
name: "delete-category",
description: "Delete selected category",
shortcut: None,
action: CommandAction::DeleteCategory,
},
Command {
name: "edit-group",
description: "Edit selected category group",
shortcut: Some("E"),
action: CommandAction::EditGroup,
},
Command {
name: "delete-group",
description: "Delete selected category group",
shortcut: Some("D"),
action: CommandAction::DeleteGroup,
},
Command {
name: "help",
description: "Show help",
shortcut: Some("?"),
action: CommandAction::Help,
},
Command {
name: "quit",
description: "Quit application",
shortcut: Some("q"),
action: CommandAction::Quit,
},
Command {
name: "refresh",
description: "Refresh data from disk",
shortcut: None,
action: CommandAction::Refresh,
},
Command {
name: "auto-fill-targets",
description: "Fill budgets from targets",
shortcut: None,
action: CommandAction::AutoFillTargets,
},
];
pub fn find_command(name: &str) -> Option<&'static Command> {
COMMANDS.iter().find(|cmd| cmd.name == name)
}
pub fn filter_commands(query: &str) -> Vec<&'static Command> {
if query.is_empty() {
COMMANDS.iter().collect()
} else {
let query_lower = query.to_lowercase();
COMMANDS
.iter()
.filter(|cmd| {
cmd.name.to_lowercase().contains(&query_lower)
|| cmd.description.to_lowercase().contains(&query_lower)
})
.collect()
}
}