code-mesh-tui 0.1.0

Interactive Terminal User Interface for the Code-Mesh distributed swarm intelligence system
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
use anyhow::Result;
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
use tokio::time::{Duration, Instant};

use crate::{
    chat::ChatComponent,
    components::{CommandPalette, Dialog, StatusBar},
    config::Config,
    events::{AppEvent, EventHandler, InputEvent, KeybindHandler, MouseHandler},
    file_viewer::FileViewer,
    layout::{LayoutManager, PopupLayout},
    renderer::Renderer,
    theme::ThemeManager,
};

/// Main application state
pub struct App {
    /// Application configuration
    config: Config,
    /// Theme manager
    theme_manager: ThemeManager,
    /// Event handler
    event_handler: EventHandler,
    /// Keybind handler
    keybind_handler: KeybindHandler,
    /// Mouse handler
    mouse_handler: MouseHandler,
    /// Layout manager
    layout_manager: LayoutManager,
    /// Chat component
    chat: ChatComponent,
    /// File viewer component
    file_viewer: FileViewer,
    /// Status bar component
    status_bar: StatusBar,
    /// Command palette
    command_palette: CommandPalette,
    /// Active dialog
    active_dialog: Option<Dialog>,
    /// Application state
    state: AppState,
    /// Last render time
    last_render: Instant,
    /// Frame rate target
    target_fps: u64,
}

/// Application state enumeration
#[derive(Debug, Clone, PartialEq)]
pub enum AppState {
    /// Normal operation
    Running,
    /// Application should quit
    Quitting,
    /// Showing help
    Help,
    /// Command palette is open
    CommandPalette,
    /// Dialog is open
    Dialog,
    /// File viewer is focused
    FileViewer,
    /// Chat is focused
    Chat,
}

impl App {
    /// Create a new application instance
    pub async fn new(config: Config) -> Result<Self> {
        let theme_manager = ThemeManager::default();
        let event_handler = EventHandler::new();
        let mut keybind_handler = KeybindHandler::new();
        
        // Setup keybindings from config
        keybind_handler.set_leader_key(config.keybinds.leader.clone());
        for (action, key) in &config.keybinds.bindings {
            keybind_handler.bind(key.clone(), action.clone());
        }
        
        let mouse_handler = MouseHandler::new();
        
        // Initialize layout with default terminal size
        let layout_manager = LayoutManager::new(ratatui::layout::Rect::new(0, 0, 80, 24));
        
        // Initialize components
        let chat = ChatComponent::new(&config.chat, theme_manager.current_theme());
        let file_viewer = FileViewer::new(&config.file_viewer, theme_manager.current_theme());
        let status_bar = StatusBar::new(theme_manager.current_theme());
        let command_palette = CommandPalette::new(theme_manager.current_theme());
        
        Ok(Self {
            config,
            theme_manager,
            event_handler,
            keybind_handler,
            mouse_handler,
            layout_manager,
            chat,
            file_viewer,
            status_bar,
            command_palette,
            active_dialog: None,
            state: AppState::Running,
            last_render: Instant::now(),
            target_fps: 60,
        })
    }
    
    /// Run the application main loop
    pub async fn run(&mut self) -> Result<()> {
        let mut terminal = crate::init_terminal()?;
        
        // Main application loop
        while self.state != AppState::Quitting {
            // Handle events
            if let Some(event) = self.event_handler.try_next() {
                self.handle_event(event).await?;
            }
            
            // Render at target FPS
            let now = Instant::now();
            let frame_duration = Duration::from_millis(1000 / self.target_fps);
            
            if now.duration_since(self.last_render) >= frame_duration {
                self.render(&mut terminal)?;
                self.last_render = now;
            }
            
            // Small sleep to prevent busy waiting
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
        
        crate::restore_terminal(&mut terminal)?;
        Ok(())
    }
    
    /// Handle an application event
    async fn handle_event(&mut self, event: AppEvent) -> Result<()> {
        match event {
            AppEvent::Input(input_event) => {
                self.handle_input_event(input_event).await?;
            }
            AppEvent::Resize(width, height) => {
                let new_area = ratatui::layout::Rect::new(0, 0, width, height);
                self.layout_manager.resize(new_area);
            }
            AppEvent::Quit => {
                self.state = AppState::Quitting;
            }
            AppEvent::Tick => {
                // Handle periodic updates
                self.update_components().await?;
            }
            AppEvent::Custom(message) => {
                self.handle_custom_event(message).await?;
            }
        }
        Ok(())
    }
    
    /// Handle input events
    async fn handle_input_event(&mut self, event: InputEvent) -> Result<()> {
        match event {
            InputEvent::Key(key_event) => {
                // Check for global keybindings first
                if let Some(action) = self.keybind_handler.handle_key(&key_event) {
                    self.execute_action(&action).await?;
                } else {
                    // Route to active component
                    self.route_key_event(key_event).await?;
                }
            }
            InputEvent::Mouse(mouse_event) => {
                let action = self.mouse_handler.handle_mouse(&mouse_event);
                self.handle_mouse_action(action).await?;
            }
            InputEvent::Paste(data) => {
                // Route paste to active component
                if self.state == AppState::Chat {
                    self.chat.handle_paste(data).await?;
                }
            }
            InputEvent::FocusGained | InputEvent::FocusLost => {
                // Handle focus changes if needed
            }
        }
        Ok(())
    }
    
    /// Execute a bound action
    async fn execute_action(&mut self, action: &str) -> Result<()> {
        match action {
            "quit" => {
                self.state = AppState::Quitting;
            }
            "help" => {
                self.toggle_help();
            }
            "command_palette" => {
                self.toggle_command_palette();
            }
            "send_message" => {
                if self.state == AppState::Chat {
                    self.chat.send_message().await?;
                }
            }
            "new_line" => {
                if self.state == AppState::Chat {
                    self.chat.insert_newline();
                }
            }
            "clear_input" => {
                if self.state == AppState::Chat {
                    self.chat.clear_input();
                }
            }
            "open_file" => {
                self.open_file_dialog();
            }
            "close_file" => {
                self.file_viewer.close_file();
                if self.state == AppState::FileViewer {
                    self.state = AppState::Chat;
                }
            }
            "toggle_diff" => {
                self.file_viewer.toggle_diff_style();
            }
            "scroll_up" => {
                self.handle_scroll(true).await?;
            }
            "scroll_down" => {
                self.handle_scroll(false).await?;
            }
            "page_up" => {
                self.handle_page_scroll(true).await?;
            }
            "page_down" => {
                self.handle_page_scroll(false).await?;
            }
            _ => {
                // Unknown action, ignore or log
            }
        }
        Ok(())
    }
    
    /// Route key events to the appropriate component
    async fn route_key_event(&mut self, key_event: crossterm::event::KeyEvent) -> Result<()> {
        match self.state {
            AppState::Chat => {
                self.chat.handle_key_event(key_event).await?;
            }
            AppState::FileViewer => {
                self.file_viewer.handle_key_event(key_event).await?;
            }
            AppState::CommandPalette => {
                if let Some(result) = self.command_palette.handle_key_event(key_event).await? {
                    self.execute_command_palette_result(result).await?;
                    self.state = AppState::Chat;
                }
            }
            AppState::Dialog => {
                if let Some(ref mut dialog) = self.active_dialog {
                    if let Some(result) = dialog.handle_key_event(key_event).await? {
                        self.handle_dialog_result(result).await?;
                        self.active_dialog = None;
                        self.state = AppState::Chat;
                    }
                }
            }
            _ => {}
        }
        Ok(())
    }
    
    /// Handle mouse actions
    async fn handle_mouse_action(&mut self, action: crate::events::MouseAction) -> Result<()> {
        use crate::events::MouseAction;
        
        match action {
            MouseAction::LeftClick(x, y) => {
                // Determine which component was clicked
                if self.layout_manager.main_area.intersects(ratatui::layout::Rect::new(x, y, 1, 1)) {
                    if self.file_viewer.is_visible() {
                        self.state = AppState::FileViewer;
                    } else {
                        self.state = AppState::Chat;
                    }
                }
            }
            MouseAction::ScrollUp(x, y) => {
                if self.is_in_scrollable_area(x, y) {
                    self.handle_scroll(true).await?;
                }
            }
            MouseAction::ScrollDown(x, y) => {
                if self.is_in_scrollable_area(x, y) {
                    self.handle_scroll(false).await?;
                }
            }
            _ => {}
        }
        Ok(())
    }
    
    /// Update components periodically
    async fn update_components(&mut self) -> Result<()> {
        self.chat.update().await?;
        self.file_viewer.update().await?;
        self.status_bar.update(&self.state).await?;
        Ok(())
    }
    
    /// Handle custom application events
    async fn handle_custom_event(&mut self, message: String) -> Result<()> {
        // Parse and handle custom events
        // This could be used for inter-component communication
        match message.as_str() {
            "theme_changed" => {
                self.update_theme();
            }
            "file_opened" => {
                self.state = AppState::FileViewer;
            }
            _ => {}
        }
        Ok(())
    }
    
    /// Render the application
    fn render(&mut self, terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -> Result<()> {
        terminal.draw(|frame| {
            let mut renderer = Renderer::new(frame, self.theme_manager.current_theme());
            
            // Render main layout
            self.render_main_layout(&mut renderer);
            
            // Render popups/dialogs on top
            self.render_overlays(&mut renderer);
        })?;
        Ok(())
    }
    
    /// Render the main application layout
    fn render_main_layout(&mut self, renderer: &mut Renderer) {
        // Render status bar
        self.status_bar.render(renderer, self.layout_manager.status_area);
        
        // Render main content area
        if self.file_viewer.is_visible() {
            // Show file viewer in side panel if available, or full area
            if let Some(side_panel) = self.layout_manager.side_panel {
                self.chat.render(renderer, self.layout_manager.main_area);
                self.file_viewer.render(renderer, side_panel);
            } else {
                self.file_viewer.render(renderer, self.layout_manager.main_area);
            }
        } else {
            self.chat.render(renderer, self.layout_manager.main_area);
        }
        
        // Render input area
        self.chat.render_input(renderer, self.layout_manager.input_area);
    }
    
    /// Render overlay components like dialogs and command palette
    fn render_overlays(&mut self, renderer: &mut Renderer) {
        match self.state {
            AppState::CommandPalette => {
                let popup_area = PopupLayout::centered(
                    self.layout_manager.terminal_area,
                    60,
                    15,
                );
                self.command_palette.render(renderer, popup_area);
            }
            AppState::Dialog => {
                if let Some(ref mut dialog) = self.active_dialog {
                    let popup_area = PopupLayout::centered(
                        self.layout_manager.terminal_area,
                        dialog.width(),
                        dialog.height(),
                    );
                    dialog.render(renderer, popup_area);
                }
            }
            AppState::Help => {
                let popup_area = PopupLayout::percentage(
                    self.layout_manager.terminal_area,
                    80,
                    80,
                );
                self.render_help(renderer, popup_area);
            }
            _ => {}
        }
    }
    
    /// Toggle help display
    fn toggle_help(&mut self) {
        self.state = if self.state == AppState::Help {
            AppState::Chat
        } else {
            AppState::Help
        };
    }
    
    /// Toggle command palette
    fn toggle_command_palette(&mut self) {
        self.state = if self.state == AppState::CommandPalette {
            AppState::Chat
        } else {
            AppState::CommandPalette
        };
    }
    
    /// Open file dialog
    fn open_file_dialog(&mut self) {
        // Implementation would create a file picker dialog
        // For now, this is a placeholder
    }
    
    /// Handle scrolling
    async fn handle_scroll(&mut self, up: bool) -> Result<()> {
        match self.state {
            AppState::Chat => {
                if up {
                    self.chat.scroll_up();
                } else {
                    self.chat.scroll_down();
                }
            }
            AppState::FileViewer => {
                if up {
                    self.file_viewer.scroll_up();
                } else {
                    self.file_viewer.scroll_down();
                }
            }
            _ => {}
        }
        Ok(())
    }
    
    /// Handle page scrolling
    async fn handle_page_scroll(&mut self, up: bool) -> Result<()> {
        match self.state {
            AppState::Chat => {
                if up {
                    self.chat.page_up();
                } else {
                    self.chat.page_down();
                }
            }
            AppState::FileViewer => {
                if up {
                    self.file_viewer.page_up();
                } else {
                    self.file_viewer.page_down();
                }
            }
            _ => {}
        }
        Ok(())
    }
    
    /// Check if coordinates are in a scrollable area
    fn is_in_scrollable_area(&self, x: u16, y: u16) -> bool {
        let point = ratatui::layout::Rect::new(x, y, 1, 1);
        self.layout_manager.main_area.intersects(point) ||
        self.layout_manager.side_panel.map_or(false, |area| area.intersects(point))
    }
    
    /// Execute command palette result
    async fn execute_command_palette_result(&mut self, result: String) -> Result<()> {
        // Parse and execute command palette commands
        match result.as_str() {
            "open-file" => self.open_file_dialog(),
            "toggle-theme" => self.cycle_theme(),
            "clear-chat" => self.chat.clear().await?,
            _ => {}
        }
        Ok(())
    }
    
    /// Handle dialog result
    async fn handle_dialog_result(&mut self, result: crate::components::DialogResult) -> Result<()> {
        use crate::components::DialogResult;
        
        match result {
            DialogResult::Confirmed(_data) => {
                // Handle confirmed dialog with data
            }
            DialogResult::Cancelled => {
                // Handle cancelled dialog
            }
        }
        Ok(())
    }
    
    /// Update theme for all components
    fn update_theme(&mut self) {
        let theme = self.theme_manager.current_theme();
        self.chat.update_theme(theme);
        self.file_viewer.update_theme(theme);
        self.status_bar.update_theme(theme);
        self.command_palette.update_theme(theme);
    }
    
    /// Cycle through available themes
    fn cycle_theme(&mut self) {
        let themes = self.theme_manager.available_themes();
        if !themes.is_empty() {
            let current_name = self.theme_manager.current_theme().name();
            let current_index = themes.iter().position(|name| name == current_name).unwrap_or(0);
            let next_index = (current_index + 1) % themes.len();
            let next_theme = &themes[next_index];
            
            if let Err(e) = self.theme_manager.set_theme(next_theme) {
                eprintln!("Failed to set theme {}: {}", next_theme, e);
            } else {
                self.update_theme();
            }
        }
    }
    
    /// Render help overlay
    fn render_help(&self, renderer: &mut Renderer, area: ratatui::layout::Rect) {
        // Implementation would render help content
        // This is a placeholder
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;

    #[tokio::test]
    async fn test_app_creation() {
        let config = Config::default();
        let app = App::new(config).await;
        assert!(app.is_ok());
    }

    #[tokio::test]
    async fn test_app_state_transitions() {
        let config = Config::default();
        let mut app = App::new(config).await.unwrap();
        
        assert_eq!(app.state, AppState::Running);
        
        app.toggle_help();
        assert_eq!(app.state, AppState::Help);
        
        app.toggle_help();
        assert_eq!(app.state, AppState::Chat);
    }
}