1#[derive(Debug, Clone)]
7pub struct Command {
8 pub name: &'static str,
10 pub description: &'static str,
12 pub shortcut: Option<&'static str>,
14 pub action: CommandAction,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum CommandAction {
21 ViewAccounts,
23 ViewBudget,
24 ViewReports,
25 ViewRegister,
26
27 AddAccount,
29 EditAccount,
30 ArchiveAccount,
31
32 AddTransaction,
34 EditTransaction,
35 DeleteTransaction,
36 ClearTransaction,
37
38 MoveFunds,
40 AssignBudget,
41 NextPeriod,
42 PrevPeriod,
43
44 AddCategory,
46 AddGroup,
47 EditCategory,
48 DeleteCategory,
49
50 Help,
52 Quit,
53 Refresh,
54 ToggleArchived,
55
56 AutoFillTargets,
58}
59
60pub static COMMANDS: &[Command] = &[
62 Command {
64 name: "accounts",
65 description: "View all accounts",
66 shortcut: Some("1"),
67 action: CommandAction::ViewAccounts,
68 },
69 Command {
70 name: "budget",
71 description: "View budget",
72 shortcut: Some("2"),
73 action: CommandAction::ViewBudget,
74 },
75 Command {
76 name: "reports",
77 description: "View reports",
78 shortcut: Some("3"),
79 action: CommandAction::ViewReports,
80 },
81 Command {
82 name: "register",
83 description: "View transactions for selected account",
84 shortcut: Some("Enter"),
85 action: CommandAction::ViewRegister,
86 },
87 Command {
89 name: "add-transaction",
90 description: "Add a new transaction",
91 shortcut: Some("a"),
92 action: CommandAction::AddTransaction,
93 },
94 Command {
95 name: "edit-transaction",
96 description: "Edit selected transaction",
97 shortcut: Some("e"),
98 action: CommandAction::EditTransaction,
99 },
100 Command {
101 name: "delete-transaction",
102 description: "Delete selected transaction",
103 shortcut: Some("Ctrl+d"),
104 action: CommandAction::DeleteTransaction,
105 },
106 Command {
107 name: "clear-transaction",
108 description: "Toggle transaction cleared status",
109 shortcut: Some("c"),
110 action: CommandAction::ClearTransaction,
111 },
112 Command {
114 name: "move-funds",
115 description: "Move funds between categories",
116 shortcut: Some("m"),
117 action: CommandAction::MoveFunds,
118 },
119 Command {
120 name: "assign",
121 description: "Assign funds to category",
122 shortcut: None,
123 action: CommandAction::AssignBudget,
124 },
125 Command {
126 name: "next-period",
127 description: "Go to next budget period",
128 shortcut: Some("]"),
129 action: CommandAction::NextPeriod,
130 },
131 Command {
132 name: "prev-period",
133 description: "Go to previous budget period",
134 shortcut: Some("["),
135 action: CommandAction::PrevPeriod,
136 },
137 Command {
139 name: "add-account",
140 description: "Create a new account",
141 shortcut: None,
142 action: CommandAction::AddAccount,
143 },
144 Command {
145 name: "edit-account",
146 description: "Edit selected account",
147 shortcut: None,
148 action: CommandAction::EditAccount,
149 },
150 Command {
151 name: "archive-account",
152 description: "Archive selected account",
153 shortcut: None,
154 action: CommandAction::ArchiveAccount,
155 },
156 Command {
157 name: "toggle-archived",
158 description: "Show/hide archived accounts",
159 shortcut: Some("A"),
160 action: CommandAction::ToggleArchived,
161 },
162 Command {
164 name: "add-category",
165 description: "Create a new category",
166 shortcut: Some("a"),
167 action: CommandAction::AddCategory,
168 },
169 Command {
170 name: "add-group",
171 description: "Create a new category group",
172 shortcut: Some("A"),
173 action: CommandAction::AddGroup,
174 },
175 Command {
176 name: "edit-category",
177 description: "Edit selected category",
178 shortcut: None,
179 action: CommandAction::EditCategory,
180 },
181 Command {
182 name: "delete-category",
183 description: "Delete selected category",
184 shortcut: None,
185 action: CommandAction::DeleteCategory,
186 },
187 Command {
189 name: "help",
190 description: "Show help",
191 shortcut: Some("?"),
192 action: CommandAction::Help,
193 },
194 Command {
195 name: "quit",
196 description: "Quit application",
197 shortcut: Some("q"),
198 action: CommandAction::Quit,
199 },
200 Command {
201 name: "refresh",
202 description: "Refresh data from disk",
203 shortcut: None,
204 action: CommandAction::Refresh,
205 },
206 Command {
208 name: "auto-fill-targets",
209 description: "Fill budgets from targets",
210 shortcut: None,
211 action: CommandAction::AutoFillTargets,
212 },
213];
214
215pub fn find_command(name: &str) -> Option<&'static Command> {
217 COMMANDS.iter().find(|cmd| cmd.name == name)
218}
219
220pub fn filter_commands(query: &str) -> Vec<&'static Command> {
222 if query.is_empty() {
223 COMMANDS.iter().collect()
224 } else {
225 let query_lower = query.to_lowercase();
226 COMMANDS
227 .iter()
228 .filter(|cmd| {
229 cmd.name.to_lowercase().contains(&query_lower)
230 || cmd.description.to_lowercase().contains(&query_lower)
231 })
232 .collect()
233 }
234}