agcodex-tui 0.1.0

Terminal User Interface for AGCodex with mode switching support
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//! Enhanced save session dialog for AGCodex TUI
//! Provides popup dialog with session naming, progress bar, and auto-save integration

use chrono::Local;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::KeyCode;
use ratatui::crossterm::event::KeyEvent;
use ratatui::layout::Alignment;
use ratatui::layout::Constraint;
use ratatui::layout::Direction;
use ratatui::layout::Layout;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Modifier;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::widgets::Block;
use ratatui::widgets::BorderType;
use ratatui::widgets::Borders;
use ratatui::widgets::Clear;
use ratatui::widgets::Gauge;
use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;
use ratatui::widgets::WidgetRef;
use ratatui::widgets::Wrap;
use std::path::PathBuf;
use std::time::Duration;
use std::time::Instant;
use uuid::Uuid;

/// State for the save session dialog
#[derive(Debug, Clone)]
pub struct SaveSessionState {
    /// Session name input
    pub session_name: String,
    /// Optional description input
    pub description: String,
    /// Currently focused field (0: name, 1: description, 2: save button, 3: cancel button)
    pub focused_field: usize,
    /// Cursor position in the currently focused text field
    pub cursor_pos: usize,
    /// Save location path (~/.agcodex/history/)
    pub save_location: PathBuf,
    /// Whether to show validation errors
    pub show_error: Option<String>,
    /// Save progress (0.0 to 1.0)
    pub save_progress: f64,
    /// Whether save operation is in progress
    pub saving: bool,
    /// Start time of save operation for progress estimation
    pub save_start_time: Option<Instant>,
    /// Session ID being saved
    pub session_id: Option<Uuid>,
    /// Auto-generated timestamp name
    pub default_name: String,
    /// Show success message
    pub show_success: bool,
    /// Estimated time remaining
    pub estimated_time: Option<Duration>,
}

impl SaveSessionState {
    pub fn new() -> Self {
        let save_location = dirs::home_dir()
            .map(|p| p.join(".agcodex/history"))
            .unwrap_or_else(|| PathBuf::from(".agcodex/history"));

        // Generate default timestamp name
        let default_name = Local::now().format("%Y-%m-%d_%H-%M").to_string();

        Self {
            session_name: default_name.clone(),
            description: String::new(),
            focused_field: 0,
            cursor_pos: default_name.len(),
            save_location,
            show_error: None,
            save_progress: 0.0,
            saving: false,
            save_start_time: None,
            session_id: None,
            default_name,
            show_success: false,
            estimated_time: None,
        }
    }

    /// Handle key input for the dialog
    pub fn handle_key_event(&mut self, key: KeyEvent) -> SaveSessionAction {
        if self.saving {
            // Only allow Esc during save
            if key.code == KeyCode::Esc {
                return SaveSessionAction::Cancel;
            }
            return SaveSessionAction::None;
        }

        if self.show_success {
            // Any key closes success dialog
            return SaveSessionAction::Close;
        }

        match key.code {
            KeyCode::Esc => SaveSessionAction::Cancel,
            KeyCode::Enter => {
                match self.focused_field {
                    0 | 1 => {
                        // Enter in text fields moves to next field or saves
                        if self.focused_field == 0 && !self.session_name.trim().is_empty() {
                            self.focused_field = 1;
                            self.cursor_pos = self.description.len();
                        } else if self.focused_field == 1 {
                            self.focused_field = 2; // Move to save button
                        }
                        SaveSessionAction::None
                    }
                    2 => {
                        // Save button
                        if self.validate() {
                            SaveSessionAction::Save
                        } else {
                            SaveSessionAction::None
                        }
                    }
                    3 => SaveSessionAction::Cancel, // Cancel button
                    _ => SaveSessionAction::None,
                }
            }
            KeyCode::Tab => {
                self.next_field();
                SaveSessionAction::None
            }
            KeyCode::BackTab => {
                self.prev_field();
                SaveSessionAction::None
            }
            KeyCode::Char(c) => {
                if self.focused_field <= 1 {
                    // Limit input length
                    let current_text = if self.focused_field == 0 {
                        &self.session_name
                    } else {
                        &self.description
                    };

                    if current_text.len() < 100 {
                        self.insert_char(c);
                    }
                }
                SaveSessionAction::None
            }
            KeyCode::Backspace => {
                if self.focused_field <= 1 {
                    self.delete_char();
                }
                SaveSessionAction::None
            }
            KeyCode::Delete => {
                if self.focused_field <= 1 {
                    self.delete_char_forward();
                }
                SaveSessionAction::None
            }
            KeyCode::Left => {
                if self.focused_field <= 1 {
                    self.move_cursor_left();
                }
                SaveSessionAction::None
            }
            KeyCode::Right => {
                if self.focused_field <= 1 {
                    self.move_cursor_right();
                }
                SaveSessionAction::None
            }
            KeyCode::Home => {
                if self.focused_field <= 1 {
                    self.cursor_pos = 0;
                }
                SaveSessionAction::None
            }
            KeyCode::End => {
                if self.focused_field <= 1 {
                    self.cursor_pos = self.current_field_text().len();
                }
                SaveSessionAction::None
            }
            _ => SaveSessionAction::None,
        }
    }

    fn next_field(&mut self) {
        self.focused_field = (self.focused_field + 1) % 4;
        if self.focused_field <= 1 {
            self.cursor_pos = self.current_field_text().len();
        }
    }

    fn prev_field(&mut self) {
        self.focused_field = if self.focused_field == 0 {
            3
        } else {
            self.focused_field - 1
        };
        if self.focused_field <= 1 {
            self.cursor_pos = self.current_field_text().len();
        }
    }

    fn current_field_text(&self) -> &str {
        match self.focused_field {
            0 => &self.session_name,
            1 => &self.description,
            _ => "",
        }
    }

    fn current_field_text_mut(&mut self) -> &mut String {
        match self.focused_field {
            0 => &mut self.session_name,
            1 => &mut self.description,
            _ => panic!("Invalid field for text mutation"),
        }
    }

    fn insert_char(&mut self, c: char) {
        if self.focused_field <= 1 {
            let cursor_pos = self.cursor_pos;
            let text = self.current_field_text_mut();
            text.insert(cursor_pos, c);
            self.cursor_pos = cursor_pos + c.len_utf8();
            self.show_error = None;
        }
    }

    fn delete_char(&mut self) {
        if self.focused_field <= 1 && self.cursor_pos > 0 {
            let cursor_pos = self.cursor_pos;
            let text = self.current_field_text_mut();
            let char_boundary = text
                .char_indices()
                .rev()
                .find(|(idx, _)| *idx < cursor_pos)
                .map(|(idx, _)| idx)
                .unwrap_or(0);
            text.drain(char_boundary..cursor_pos);
            self.cursor_pos = char_boundary;
            self.show_error = None;
        }
    }

    fn delete_char_forward(&mut self) {
        if self.focused_field <= 1 {
            let cursor_pos = self.cursor_pos;
            let text = self.current_field_text_mut();
            if cursor_pos < text.len() {
                let next_char_boundary = text
                    .char_indices()
                    .find(|(idx, _)| *idx > cursor_pos)
                    .map(|(idx, _)| idx)
                    .unwrap_or(text.len());
                text.drain(cursor_pos..next_char_boundary);
            }
        }
    }

    fn move_cursor_left(&mut self) {
        if self.focused_field <= 1 && self.cursor_pos > 0 {
            let text = self.current_field_text();
            self.cursor_pos = text
                .char_indices()
                .rev()
                .find(|(idx, _)| *idx < self.cursor_pos)
                .map(|(idx, _)| idx)
                .unwrap_or(0);
        }
    }

    fn move_cursor_right(&mut self) {
        if self.focused_field <= 1 {
            let text = self.current_field_text();
            if self.cursor_pos < text.len() {
                self.cursor_pos = text
                    .char_indices()
                    .find(|(idx, _)| *idx > self.cursor_pos)
                    .map(|(idx, _)| idx)
                    .unwrap_or(text.len());
            }
        }
    }

    /// Validate the dialog input
    pub fn validate(&mut self) -> bool {
        let name = self.session_name.trim();
        if name.is_empty() {
            self.show_error = Some("Session name cannot be empty".to_string());
            self.focused_field = 0;
            return false;
        }

        // Check for invalid characters
        if name
            .chars()
            .any(|c| matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|'))
        {
            self.show_error = Some("Session name contains invalid characters".to_string());
            self.focused_field = 0;
            return false;
        }

        // Check length
        if name.len() > 100 {
            self.show_error = Some("Session name too long (max 100 characters)".to_string());
            self.focused_field = 0;
            return false;
        }

        self.show_error = None;
        true
    }

    /// Start save operation
    pub fn start_save(&mut self, session_id: Uuid) {
        self.saving = true;
        self.save_progress = 0.0;
        self.save_start_time = Some(Instant::now());
        self.session_id = Some(session_id);
        self.show_error = None;
    }

    /// Update save progress
    pub fn update_progress(&mut self, progress: f64) {
        self.save_progress = progress.clamp(0.0, 1.0);

        // Estimate time remaining based on elapsed time
        if let Some(start_time) = self.save_start_time {
            let elapsed = start_time.elapsed();
            if progress > 0.0 && progress < 1.0 {
                let total_estimated = elapsed.as_secs_f64() / progress;
                let remaining = total_estimated - elapsed.as_secs_f64();
                self.estimated_time = Some(Duration::from_secs_f64(remaining));
            }
        }
    }

    /// Complete save operation
    pub const fn complete_save(&mut self) {
        self.saving = false;
        self.save_progress = 1.0;
        self.show_success = true;
        self.estimated_time = None;
    }

    /// Set error message
    pub fn set_error(&mut self, error: String) {
        self.show_error = Some(error);
        self.saving = false;
        self.save_progress = 0.0;
        self.estimated_time = None;
    }

    /// Reset to initial state
    pub fn reset(&mut self) {
        *self = Self::new();
    }
}

impl Default for SaveSessionState {
    fn default() -> Self {
        Self::new()
    }
}

/// Actions that can be triggered by the save dialog
#[derive(Debug, Clone, PartialEq)]
pub enum SaveSessionAction {
    None,
    Save,
    Cancel,
    Close,
}

/// Enhanced Save Session Dialog widget with progress bar
pub struct SaveSessionDialog<'a> {
    state: &'a SaveSessionState,
}

impl<'a> SaveSessionDialog<'a> {
    pub const fn new(state: &'a SaveSessionState) -> Self {
        Self { state }
    }

    fn render_input_field(
        &self,
        area: Rect,
        buf: &mut Buffer,
        title: &str,
        text: &str,
        placeholder: &str,
        focused: bool,
        cursor_pos: usize,
    ) {
        let block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(if focused {
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Gray)
            });

        let inner = block.inner(area);
        block.render(area, buf);

        // Render text or placeholder
        let content = if text.is_empty() && !focused {
            Span::styled(
                placeholder,
                Style::default()
                    .fg(Color::DarkGray)
                    .add_modifier(Modifier::ITALIC),
            )
        } else {
            Span::raw(text)
        };

        let paragraph = Paragraph::new(Line::from(content));
        paragraph.render(inner, buf);

        // Render cursor if focused
        if focused && inner.width > 0 {
            let visible_cursor = cursor_pos.min(text.len());
            let cursor_x = inner.x + (visible_cursor as u16).min(inner.width - 1);
            if cursor_x < inner.right()
                && let Some(cell) = buf.cell_mut((cursor_x, inner.y))
            {
                cell.set_style(Style::default().bg(Color::White).fg(Color::Black));
            }
        }
    }

    fn render_button(
        &self,
        area: Rect,
        buf: &mut Buffer,
        text: &str,
        focused: bool,
        disabled: bool,
    ) {
        let style = if disabled {
            Style::default().fg(Color::DarkGray)
        } else if focused {
            Style::default()
                .bg(Color::Cyan)
                .fg(Color::Black)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::White)
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(if focused && !disabled {
                Style::default().fg(Color::Cyan)
            } else {
                Style::default().fg(Color::DarkGray)
            });

        let paragraph = Paragraph::new(text)
            .alignment(Alignment::Center)
            .style(style);

        let inner_area = block.inner(area);
        block.render(area, buf);
        paragraph.render(inner_area, buf);
    }

    fn render_progress_bar(&self, area: Rect, buf: &mut Buffer) {
        let progress_text = if let Some(duration) = self.state.estimated_time {
            format!(
                "Saving... {:.0}% - {}s remaining",
                self.state.save_progress * 100.0,
                duration.as_secs()
            )
        } else {
            format!("Saving... {:.0}%", self.state.save_progress * 100.0)
        };

        let gauge = Gauge::default()
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_type(BorderType::Rounded)
                    .title(" Progress "),
            )
            .gauge_style(Style::default().fg(Color::Cyan).bg(Color::Black))
            .percent((self.state.save_progress * 100.0) as u16)
            .label(progress_text);

        gauge.render(area, buf);
    }

    fn render_success_message(&self, area: Rect, buf: &mut Buffer) {
        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Double)
            .border_style(Style::default().fg(Color::Green))
            .title(" Success ");

        let inner = block.inner(area);
        block.render(area, buf);

        let success_text = vec![
            Line::from(""),
            Line::from(vec![
                Span::raw(""),
                Span::styled(
                    "Session saved successfully!",
                    Style::default()
                        .fg(Color::Green)
                        .add_modifier(Modifier::BOLD),
                ),
            ]),
            Line::from(""),
            Line::from(vec![
                Span::raw("Name: "),
                Span::styled(&self.state.session_name, Style::default().fg(Color::White)),
            ]),
            Line::from(vec![
                Span::raw("Location: "),
                Span::styled(
                    self.state.save_location.display().to_string(),
                    Style::default().fg(Color::DarkGray),
                ),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                "Press any key to continue",
                Style::default()
                    .fg(Color::DarkGray)
                    .add_modifier(Modifier::ITALIC),
            )),
        ];

        let paragraph = Paragraph::new(success_text)
            .alignment(Alignment::Center)
            .wrap(Wrap { trim: true });

        paragraph.render(inner, buf);
    }
}

impl<'a> WidgetRef for SaveSessionDialog<'a> {
    fn render_ref(&self, area: Rect, buf: &mut Buffer) {
        // Clear the background
        Clear.render(area, buf);

        // Calculate dialog dimensions
        let dialog_width = 70;
        let dialog_height = if self.state.show_success {
            12
        } else if self.state.saving {
            10
        } else {
            18
        };

        let x = area.width.saturating_sub(dialog_width) / 2;
        let y = area.height.saturating_sub(dialog_height) / 2;
        let dialog_area = Rect::new(x, y, dialog_width, dialog_height);

        // Show success message if save completed
        if self.state.show_success {
            self.render_success_message(dialog_area, buf);
            return;
        }

        // Main dialog block
        let block = Block::default()
            .title(" Save Session ")
            .borders(Borders::ALL)
            .border_type(BorderType::Double)
            .border_style(Style::default().fg(Color::Blue));

        let inner = block.inner(dialog_area);
        block.render(dialog_area, buf);

        if self.state.saving {
            // Show progress bar during save
            let layout = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(2), // Info text
                    Constraint::Length(1), // Spacing
                    Constraint::Length(3), // Progress bar
                    Constraint::Length(1), // Spacing
                    Constraint::Length(1), // Cancel hint
                ])
                .split(inner);

            let info_text = format!("Saving session: {}", self.state.session_name);
            let info = Paragraph::new(info_text)
                .alignment(Alignment::Center)
                .style(Style::default().fg(Color::White));
            info.render(layout[0], buf);

            self.render_progress_bar(layout[2], buf);

            let cancel_hint = Paragraph::new("Press Esc to cancel")
                .alignment(Alignment::Center)
                .style(Style::default().fg(Color::DarkGray));
            cancel_hint.render(layout[4], buf);
        } else {
            // Normal input mode
            let layout = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(2), // Save location
                    Constraint::Length(1), // Spacing
                    Constraint::Length(3), // Session name
                    Constraint::Length(3), // Description
                    Constraint::Length(1), // Spacing
                    Constraint::Length(2), // Error message
                    Constraint::Length(1), // Spacing
                    Constraint::Length(3), // Buttons
                ])
                .split(inner);

            // Save location info
            let location_text = format!("📁 {}", self.state.save_location.display());
            let location = Paragraph::new(location_text)
                .style(Style::default().fg(Color::DarkGray))
                .alignment(Alignment::Center);
            location.render(layout[0], buf);

            // Session name input
            self.render_input_field(
                layout[2],
                buf,
                "Session Name",
                &self.state.session_name,
                &format!("e.g., {}", self.state.default_name),
                self.state.focused_field == 0,
                if self.state.focused_field == 0 {
                    self.state.cursor_pos
                } else {
                    0
                },
            );

            // Description input
            self.render_input_field(
                layout[3],
                buf,
                "Description (optional)",
                &self.state.description,
                "Brief description of this session...",
                self.state.focused_field == 1,
                if self.state.focused_field == 1 {
                    self.state.cursor_pos
                } else {
                    0
                },
            );

            // Error message
            if let Some(ref error) = self.state.show_error {
                let error_text = format!("{}", error);
                let error_paragraph = Paragraph::new(error_text)
                    .style(Style::default().fg(Color::Red))
                    .alignment(Alignment::Center)
                    .wrap(Wrap { trim: true });
                error_paragraph.render(layout[5], buf);
            }

            // Buttons
            let button_layout = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([
                    Constraint::Percentage(20),
                    Constraint::Percentage(25),
                    Constraint::Percentage(10),
                    Constraint::Percentage(25),
                    Constraint::Percentage(20),
                ])
                .split(layout[7]);

            self.render_button(
                button_layout[1],
                buf,
                "💾 Save",
                self.state.focused_field == 2,
                self.state.session_name.trim().is_empty(),
            );

            self.render_button(
                button_layout[3],
                buf,
                "Cancel",
                self.state.focused_field == 3,
                false,
            );
        }

        // Help text at the bottom
        let help_text = if self.state.saving {
            "Saving session... Please wait"
        } else if self.state.show_success {
            "Session saved! Press any key to continue"
        } else {
            "Tab: Navigate • Enter: Confirm • Esc: Cancel • Ctrl+S: Quick Save"
        };

        let help_area = Rect::new(
            dialog_area.x,
            dialog_area.y + dialog_area.height,
            dialog_area.width,
            1,
        );

        if help_area.y < area.height {
            let help = Paragraph::new(help_text)
                .style(Style::default().fg(Color::DarkGray))
                .alignment(Alignment::Center);
            help.render(help_area, buf);
        }
    }
}