use ratatui::{
Frame,
layout::Rect,
style::{Color, Modifier, Style, Stylize},
text::{Line, Span},
widgets::Paragraph,
};
use crate::model::Notification;
use crate::ui::components;
use super::BlameView;
mod layout {
pub const CHANGE_ID_WIDTH: usize = 8;
pub const AUTHOR_WIDTH: usize = 10;
pub const TIMESTAMP_WIDTH: usize = 5;
pub const LINE_NUMBER_WIDTH: usize = 6;
pub const AI_BADGE_WIDTH: usize = 5;
}
mod colors {
use super::Color;
use crate::ui::theme;
pub const CHANGE_ID: Color = Color::Cyan;
pub const AUTHOR: Color = Color::White;
pub const TIMESTAMP: Color = Color::Gray;
pub const LINE_NUMBER: Color = Color::Gray;
pub const CONTINUATION: Color = Color::DarkGray;
pub const SELECTED_BG: Color = theme::selection::BG;
pub const SELECTED_FG: Color = theme::selection::FG;
}
impl BlameView {
pub fn render(&self, frame: &mut Frame, area: Rect, notification: Option<&Notification>) {
let title = Line::from(format!(" Blame View: {} ", self.file_path()))
.bold()
.cyan()
.centered();
let title_width = title.width();
let available_for_notif = area.width.saturating_sub(title_width as u16 + 4) as usize;
let notif_line = notification
.filter(|n| !n.is_expired())
.map(|n| components::build_notification_title(n, Some(available_for_notif)))
.filter(|line| !line.spans.is_empty());
let block = components::bordered_block_with_notification(title, notif_line);
if self.is_empty() {
let paragraph = components::empty_state("No content to annotate", None).block(block);
frame.render_widget(paragraph, area);
return;
}
let inner_height = area.height.saturating_sub(2) as usize;
if inner_height == 0 {
return;
}
let scroll_offset = self.calculate_scroll_offset(inner_height);
let mut lines: Vec<Line> = Vec::new();
for (idx, annotation) in self.content.lines.iter().enumerate().skip(scroll_offset) {
if lines.len() >= inner_height {
break;
}
let is_selected = idx == self.selected_index;
let line = self.build_annotation_line(annotation, is_selected);
lines.push(line);
}
let paragraph = Paragraph::new(lines).block(block);
frame.render_widget(paragraph, area);
}
fn build_annotation_line(
&self,
annotation: &crate::model::AnnotationLine,
is_selected: bool,
) -> Line<'static> {
let mut spans = Vec::new();
let has_ai_overlay = !self.ai_badges.is_empty();
if annotation.first_in_hunk {
spans.push(Span::styled(
format!(
"{:<width$}",
annotation.change_id,
width = layout::CHANGE_ID_WIDTH
),
Style::default().fg(colors::CHANGE_ID),
));
spans.push(Span::raw(" "));
if has_ai_overlay {
let commit = annotation.commit_id.as_str();
if self.ai_badges.confirmed.contains(commit) {
spans.push(Span::styled(
format!("{:<width$} ", "[AI]", width = layout::AI_BADGE_WIDTH),
Style::default()
.fg(crate::ui::theme::log_view::AI_BADGE)
.add_modifier(Modifier::BOLD),
));
} else if self.ai_badges.heuristic.contains(commit) {
spans.push(Span::styled(
format!("{:<width$} ", "[AI?]", width = layout::AI_BADGE_WIDTH),
Style::default().fg(crate::ui::theme::log_view::AI_BADGE),
));
} else {
spans.push(Span::raw(" ".repeat(layout::AI_BADGE_WIDTH + 1)));
}
}
spans.push(Span::styled(
crate::ui::text::fit_display_width(&annotation.author, layout::AUTHOR_WIDTH),
Style::default().fg(colors::AUTHOR),
));
spans.push(Span::raw(" "));
let timestamp = annotation.short_timestamp();
spans.push(Span::styled(
format!("{:<width$}", timestamp, width = layout::TIMESTAMP_WIDTH),
Style::default().fg(colors::TIMESTAMP),
));
spans.push(Span::raw(" "));
} else {
let mut continuation_width =
layout::CHANGE_ID_WIDTH + 1 + layout::AUTHOR_WIDTH + 1 + layout::TIMESTAMP_WIDTH;
if has_ai_overlay {
continuation_width += layout::AI_BADGE_WIDTH + 1;
}
spans.push(Span::styled(
format!("{:>width$} ", "↑", width = continuation_width),
Style::default().fg(colors::CONTINUATION),
));
}
spans.push(Span::styled(
format!(
"{:>width$}: ",
annotation.line_number,
width = layout::LINE_NUMBER_WIDTH
),
Style::default().fg(colors::LINE_NUMBER),
));
let content = annotation.content.trim_end_matches('\n');
spans.push(Span::raw(content.to_string()));
let mut line = Line::from(spans);
if is_selected {
line = line.style(
Style::default()
.fg(colors::SELECTED_FG)
.bg(colors::SELECTED_BG)
.add_modifier(Modifier::BOLD),
);
}
line
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{AnnotationContent, AnnotationLine, ChangeId, CommitId};
use crate::trace::AiBadgeSets;
fn line(change: &str, commit: &str, n: usize, first_in_hunk: bool) -> AnnotationLine {
AnnotationLine {
change_id: ChangeId::new(change.to_string()),
commit_id: CommitId::new(commit.to_string()),
author: "nakamura".to_string(),
timestamp: "2026-06-05 14:20".to_string(),
line_number: n,
content: format!("line {n}"),
first_in_hunk,
}
}
fn text(line: &Line<'static>) -> String {
line.spans.iter().map(|s| s.content.as_ref()).collect()
}
fn view_with(lines: Vec<AnnotationLine>, badges: AiBadgeSets) -> BlameView {
let mut content = AnnotationContent::new("greet.py".to_string());
content.lines = lines;
let mut bv = BlameView::new();
bv.set_content(content, None);
bv.set_ai_badges(badges);
bv
}
#[test]
fn confirmed_line_renders_ai_badge() {
let mut badges = AiBadgeSets::default();
badges.confirmed.insert("c0ffee01".to_string());
let bv = view_with(vec![line("xqnktzml", "c0ffee01", 1, true)], badges);
let t = text(&bv.build_annotation_line(&bv.content.lines[0], false));
assert!(t.contains("[AI]"), "got: {t}");
assert!(!t.contains("[AI?]"), "got: {t}");
}
#[test]
fn heuristic_line_renders_ai_question_badge() {
let mut badges = AiBadgeSets::default();
badges.heuristic.insert("c0ffee01".to_string());
let bv = view_with(vec![line("rlxnnrwv", "c0ffee01", 1, true)], badges);
let t = text(&bv.build_annotation_line(&bv.content.lines[0], false));
assert!(t.contains("[AI?]"), "got: {t}");
}
#[test]
fn unmatched_line_in_overlay_view_shows_blank_column() {
let mut badges = AiBadgeSets::default();
badges.confirmed.insert("c0ffee01".to_string());
let bv = view_with(
vec![
line("xqnktzml", "c0ffee01", 1, true), line("lrptplro", "b06e4c8c", 2, true), ],
badges,
);
let matched = text(&bv.build_annotation_line(&bv.content.lines[0], false));
let unmatched = text(&bv.build_annotation_line(&bv.content.lines[1], false));
assert!(matched.contains("[AI]"));
assert!(!unmatched.contains("[AI"), "got: {unmatched}");
let col = |s: &str| s.split("line ").next().unwrap().chars().count();
assert_eq!(col(&matched), col(&unmatched));
}
#[test]
fn no_overlay_output_is_unchanged_and_has_no_ai_column() {
let bv = view_with(
vec![line("xqnktzml", "c0ffee01", 1, true)],
AiBadgeSets::default(),
);
let t = text(&bv.build_annotation_line(&bv.content.lines[0], false));
assert!(!t.contains("[AI"), "no AI column when no trace: {t}");
assert!(t.contains("xqnktzml nakamura"), "got: {t}");
}
#[test]
fn continuation_line_has_no_badge_but_stays_aligned() {
let mut badges = AiBadgeSets::default();
badges.confirmed.insert("c0ffee01".to_string());
let bv = view_with(
vec![
line("xqnktzml", "c0ffee01", 1, true), line("xqnktzml", "c0ffee01", 2, false), ],
badges,
);
let head = text(&bv.build_annotation_line(&bv.content.lines[0], false));
let cont = text(&bv.build_annotation_line(&bv.content.lines[1], false));
assert!(cont.contains('↑'));
assert!(!cont.contains("[AI"), "continuation must not badge: {cont}");
let col = |s: &str| s.split("line ").next().unwrap().chars().count();
assert_eq!(col(&head), col(&cont));
}
}