ai-code-buddy 0.4.20

An AI-powered code review tool with elegant Bevy-based TUI
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
use bevy::prelude::*;
use bevy_ratatui::{error::exit_on_error, terminal::RatatuiContext};
use crossterm::event::{KeyCode, KeyEventKind, MouseEventKind};
use ratatui::{
    buffer::Buffer,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, StatefulWidgetRef, WidgetRef},
};

use crate::{
    args::Args,
    bevy_states::app::AppState,
    events::{app::AppEvent, overview::OverviewEvent},
    theme::THEME,
    version::APP_VERSION,
    widget_states::overview::{OverviewComponent, OverviewWidgetState, SelectionDirection},
};

pub struct OverviewPlugin;

impl Plugin for OverviewPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<OverviewEvent>()
            .init_resource::<OverviewWidgetState>()
            .add_systems(Startup, initialize_overview_state)
            .add_systems(PreUpdate, overview_event_handler)
            .add_systems(Update, render_overview.pipe(exit_on_error));
    }
}

pub fn initialize_overview_state(mut overview_state: ResMut<OverviewWidgetState>, args: Res<Args>) {
    overview_state.repo_info.path = args.repo_path.clone();
    overview_state.repo_info.source_branch = args.source_branch.clone();
    overview_state.repo_info.target_branch = args.target_branch.clone();

    // TODO: Calculate files to analyze
    overview_state.repo_info.files_to_analyze = 42; // Placeholder
}

pub fn overview_event_handler(
    mut overview_events: EventReader<OverviewEvent>,
    mut overview_state: ResMut<OverviewWidgetState>,
    mut app_events: EventWriter<AppEvent>,
) {
    for event in overview_events.read() {
        match event {
            OverviewEvent::KeyEvent(key_event) => {
                if key_event.kind == KeyEventKind::Release {
                    // If help is showing, any key closes it
                    if overview_state.show_help {
                        overview_state.show_help = false;
                        return;
                    }

                    match key_event.code {
                        KeyCode::Tab => {
                            overview_state.move_selection(SelectionDirection::Next);
                        }
                        KeyCode::BackTab => {
                            overview_state.move_selection(SelectionDirection::Previous);
                        }
                        KeyCode::Up => {
                            overview_state.move_selection(SelectionDirection::Previous);
                        }
                        KeyCode::Down => {
                            overview_state.move_selection(SelectionDirection::Next);
                        }
                        KeyCode::Enter => match overview_state.selected_component {
                            OverviewComponent::Help => {
                                overview_state.show_help = !overview_state.show_help;
                            }
                            _ => {
                                handle_selection(
                                    &overview_state.selected_component,
                                    &mut app_events,
                                );
                            }
                        },
                        _ => {}
                    }
                }
            }
            OverviewEvent::MouseEvent(mouse_event) => {
                // If help is showing, any click closes it
                if overview_state.show_help {
                    if let MouseEventKind::Up(_) = mouse_event.kind {
                        overview_state.show_help = false;
                    }
                    return;
                }

                match mouse_event.kind {
                    MouseEventKind::Up(_) => {
                        let x = mouse_event.column;
                        let y = mouse_event.row;

                        let components: Vec<_> = overview_state
                            .registered_components
                            .clone()
                            .into_iter()
                            .collect();
                        for (component, _rect) in components {
                            if overview_state.is_over(component.clone(), x, y) {
                                overview_state.selected_component = component.clone();
                                match component {
                                    OverviewComponent::Help => {
                                        overview_state.show_help = !overview_state.show_help;
                                    }
                                    _ => {
                                        handle_selection(&component, &mut app_events);
                                    }
                                }
                                break;
                            }
                        }
                    }
                    MouseEventKind::Moved => {
                        let x = mouse_event.column;
                        let y = mouse_event.row;
                        overview_state.update_hover(x, y);
                    }
                    _ => {}
                }
            }
        }
    }
}

fn handle_selection(component: &OverviewComponent, app_events: &mut EventWriter<AppEvent>) {
    match component {
        OverviewComponent::StartAnalysis => {
            app_events.send(AppEvent::SwitchTo(AppState::Analysis));
        }
        OverviewComponent::ViewReports => {
            app_events.send(AppEvent::SwitchTo(AppState::Reports));
        }
        OverviewComponent::Settings => {
            // Settings functionality - for now we'll add this as a placeholder
            // In a real implementation, this might open a settings dialog
        }
        OverviewComponent::Credits => {
            app_events.send(AppEvent::SwitchTo(AppState::Credits));
        }
        OverviewComponent::Help => {
            // Show help dialog - for now we'll add this as a state toggle
            // In a real implementation, this might open a help dialog
        }
        OverviewComponent::Exit => {
            app_events.send(AppEvent::Exit);
        }
    }
}

fn render_overview(
    app_state: Res<State<AppState>>,
    mut ratatui_context: ResMut<RatatuiContext>,
    mut overview_state: ResMut<OverviewWidgetState>,
) -> color_eyre::Result<()> {
    if app_state.get() != &AppState::Overview {
        return Ok(());
    }

    ratatui_context.draw(|frame| {
        let area = frame.area();
        frame.render_stateful_widget_ref(OverviewWidget, area, &mut overview_state);
    })?;

    Ok(())
}

pub struct OverviewWidget;

impl StatefulWidgetRef for OverviewWidget {
    type State = OverviewWidgetState;

    fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        state.registered_components.clear();

        if state.show_help {
            self.render_help_overlay(area, buf, state);
            return;
        }

        // Main layout
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Title
                Constraint::Length(8), // Repository info
                Constraint::Min(10),   // Menu buttons
                Constraint::Length(3), // Status bar
            ])
            .split(area);

        // Render title
        self.render_title(chunks[0], buf);

        // Render repository info
        self.render_repo_info(chunks[1], buf, state);

        // Render menu
        self.render_menu(chunks[2], buf, state);

        // Render status bar
        self.render_status_bar(chunks[3], buf);
    }
}

impl OverviewWidget {
    fn render_title(&self, area: Rect, buf: &mut Buffer) {
        let title = Paragraph::new(format!("🤖 AI Code Buddy v{APP_VERSION}"))
            .style(THEME.title_style())
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(THEME.header_style()),
            );
        title.render_ref(area, buf);
    }

    fn render_repo_info(&self, area: Rect, buf: &mut Buffer, state: &OverviewWidgetState) {
        let info_lines = vec![
            Line::from(vec![
                Span::styled("📂 Repository: ", THEME.info_style()),
                Span::raw(&state.repo_info.path),
            ]),
            Line::from(vec![
                Span::styled("🌿 Source Branch: ", THEME.info_style()),
                Span::raw(&state.repo_info.source_branch),
            ]),
            Line::from(vec![
                Span::styled("🎯 Target Branch: ", THEME.info_style()),
                Span::raw(&state.repo_info.target_branch),
            ]),
            Line::from(vec![
                Span::styled("📊 Files to Analyze: ", THEME.info_style()),
                Span::raw(format!("{}", state.repo_info.files_to_analyze)),
            ]),
        ];

        let repo_info = Paragraph::new(info_lines)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title("Repository Information")
                    .title_style(THEME.header_style()),
            )
            .wrap(ratatui::widgets::Wrap { trim: true });

        repo_info.render_ref(area, buf);
    }

    fn render_menu(&self, area: Rect, buf: &mut Buffer, state: &mut OverviewWidgetState) {
        // Center the menu items
        let menu_layout = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(20),
                Constraint::Percentage(60),
                Constraint::Percentage(20),
            ])
            .split(area);

        let menu_area = menu_layout[1];

        let items_layout = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Start Analysis
                Constraint::Length(1), // Spacer
                Constraint::Length(3), // View Reports
                Constraint::Length(1), // Spacer
                Constraint::Length(3), // Credits
                Constraint::Length(1), // Spacer
                Constraint::Length(3), // Help
                Constraint::Length(1), // Spacer
                Constraint::Length(3), // Exit
            ])
            .split(menu_area);

        self.render_menu_button(
            items_layout[0],
            buf,
            state,
            OverviewComponent::StartAnalysis,
            "🚀 Start Analysis",
        );

        self.render_menu_button(
            items_layout[2],
            buf,
            state,
            OverviewComponent::ViewReports,
            "📊 View Reports",
        );

        self.render_menu_button(
            items_layout[4],
            buf,
            state,
            OverviewComponent::Settings,
            "⚙️  Settings",
        );

        self.render_menu_button(
            items_layout[6],
            buf,
            state,
            OverviewComponent::Credits,
            "🎉 Credits",
        );

        self.render_menu_button(
            items_layout[6],
            buf,
            state,
            OverviewComponent::Help,
            "❓ Help",
        );

        self.render_menu_button(
            items_layout[8],
            buf,
            state,
            OverviewComponent::Exit,
            "🚪 Exit",
        );
    }

    fn render_menu_button(
        &self,
        area: Rect,
        buf: &mut Buffer,
        state: &mut OverviewWidgetState,
        component: OverviewComponent,
        text: &str,
    ) {
        let is_selected = state.selected_component == component;
        let is_hovered = state.hovered_component == Some(component.clone());

        let style = if is_selected {
            THEME.selected_style()
        } else if is_hovered {
            THEME.button_hover_style()
        } else {
            THEME.button_normal_style()
        };

        let border_style = if is_selected {
            THEME.selected_style()
        } else if is_hovered {
            THEME.button_hover_style()
        } else {
            Style::default()
        };

        let button = Paragraph::new(text)
            .style(style)
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(border_style),
            );

        button.render_ref(area, buf);
        state.registered_components.insert(component, area);
    }

    fn render_status_bar(&self, area: Rect, buf: &mut Buffer) {
        let status = Paragraph::new("Use ↑↓ or Tab to navigate, Enter to select, Q to quit")
            .style(THEME.info_style())
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::TOP)
                    .border_style(THEME.info_style()),
            );

        status.render_ref(area, buf);
    }

    fn render_help_overlay(&self, area: Rect, buf: &mut Buffer, state: &mut OverviewWidgetState) {
        // Create a centered help dialog
        let help_area = {
            let vertical = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Percentage(20),
                    Constraint::Percentage(60),
                    Constraint::Percentage(20),
                ])
                .split(area);

            Layout::default()
                .direction(Direction::Horizontal)
                .constraints([
                    Constraint::Percentage(15),
                    Constraint::Percentage(70),
                    Constraint::Percentage(15),
                ])
                .split(vertical[1])[1]
        };

        // Clear the background
        for y in help_area.top()..help_area.bottom() {
            for x in help_area.left()..help_area.right() {
                buf.cell_mut((x, y)).unwrap().set_bg(THEME.background);
            }
        }

        let help_content = vec![
            Line::from("🤖 AI Code Buddy - Help"),
            Line::from(""),
            Line::from("🎯 What it does:"),
            Line::from("  • Analyzes Git repositories for code quality issues"),
            Line::from("  • Detects security vulnerabilities (OWASP Top 10)"),
            Line::from("  • Provides performance and maintainability suggestions"),
            Line::from("  • Compares code changes between Git branches"),
            Line::from(""),
            Line::from("⌨️  Keyboard Controls:"),
            Line::from("  • ↑/↓ or Tab/Shift+Tab: Navigate menu"),
            Line::from("  • Enter: Select menu item"),
            Line::from("  • q: Quit application"),
            Line::from(""),
            Line::from("🖱️  Mouse Controls:"),
            Line::from("  • Click: Select menu item"),
            Line::from("  • Hover: Highlight menu item"),
            Line::from(""),
            Line::from("📋 Menu Options:"),
            Line::from("  • 🚀 Start Analysis: Begin analyzing the repository"),
            Line::from("  • 📊 View Reports: See analysis results and export"),
            Line::from("  • 🎉 Credits: View project contributors and acknowledgments"),
            Line::from("  • ❓ Help: Show this help screen"),
            Line::from("  • 🚪 Exit: Quit the application"),
            Line::from(""),
            Line::from(Span::styled(
                "Press any key or click anywhere to close help",
                Style::default()
                    .fg(THEME.accent)
                    .add_modifier(Modifier::BOLD),
            )),
        ];

        let help_dialog = Paragraph::new(help_content)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(" Help & Controls ")
                    .title_style(THEME.title_style())
                    .border_style(THEME.primary_style()),
            )
            .wrap(ratatui::widgets::Wrap { trim: true });

        help_dialog.render_ref(help_area, buf);

        // Register the entire help area as clickable to close help
        state
            .registered_components
            .insert(OverviewComponent::Help, help_area);
    }
}