git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
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
//! Explore mode rendering for Iris Studio

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
    Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Wrap,
};

use crate::studio::components::{render_code_view, render_file_tree};
use crate::studio::state::{PanelId, StudioState};
use crate::studio::theme;

/// Render a panel in Explore mode
pub fn render_explore_panel(
    state: &mut StudioState,
    frame: &mut Frame,
    area: Rect,
    panel_id: PanelId,
) {
    let is_focused = panel_id == state.focused_panel;

    match panel_id {
        PanelId::Left => {
            // File tree
            render_file_tree(
                frame,
                area,
                &mut state.modes.explore.file_tree,
                "Files",
                is_focused,
            );
        }
        PanelId::Center => {
            // Code view - display actual file content
            let title = state.modes.explore.code_view.current_file().map_or_else(
                || "Code".to_string(),
                |p| {
                    p.file_name()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .to_string()
                },
            );

            render_code_view(
                frame,
                area,
                &state.modes.explore.code_view,
                &title,
                is_focused,
            );
        }
        PanelId::Right => {
            // Right panel: semantic blame if active, otherwise file log
            if state.modes.explore.blame_loading {
                render_blame_loading(frame, area, is_focused);
            } else if let Some(ref blame) = state.modes.explore.semantic_blame {
                render_semantic_blame_panel(frame, area, blame, is_focused);
            } else {
                render_file_log_panel(frame, area, state, is_focused);
            }
        }
    }
}

/// Render the file log panel (git history for selected file)
fn render_file_log_panel(frame: &mut Frame, area: Rect, state: &mut StudioState, is_focused: bool) {
    let show_global = state.modes.explore.show_global_log;

    let title = if show_global {
        " Commit Log (L) ".to_string()
    } else {
        let file_name = state
            .modes
            .explore
            .current_file
            .as_ref()
            .and_then(|p| p.file_name())
            .map(|f| f.to_string_lossy().to_string())
            .unwrap_or_default();

        if file_name.is_empty() {
            " History (L) ".to_string()
        } else {
            format!(" {} (L) ", file_name)
        }
    };

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(if is_focused {
            theme::focused_border()
        } else {
            theme::unfocused_border()
        });
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if inner.height == 0 || inner.width == 0 {
        return;
    }

    // Use either global log or file log based on toggle
    let file_log = if show_global {
        &state.modes.explore.global_log
    } else {
        &state.modes.explore.file_log
    };
    let selected = state.modes.explore.file_log_selected;
    let scroll = state.modes.explore.file_log_scroll;
    let visible_height = inner.height as usize;

    let is_loading = if show_global {
        state.modes.explore.global_log_loading
    } else {
        state.modes.explore.file_log_loading
    };

    if is_loading {
        let frames = ['', '', '', '', '', '', '', '', '', ''];
        let idx = (std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis()
            / 100) as usize
            % frames.len();
        let spinner = frames[idx];
        let loading = Paragraph::new(format!("{} Loading history...", spinner))
            .style(Style::default().fg(theme::accent_secondary()));
        frame.render_widget(loading, inner);
        return;
    }

    if file_log.is_empty() {
        let hint = if show_global {
            "Press L to load commit log"
        } else if state.modes.explore.current_file.is_none() {
            "Select a file to view history"
        } else {
            "No history for this file"
        };
        let empty = Paragraph::new(hint).style(Style::default().fg(theme::text_dim_color()));
        frame.render_widget(empty, inner);
        return;
    }

    // Render file log entries (3 lines per entry: message, meta, separator)
    let entry_height = 3;
    let visible_entries = visible_height / entry_height;

    let mut lines: Vec<Line> = Vec::new();
    let panel_width = inner.width as usize;

    for (i, entry) in file_log
        .iter()
        .enumerate()
        .skip(scroll)
        .take(visible_entries)
    {
        let is_selected = i == selected;
        let bg = if is_selected {
            Some(theme::bg_highlight_color())
        } else {
            None
        };

        // Line 1: marker + hash + message (truncated)
        let marker = if is_selected { "" } else { "  " };
        let marker_style = if is_selected {
            Style::default()
                .fg(theme::accent_primary())
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default()
        };

        let hash_style = theme::commit_hash().bg(bg.unwrap_or(Color::Reset));
        let msg_style = Style::default()
            .fg(theme::text_primary_color())
            .bg(bg.unwrap_or(Color::Reset));

        // Calculate message width: panel - marker(2) - hash(7) - space(1)
        let msg_width = panel_width.saturating_sub(10);
        let truncated_msg = truncate_str(&entry.message, msg_width);

        lines.push(Line::from(vec![
            Span::styled(marker, marker_style.bg(bg.unwrap_or(Color::Reset))),
            Span::styled(&entry.short_hash, hash_style),
            Span::raw(" "),
            Span::styled(truncated_msg, msg_style),
        ]));

        // Line 2: author · time · stats (indented)
        let meta_style = Style::default()
            .fg(theme::text_dim_color())
            .bg(bg.unwrap_or(Color::Reset));
        let author_style = Style::default()
            .fg(theme::text_muted_color())
            .bg(bg.unwrap_or(Color::Reset));
        let time_style = Style::default()
            .fg(theme::text_dim_color())
            .bg(bg.unwrap_or(Color::Reset));

        let mut meta_spans = vec![
            Span::styled("  ", meta_style), // indent to align with message
            Span::styled(&entry.author, author_style),
            Span::styled(" · ", meta_style),
            Span::styled(&entry.relative_time, time_style),
        ];

        // Add +/- stats if available
        if let (Some(adds), Some(dels)) = (entry.additions, entry.deletions)
            && (adds > 0 || dels > 0)
        {
            meta_spans.push(Span::styled(" ", meta_style));
            if adds > 0 {
                meta_spans.push(Span::styled(
                    format!("+{adds}"),
                    Style::default()
                        .fg(theme::success_color())
                        .bg(bg.unwrap_or(Color::Reset)),
                ));
            }
            if dels > 0 {
                meta_spans.push(Span::styled(
                    format!("-{dels}"),
                    Style::default()
                        .fg(theme::error_color())
                        .bg(bg.unwrap_or(Color::Reset)),
                ));
            }
        }

        lines.push(Line::from(meta_spans));

        // Line 3: subtle separator
        let sep_char = if i + 1 < file_log.len() { "" } else { " " };
        let sep = sep_char.repeat(panel_width.saturating_sub(2));
        lines.push(Line::from(Span::styled(
            format!(" {sep}"),
            Style::default().fg(theme::bg_highlight_color()),
        )));
    }

    let paragraph = Paragraph::new(lines);
    frame.render_widget(paragraph, inner);

    // Scrollbar
    if file_log.len() > visible_entries {
        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(None)
            .end_symbol(None);
        let mut scrollbar_state = ScrollbarState::new(file_log.len()).position(scroll);
        frame.render_stateful_widget(
            scrollbar,
            area.inner(ratatui::layout::Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut scrollbar_state,
        );
    }
}

/// Render blame loading state
fn render_blame_loading(frame: &mut Frame, area: Rect, is_focused: bool) {
    let block = Block::default()
        .title(" Analyzing... ")
        .borders(Borders::ALL)
        .border_style(if is_focused {
            theme::focused_border()
        } else {
            theme::unfocused_border()
        });
    let inner = block.inner(area);
    frame.render_widget(block, area);

    let frames = ['', '', '', '', '', '', '', '', '', ''];
    let idx = (std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis()
        / 100) as usize
        % frames.len();
    let spinner = frames[idx];

    let loading_text = Paragraph::new(format!("{} Iris is analyzing the code history...", spinner))
        .style(Style::default().fg(theme::accent_secondary()));
    frame.render_widget(loading_text, inner);
}

/// Render semantic blame panel with full content
fn render_semantic_blame_panel(
    frame: &mut Frame,
    area: Rect,
    blame: &crate::studio::events::SemanticBlameResult,
    is_focused: bool,
) {
    let block = Block::default()
        .title(" Why This Code? ")
        .borders(Borders::ALL)
        .border_style(if is_focused {
            theme::focused_border()
        } else {
            theme::unfocused_border()
        });
    let inner = block.inner(area);
    frame.render_widget(block, area);

    render_semantic_blame(frame, inner, blame);
}

/// Render the companion status bar (compact, at bottom of explore mode)
pub fn render_companion_status_bar(frame: &mut Frame, area: Rect, state: &StudioState) {
    let display = &state.companion_display;

    // Format: ⎇ branch ↑1↓2 | ●3 staged ○5 unstaged | ◷ 2h15m | [w] why [/] chat
    let mut spans = vec![
        Span::styled("", Style::default().fg(theme::text_dim_color())),
        Span::styled(
            &display.branch,
            Style::default()
                .fg(theme::accent_secondary())
                .add_modifier(Modifier::BOLD),
        ),
    ];

    // Ahead/behind
    if display.ahead > 0 || display.behind > 0 {
        spans.push(Span::raw(" "));
        if display.ahead > 0 {
            spans.push(Span::styled(
                format!("{}", display.ahead),
                Style::default().fg(theme::success_color()),
            ));
        }
        if display.behind > 0 {
            if display.ahead > 0 {
                spans.push(Span::raw(" "));
            }
            spans.push(Span::styled(
                format!("{}", display.behind),
                Style::default().fg(theme::warning_color()),
            ));
        }
    }

    spans.push(Span::styled(
        "",
        Style::default().fg(theme::text_dim_color()),
    ));

    // Staged/unstaged counts
    if display.staged_count > 0 {
        spans.push(Span::styled(
            format!("{}", display.staged_count),
            Style::default().fg(theme::success_color()),
        ));
        spans.push(Span::raw(" "));
    }
    if display.unstaged_count > 0 {
        spans.push(Span::styled(
            format!("{}", display.unstaged_count),
            Style::default().fg(theme::warning_color()),
        ));
    }
    if display.staged_count == 0 && display.unstaged_count == 0 {
        spans.push(Span::styled(
            "clean",
            Style::default().fg(theme::text_dim_color()),
        ));
    }

    spans.push(Span::styled(
        "",
        Style::default().fg(theme::text_dim_color()),
    ));

    // Session duration
    spans.push(Span::styled(
        format!("{}", display.duration),
        Style::default().fg(theme::text_muted_color()),
    ));

    // Welcome message (if any)
    if let Some(ref welcome) = display.welcome_message {
        spans.push(Span::styled(
            "",
            Style::default().fg(theme::text_dim_color()),
        ));
        spans.push(Span::styled(
            welcome.clone(),
            Style::default()
                .fg(theme::accent_primary())
                .add_modifier(Modifier::ITALIC),
        ));
    }

    // Keyboard hints (right-aligned would require calculating width)
    spans.push(Span::styled(
        "",
        Style::default().fg(theme::text_dim_color()),
    ));
    spans.push(Span::styled(
        "[w]",
        Style::default().fg(theme::accent_secondary()),
    ));
    spans.push(Span::styled(
        "hy ",
        Style::default().fg(theme::text_muted_color()),
    ));
    spans.push(Span::styled(
        "[/]",
        Style::default().fg(theme::accent_secondary()),
    ));
    spans.push(Span::styled(
        "chat",
        Style::default().fg(theme::text_muted_color()),
    ));

    let line = Line::from(spans);
    let paragraph = Paragraph::new(line);
    frame.render_widget(paragraph, area);
}

/// Truncate a string to max length with ellipsis
fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}", &s[..max_len.saturating_sub(1)])
    }
}

/// Render semantic blame result in the context panel
fn render_semantic_blame(
    frame: &mut Frame,
    area: Rect,
    blame: &crate::studio::events::SemanticBlameResult,
) {
    use ratatui::layout::{Constraint, Layout};

    // Split area: header (commit info) and body (explanation)
    let chunks = Layout::vertical([
        Constraint::Length(5), // Header with commit info
        Constraint::Min(1),    // Explanation
    ])
    .split(area);

    // Header: commit info
    let file_name = blame.file.file_name().map_or_else(
        || "Unknown file".to_string(),
        |f| f.to_string_lossy().to_string(),
    );

    let header_lines = vec![
        Line::from(vec![
            Span::styled("File: ", Style::default().fg(theme::text_dim_color())),
            Span::styled(
                file_name,
                Style::default()
                    .fg(theme::accent_secondary())
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!(" (L{}-{})", blame.start_line, blame.end_line),
                Style::default().fg(theme::text_dim_color()),
            ),
        ]),
        Line::from(vec![
            Span::styled("Commit: ", Style::default().fg(theme::text_dim_color())),
            Span::styled(
                &blame.commit_hash[..8.min(blame.commit_hash.len())],
                theme::commit_hash(),
            ),
            Span::styled(" by ", Style::default().fg(theme::text_dim_color())),
            Span::styled(&blame.author, theme::author()),
        ]),
        Line::from(vec![
            Span::styled("Date: ", Style::default().fg(theme::text_dim_color())),
            Span::styled(&blame.commit_date, theme::timestamp()),
        ]),
        Line::from(vec![
            Span::styled("Message: ", Style::default().fg(theme::text_dim_color())),
            Span::styled(
                &blame.commit_message,
                Style::default().fg(theme::text_secondary_color()),
            ),
        ]),
    ];

    let header = Paragraph::new(header_lines);
    frame.render_widget(header, chunks[0]);

    // Body: explanation with markdown rendering
    let lines = render_markdown_lines(&blame.explanation);
    let explanation = Paragraph::new(lines).wrap(Wrap { trim: true });
    frame.render_widget(explanation, chunks[1]);
}

/// Render markdown text into styled Lines
fn render_markdown_lines(text: &str) -> Vec<Line<'static>> {
    let mut lines: Vec<Line<'static>> = Vec::new();

    for paragraph in text.split("\n\n") {
        if paragraph.trim().is_empty() {
            continue;
        }

        for line in paragraph.lines() {
            let trimmed = line.trim();

            if trimmed.is_empty() {
                lines.push(Line::from(""));
                continue;
            }

            // Handle headers
            if let Some(header) = trimmed.strip_prefix("### ") {
                lines.push(Line::from(Span::styled(
                    header.to_string(),
                    Style::default()
                        .fg(theme::accent_secondary())
                        .add_modifier(Modifier::BOLD),
                )));
                continue;
            }
            if let Some(header) = trimmed.strip_prefix("## ") {
                lines.push(Line::from(Span::styled(
                    header.to_string(),
                    Style::default()
                        .fg(theme::accent_primary())
                        .add_modifier(Modifier::BOLD),
                )));
                continue;
            }
            if let Some(header) = trimmed.strip_prefix("# ") {
                lines.push(Line::from(Span::styled(
                    header.to_string(),
                    Style::default()
                        .fg(theme::accent_primary())
                        .add_modifier(Modifier::BOLD),
                )));
                continue;
            }

            // Handle bullet points
            if let Some(bullet_text) = trimmed.strip_prefix("- ") {
                let mut spans = vec![Span::styled(
                    "",
                    Style::default().fg(theme::accent_tertiary()),
                )];
                spans.extend(parse_inline_markdown(bullet_text));
                lines.push(Line::from(spans));
                continue;
            }
            if let Some(bullet_text) = trimmed.strip_prefix("* ") {
                let mut spans = vec![Span::styled(
                    "",
                    Style::default().fg(theme::accent_tertiary()),
                )];
                spans.extend(parse_inline_markdown(bullet_text));
                lines.push(Line::from(spans));
                continue;
            }

            // Handle numbered lists
            if trimmed.chars().next().is_some_and(|c| c.is_ascii_digit())
                && let Some(dot_pos) = trimmed.find(". ")
            {
                let num = &trimmed[..dot_pos];
                if num.chars().all(|c| c.is_ascii_digit()) {
                    let rest = &trimmed[dot_pos + 2..];
                    let mut spans = vec![Span::styled(
                        format!("  {}. ", num),
                        Style::default().fg(theme::accent_tertiary()),
                    )];
                    spans.extend(parse_inline_markdown(rest));
                    lines.push(Line::from(spans));
                    continue;
                }
            }

            // Regular paragraph text with inline formatting
            let spans = parse_inline_markdown(trimmed);
            lines.push(Line::from(spans));
        }

        // Add spacing between paragraphs
        lines.push(Line::from(""));
    }

    // Remove trailing empty line
    if lines.last().is_some_and(|l| l.spans.is_empty()) {
        lines.pop();
    }

    lines
}

/// Parse inline markdown (bold, italic, code) into spans
fn parse_inline_markdown(text: &str) -> Vec<Span<'static>> {
    let mut spans = Vec::new();
    let mut current = String::new();
    let mut chars = text.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '*' if chars.peek() == Some(&'*') => {
                // Bold: **text**
                chars.next(); // consume second *
                if !current.is_empty() {
                    spans.push(Span::styled(
                        std::mem::take(&mut current),
                        Style::default().fg(theme::text_primary_color()),
                    ));
                }
                // Collect bold text
                let mut bold_text = String::new();
                while let Some(bc) = chars.next() {
                    if bc == '*' && chars.peek() == Some(&'*') {
                        chars.next(); // consume closing **
                        break;
                    }
                    bold_text.push(bc);
                }
                if !bold_text.is_empty() {
                    spans.push(Span::styled(
                        bold_text,
                        Style::default()
                            .fg(theme::warning_color())
                            .add_modifier(Modifier::BOLD),
                    ));
                }
            }
            '`' => {
                // Inline code: `code`
                if !current.is_empty() {
                    spans.push(Span::styled(
                        std::mem::take(&mut current),
                        Style::default().fg(theme::text_primary_color()),
                    ));
                }
                let mut code_text = String::new();
                for cc in chars.by_ref() {
                    if cc == '`' {
                        break;
                    }
                    code_text.push(cc);
                }
                if !code_text.is_empty() {
                    spans.push(Span::styled(
                        code_text,
                        Style::default().fg(theme::accent_secondary()),
                    ));
                }
            }
            _ => current.push(c),
        }
    }

    if !current.is_empty() {
        spans.push(Span::styled(
            current,
            Style::default().fg(theme::text_primary_color()),
        ));
    }

    if spans.is_empty() {
        spans.push(Span::styled(
            text.to_string(),
            Style::default().fg(theme::text_primary_color()),
        ));
    }

    spans
}