command_vault/ui/
add.rs

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
use std::io::Stdout;
use anyhow::Result;
use crossterm::{
    event::{self, Event, KeyCode, KeyModifiers, KeyEvent},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Style},
    widgets::{Block, Borders, Paragraph, Clear},
    Terminal,
};

/// Type alias for the command result tuple
pub type CommandResult = Option<(String, Vec<String>, Option<i32>)>;

#[derive(Default)]
pub struct AddCommandApp {
    /// The command being entered
    pub command: String,
    /// Tags for the command
    pub tags: Vec<String>,
    /// Current tag being entered
    pub current_tag: String,
    /// Current cursor position in the command
    pub command_cursor: usize,
    /// Current line in multi-line command
    pub command_line: usize,
    /// Current input mode
    pub input_mode: InputMode,
    /// Suggested tags
    pub suggested_tags: Vec<String>,
    /// Previous input mode (for returning from help)
    pub previous_mode: InputMode,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub enum InputMode {
    #[default]
    Command,
    Tag,
    Confirm,
    Help,
}

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

    pub fn run(&mut self) -> Result<CommandResult> {
        let mut terminal = setup_terminal()?;
        let result = self.run_app(&mut terminal);
        restore_terminal(&mut terminal)?;
        result
    }

    fn run_app(&mut self, terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<CommandResult> {
        loop {
            terminal.draw(|f| self.ui(f))?;

            if let Event::Key(key) = event::read()? {
                match self.input_mode {
                    InputMode::Help => match key.code {
                        KeyCode::Char('?') | KeyCode::Esc => {
                            self.input_mode = self.previous_mode.clone();
                        }
                        _ => {}
                    },
                    _ => match key.code {
                        KeyCode::Char('?') => {
                            eprintln!("Debug: ? key pressed, switching to help mode");
                            self.previous_mode = self.input_mode.clone();
                            self.input_mode = InputMode::Help;
                            eprintln!("Debug: Input mode is now Help");
                        }
                        _ => match self.input_mode {
                            InputMode::Command => match key.code {
                                KeyCode::Enter => {
                                    if key.modifiers.contains(KeyModifiers::SHIFT) {
                                        // Add newline to command
                                        self.command.insert(self.command_cursor, '\n');
                                        self.command_cursor += 1;
                                        self.command_line += 1;
                                    } else {
                                        if !self.command.is_empty() {
                                            self.suggest_tags();
                                            self.input_mode = InputMode::Tag;
                                        }
                                    }
                                }
                                KeyCode::Char(c) => {
                                    self.command.insert(self.command_cursor, c);
                                    self.command_cursor += 1;
                                }
                                KeyCode::Backspace => {
                                    if self.command_cursor > 0 {
                                        self.command.remove(self.command_cursor - 1);
                                        self.command_cursor -= 1;
                                        if self.command_cursor > 0 && self.command.chars().nth(self.command_cursor - 1) == Some('\n') {
                                            self.command_line -= 1;
                                        }
                                    }
                                }
                                KeyCode::Left => {
                                    if self.command_cursor > 0 {
                                        self.command_cursor -= 1;
                                        if self.command_cursor > 0 && self.command.chars().nth(self.command_cursor - 1) == Some('\n') {
                                            self.command_line -= 1;
                                        }
                                    }
                                }
                                KeyCode::Right => {
                                    if self.command_cursor < self.command.len() {
                                        if self.command.chars().nth(self.command_cursor) == Some('\n') {
                                            self.command_line += 1;
                                        }
                                        self.command_cursor += 1;
                                    }
                                }
                                KeyCode::Up => {
                                    // Move cursor to previous line
                                    let current_line_start = self.command[..self.command_cursor]
                                        .rfind('\n')
                                        .map(|pos| pos + 1)
                                        .unwrap_or(0);
                                    if let Some(prev_line_start) = self.command[..current_line_start.saturating_sub(1)]
                                        .rfind('\n')
                                        .map(|pos| pos + 1) {
                                        let column = self.command_cursor - current_line_start;
                                        self.command_cursor = prev_line_start + column.min(
                                            self.command[prev_line_start..current_line_start.saturating_sub(1)]
                                                .chars()
                                                .count(),
                                        );
                                        self.command_line -= 1;
                                    }
                                }
                                KeyCode::Down => {
                                    // Move cursor to next line
                                    let current_line_start = self.command[..self.command_cursor]
                                        .rfind('\n')
                                        .map(|pos| pos + 1)
                                        .unwrap_or(0);
                                    if let Some(next_line_start) = self.command[self.command_cursor..]
                                        .find('\n')
                                        .map(|pos| self.command_cursor + pos + 1) {
                                        let column = self.command_cursor - current_line_start;
                                        let next_line_end = self.command[next_line_start..]
                                            .find('\n')
                                            .map(|pos| next_line_start + pos)
                                            .unwrap_or_else(|| self.command.len());
                                        self.command_cursor = next_line_start + column.min(next_line_end - next_line_start);
                                        self.command_line += 1;
                                    }
                                }
                                KeyCode::Esc => {
                                    return Ok(None);
                                }
                                _ => {}
                            },
                            InputMode::Tag => {
                                match key.code {
                                    KeyCode::Enter => {
                                        if !self.current_tag.is_empty() {
                                            self.tags.push(self.current_tag.clone());
                                            self.current_tag.clear();
                                        } else {
                                            self.input_mode = InputMode::Confirm;
                                        }
                                    }
                                    KeyCode::Char(c) => {
                                        self.current_tag.push(c);
                                    }
                                    KeyCode::Backspace => {
                                        self.current_tag.pop();
                                    }
                                    KeyCode::Tab => {
                                        if !self.suggested_tags.is_empty() {
                                            self.tags.push(self.suggested_tags[0].clone());
                                            self.suggested_tags.remove(0);
                                        }
                                    }
                                    KeyCode::Esc => {
                                        self.input_mode = InputMode::Command;
                                    }
                                    _ => {}
                                }
                            }
                            InputMode::Confirm => {
                                match key.code {
                                    KeyCode::Char('y') => {
                                        return Ok(Some((
                                            self.command.clone(),
                                            self.tags.clone(),
                                            None,
                                        )));
                                    }
                                    KeyCode::Char('n') | KeyCode::Esc => {
                                        return Ok(None);
                                    }
                                    _ => {}
                                }
                            }
                            _ => {}
                        }
                    }
                }
            }
        }
    }

    pub fn set_command(&mut self, command: String) {
        self.command = command;
        self.command_cursor = self.command.len();
    }

    pub fn set_tags(&mut self, tags: Vec<String>) {
        self.tags = tags;
    }

    fn ui(&self, f: &mut ratatui::Frame) {
        match self.input_mode {
            InputMode::Help => {
                let help_text = vec![
                    "Command Vault Help",
                    "",
                    "Global Commands:",
                    "  ?      - Toggle this help screen",
                    "  Esc    - Go back / Cancel",
                    "",
                    "Command Input Mode:",
                    "  Enter        - Continue to tag input",
                    "  Shift+Enter  - Add new line",
                    "  ←/→         - Move cursor",
                    "  ↑/↓         - Navigate between lines",
                    "",
                    "Tag Input Mode:",
                    "  Enter  - Add tag",
                    "  Tab    - Show tag suggestions",
                    "",
                    "Confirmation Mode:",
                    "  y/Y    - Save command",
                    "  n/N    - Cancel",
                ];

                let help_paragraph = Paragraph::new(help_text.join("\n"))
                    .style(Style::default().fg(Color::White))
                    .block(Block::default().borders(Borders::ALL).title("Help (press ? or Esc to close)"));

                // Center the help window
                let area = centered_rect(60, 80, f.size());
                f.render_widget(Clear, area); // Clear the background
                f.render_widget(help_paragraph, area);
            }
            _ => {
                let chunks = Layout::default()
                    .direction(Direction::Vertical)
                    .margin(1)
                    .constraints([
                        Constraint::Length(3),  // Title
                        Constraint::Min(5),     // Command input
                        Constraint::Length(3),  // Tags input
                        Constraint::Min(0),     // Message/Help
                    ])
                    .split(f.size());

                // Title
                let title = Paragraph::new("Add Command")
                    .style(Style::default().fg(Color::Cyan))
                    .block(Block::default().borders(Borders::ALL));
                f.render_widget(title, chunks[0]);

                // Command input
                let mut command_text = self.command.clone();
                if self.input_mode == InputMode::Command {
                    command_text.insert(self.command_cursor, '│'); // Add cursor
                }
                let command_input = Paragraph::new(command_text)
                    .style(Style::default().fg(if self.input_mode == InputMode::Command {
                        Color::Yellow
                    } else {
                        Color::Gray
                    }))
                    .block(Block::default().borders(Borders::ALL).title("Command (Shift+Enter for new line)"))
                    .wrap(ratatui::widgets::Wrap { trim: false });
                f.render_widget(command_input, chunks[1]);

                // Tags input
                let mut tags_text = self.tags.join(", ");
                if !tags_text.is_empty() {
                    tags_text.push_str(", ");
                }
                tags_text.push_str(&self.current_tag);
                if self.input_mode == InputMode::Tag {
                    tags_text.push('│');
                }
                let tags_input = Paragraph::new(tags_text)
                    .style(Style::default().fg(if self.input_mode == InputMode::Tag {
                        Color::Yellow
                    } else {
                        Color::Gray
                    }))
                    .block(Block::default().borders(Borders::ALL).title("Tags"));
                f.render_widget(tags_input, chunks[2]);

                // Help text or confirmation prompt
                let help_text = match self.input_mode {
                    InputMode::Command => "Press ? for help",
                    InputMode::Tag => "Press ? for help",
                    InputMode::Confirm => "Save command? (y/n)",
                    InputMode::Help => unreachable!(),
                };
                let help = Paragraph::new(help_text)
                    .style(Style::default().fg(Color::White))
                    .block(Block::default().borders(Borders::ALL));
                f.render_widget(help, chunks[3]);
            }
        }
    }

    fn suggest_tags(&mut self) {
        self.suggested_tags.clear();
        
        // Simple tag suggestions based on command content
        let command = self.command.to_lowercase();
        
        if command.contains("git") {
            self.suggested_tags.push("git".to_string());
            if command.contains("push") {
                self.suggested_tags.push("push".to_string());
            }
            if command.contains("pull") {
                self.suggested_tags.push("pull".to_string());
            }
        }
        
        if command.contains("docker") {
            self.suggested_tags.push("docker".to_string());
        }
        
        if command.contains("cargo") {
            self.suggested_tags.push("rust".to_string());
            self.suggested_tags.push("cargo".to_string());
        }
        
        if command.contains("npm") || command.contains("yarn") {
            self.suggested_tags.push("javascript".to_string());
            self.suggested_tags.push("node".to_string());
        }
    }

    pub fn handle_key_event(&mut self, key: KeyEvent) {
        match self.input_mode {
            InputMode::Help => match key.code {
                KeyCode::Char('?') | KeyCode::Esc => {
                    self.input_mode = self.previous_mode.clone();
                }
                _ => {}
            },
            _ => match key.code {
                KeyCode::Char('?') => {
                    self.previous_mode = self.input_mode.clone();
                    self.input_mode = InputMode::Help;
                }
                _ => match self.input_mode {
                    InputMode::Command => match key.code {
                        KeyCode::Char(c) => {
                            self.command.insert(self.command_cursor, c);
                            self.command_cursor += 1;
                        }
                        KeyCode::Backspace => {
                            if self.command_cursor > 0 {
                                self.command.remove(self.command_cursor - 1);
                                self.command_cursor -= 1;
                            }
                        }
                        KeyCode::Left => {
                            if self.command_cursor > 0 {
                                self.command_cursor -= 1;
                            }
                        }
                        KeyCode::Right => {
                            if self.command_cursor < self.command.len() {
                                self.command_cursor += 1;
                            }
                        }
                        KeyCode::Enter => {
                            if key.modifiers.contains(KeyModifiers::SHIFT) {
                                self.command.insert(self.command_cursor, '\n');
                                self.command_cursor += 1;
                                self.command_line += 1;
                            } else if !self.command.is_empty() {
                                self.input_mode = InputMode::Tag;
                            }
                        }
                        _ => {}
                    },
                    InputMode::Tag => match key.code {
                        KeyCode::Char(c) => {
                            self.current_tag.push(c);
                        }
                        KeyCode::Enter => {
                            if !self.current_tag.is_empty() {
                                self.tags.push(self.current_tag.clone());
                                self.current_tag.clear();
                            }
                        }
                        _ => {}
                    },
                    _ => {}
                }
            }
        }
    }
}

fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
    enable_raw_mode()?;
    let mut stdout = std::io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    terminal.hide_cursor()?;
    Ok(terminal)
}

fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
    terminal.show_cursor()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    disable_raw_mode()?;
    Ok(())
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}