envelope-cli 0.2.6

Terminal-based zero-based budgeting application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Income dialog
//!
//! A dialog for setting expected income for a budget period.
//! Allows users to set, view, and remove income expectations.

use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

use crate::models::{BudgetPeriod, Money};
use crate::services::IncomeService;
use crate::tui::app::App;
use crate::tui::layout::centered_rect_fixed;

/// Which field is focused in the income dialog
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IncomeField {
    #[default]
    Amount,
    Notes,
}

impl IncomeField {
    pub fn next(self) -> Self {
        match self {
            Self::Amount => Self::Notes,
            Self::Notes => Self::Amount,
        }
    }

    pub fn prev(self) -> Self {
        match self {
            Self::Amount => Self::Notes,
            Self::Notes => Self::Amount,
        }
    }
}

/// State for the income dialog
#[derive(Debug, Clone, Default)]
pub struct IncomeFormState {
    /// The period being edited
    pub period: Option<BudgetPeriod>,
    /// Which field is focused
    pub focused_field: IncomeField,
    /// Amount input
    pub amount_input: String,
    /// Amount cursor position
    pub amount_cursor: usize,
    /// Notes input
    pub notes_input: String,
    /// Notes cursor position
    pub notes_cursor: usize,
    /// Whether there's an existing income expectation
    pub has_existing: bool,
    /// Current expected income (for display)
    pub current_amount: Option<Money>,
    /// Error message
    pub error_message: Option<String>,
}

impl IncomeFormState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Initialize the dialog for a period
    pub fn init_for_period(&mut self, period: &BudgetPeriod, storage: &crate::storage::Storage) {
        self.period = Some(period.clone());
        self.focused_field = IncomeField::Amount;
        self.error_message = None;

        let service = IncomeService::new(storage);
        if let Some(expectation) = service.get_income_expectation(period) {
            self.has_existing = true;
            self.current_amount = Some(expectation.expected_amount);
            let cents = expectation.expected_amount.cents();
            if cents == 0 {
                self.amount_input = String::new();
            } else {
                self.amount_input = format!("{:.2}", cents as f64 / 100.0);
            }
            self.amount_cursor = self.amount_input.len();
            self.notes_input = expectation.notes.clone();
            self.notes_cursor = self.notes_input.len();
        } else {
            self.has_existing = false;
            self.current_amount = None;
            self.amount_input = String::new();
            self.amount_cursor = 0;
            self.notes_input = String::new();
            self.notes_cursor = 0;
        }
    }

    /// Reset the state
    pub fn reset(&mut self) {
        *self = Self::default();
    }

    /// Set focus to a field
    pub fn set_focus(&mut self, field: IncomeField) {
        self.focused_field = field;
    }

    /// Move to next field
    pub fn next_field(&mut self) {
        self.focused_field = self.focused_field.next();
    }

    /// Move to previous field
    pub fn prev_field(&mut self) {
        self.focused_field = self.focused_field.prev();
    }

    /// Insert character into current field
    pub fn insert_char(&mut self, c: char) {
        match self.focused_field {
            IncomeField::Amount => {
                if c.is_ascii_digit() || c == '.' {
                    self.amount_input.insert(self.amount_cursor, c);
                    self.amount_cursor += 1;
                    self.error_message = None;
                }
            }
            IncomeField::Notes => {
                self.notes_input.insert(self.notes_cursor, c);
                self.notes_cursor += 1;
                self.error_message = None;
            }
        }
    }

    /// Delete character before cursor
    pub fn backspace(&mut self) {
        match self.focused_field {
            IncomeField::Amount => {
                if self.amount_cursor > 0 {
                    self.amount_cursor -= 1;
                    self.amount_input.remove(self.amount_cursor);
                    self.error_message = None;
                }
            }
            IncomeField::Notes => {
                if self.notes_cursor > 0 {
                    self.notes_cursor -= 1;
                    self.notes_input.remove(self.notes_cursor);
                    self.error_message = None;
                }
            }
        }
    }

    /// Move cursor left
    pub fn move_left(&mut self) {
        match self.focused_field {
            IncomeField::Amount => {
                if self.amount_cursor > 0 {
                    self.amount_cursor -= 1;
                }
            }
            IncomeField::Notes => {
                if self.notes_cursor > 0 {
                    self.notes_cursor -= 1;
                }
            }
        }
    }

    /// Move cursor right
    pub fn move_right(&mut self) {
        match self.focused_field {
            IncomeField::Amount => {
                if self.amount_cursor < self.amount_input.len() {
                    self.amount_cursor += 1;
                }
            }
            IncomeField::Notes => {
                if self.notes_cursor < self.notes_input.len() {
                    self.notes_cursor += 1;
                }
            }
        }
    }

    /// Clear current field
    pub fn clear_field(&mut self) {
        match self.focused_field {
            IncomeField::Amount => {
                self.amount_input.clear();
                self.amount_cursor = 0;
            }
            IncomeField::Notes => {
                self.notes_input.clear();
                self.notes_cursor = 0;
            }
        }
        self.error_message = None;
    }

    /// Parse the amount input
    pub fn parse_amount(&self) -> Result<Money, String> {
        if self.amount_input.trim().is_empty() {
            return Err("Amount is required".to_string());
        }
        Money::parse(&self.amount_input).map_err(|_| "Invalid amount format".to_string())
    }

    /// Get notes (None if empty)
    pub fn get_notes(&self) -> Option<String> {
        let trimmed = self.notes_input.trim();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed.to_string())
        }
    }

    /// Set error message
    pub fn set_error(&mut self, msg: impl Into<String>) {
        self.error_message = Some(msg.into());
    }
}

/// Render the income dialog
pub fn render(frame: &mut Frame, app: &App) {
    let state = &app.income_form;

    let height = if state.has_existing { 14 } else { 12 };
    let area = centered_rect_fixed(55, height, frame.area());
    frame.render_widget(Clear, area);

    let title = format!(" Expected Income: {} ", app.current_period);
    let block = Block::default()
        .title(title)
        .title_style(
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));

    let inner = block.inner(area);
    frame.render_widget(block, area);

    let mut constraints = vec![
        Constraint::Length(1), // Current income (if exists)
        Constraint::Length(1), // Spacer
        Constraint::Length(1), // Amount label
        Constraint::Length(1), // Amount input
        Constraint::Length(1), // Spacer
        Constraint::Length(1), // Notes label
        Constraint::Length(1), // Notes input
        Constraint::Length(1), // Spacer
        Constraint::Length(1), // Error
        Constraint::Length(1), // Instructions
        Constraint::Min(0),
    ];

    if !state.has_existing {
        constraints.remove(0); // Remove current income row if none exists
        constraints.remove(0); // Remove spacer after current income
    }

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(inner);

    let mut row = 0;

    // Current income (if exists)
    if state.has_existing {
        if let Some(current) = state.current_amount {
            let current_line = Line::from(vec![
                Span::styled("Current:   ", Style::default().fg(Color::Yellow)),
                Span::styled(
                    format!("{}", current),
                    Style::default()
                        .fg(Color::Green)
                        .add_modifier(Modifier::BOLD),
                ),
            ]);
            frame.render_widget(Paragraph::new(current_line), chunks[row]);
        }
        row += 2; // Skip current and spacer
    }

    // Amount label
    let amount_label_style = if state.focused_field == IncomeField::Amount {
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::Yellow)
    };
    frame.render_widget(
        Paragraph::new(Span::styled("Amount:", amount_label_style)),
        chunks[row],
    );
    row += 1;

    // Amount input with cursor
    let amount_line = render_input_with_cursor(
        "$",
        &state.amount_input,
        state.amount_cursor,
        state.focused_field == IncomeField::Amount,
    );
    frame.render_widget(Paragraph::new(amount_line), chunks[row]);
    row += 2; // Skip input and spacer

    // Notes label
    let notes_label_style = if state.focused_field == IncomeField::Notes {
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::Yellow)
    };
    frame.render_widget(
        Paragraph::new(Span::styled("Notes (optional):", notes_label_style)),
        chunks[row],
    );
    row += 1;

    // Notes input with cursor
    let notes_line = render_input_with_cursor(
        "",
        &state.notes_input,
        state.notes_cursor,
        state.focused_field == IncomeField::Notes,
    );
    frame.render_widget(Paragraph::new(notes_line), chunks[row]);
    row += 2; // Skip input and spacer

    // Error message
    if let Some(ref error) = state.error_message {
        let error_line = Line::from(Span::styled(
            error.as_str(),
            Style::default().fg(Color::Red),
        ));
        frame.render_widget(Paragraph::new(error_line), chunks[row]);
    }
    row += 1;

    // Instructions
    let mut instructions = vec![
        Span::styled("[Enter]", Style::default().fg(Color::Green)),
        Span::raw(" Save  "),
        Span::styled("[Esc]", Style::default().fg(Color::Yellow)),
        Span::raw(" Cancel  "),
        Span::styled("[Tab]", Style::default().fg(Color::Cyan)),
        Span::raw(" Fields"),
    ];

    if state.has_existing {
        instructions.push(Span::raw("  "));
        instructions.push(Span::styled("[Del]", Style::default().fg(Color::Magenta)));
        instructions.push(Span::raw(" Remove"));
    }

    frame.render_widget(Paragraph::new(Line::from(instructions)), chunks[row]);
}

fn render_input_with_cursor(
    prefix: &str,
    value: &str,
    cursor: usize,
    focused: bool,
) -> Line<'static> {
    let mut spans = vec![];

    if !prefix.is_empty() {
        spans.push(Span::raw(prefix.to_string()));
    }

    if focused {
        let cursor_pos = cursor.min(value.len());
        let (before, after) = value.split_at(cursor_pos);

        spans.push(Span::styled(
            before.to_string(),
            Style::default().fg(Color::White),
        ));

        let cursor_char = after.chars().next().unwrap_or(' ');
        spans.push(Span::styled(
            cursor_char.to_string(),
            Style::default().fg(Color::Black).bg(Color::Cyan),
        ));

        if after.len() > 1 {
            spans.push(Span::styled(
                after[1..].to_string(),
                Style::default().fg(Color::White),
            ));
        }
    } else {
        spans.push(Span::styled(
            value.to_string(),
            Style::default().fg(Color::White),
        ));
    }

    Line::from(spans)
}

/// Handle key events for the income dialog
pub fn handle_key(app: &mut App, key: crossterm::event::KeyEvent) -> bool {
    use crossterm::event::{KeyCode, KeyModifiers};

    match key.code {
        KeyCode::Esc => {
            app.income_form.reset();
            app.close_dialog();
            true
        }

        KeyCode::Tab => {
            app.income_form.next_field();
            true
        }

        KeyCode::BackTab => {
            app.income_form.prev_field();
            true
        }

        KeyCode::Enter => {
            if let Err(e) = save_income(app) {
                app.income_form.set_error(e);
            }
            true
        }

        KeyCode::Delete => {
            if app.income_form.has_existing {
                if let Err(e) = remove_income(app) {
                    app.income_form.set_error(e);
                }
            }
            true
        }

        KeyCode::Down | KeyCode::Char('j') if key.modifiers.is_empty() => {
            app.income_form.next_field();
            true
        }

        KeyCode::Up | KeyCode::Char('k')
            if key.modifiers.is_empty() && app.income_form.focused_field == IncomeField::Notes =>
        {
            app.income_form.prev_field();
            true
        }

        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.income_form.clear_field();
            true
        }

        KeyCode::Char(c) => {
            app.income_form.insert_char(c);
            true
        }

        KeyCode::Backspace => {
            app.income_form.backspace();
            true
        }

        KeyCode::Left => {
            app.income_form.move_left();
            true
        }

        KeyCode::Right => {
            app.income_form.move_right();
            true
        }

        _ => false,
    }
}

fn save_income(app: &mut App) -> Result<(), String> {
    let period = app.income_form.period.clone().ok_or("No period selected")?;
    let amount = app.income_form.parse_amount()?;
    let notes = app.income_form.get_notes();

    let service = IncomeService::new(app.storage);
    service
        .set_expected_income(&period, amount, notes)
        .map_err(|e| e.to_string())?;

    app.income_form.reset();
    app.close_dialog();
    app.set_status(format!("Expected income for {} set to {}", period, amount));

    Ok(())
}

fn remove_income(app: &mut App) -> Result<(), String> {
    let period = app.income_form.period.clone().ok_or("No period selected")?;

    let service = IncomeService::new(app.storage);
    if service
        .delete_expected_income(&period)
        .map_err(|e| e.to_string())?
    {
        app.income_form.reset();
        app.close_dialog();
        app.set_status(format!("Expected income removed for {}", period));
        Ok(())
    } else {
        Err("No income expectation to remove".to_string())
    }
}