git_plumber/tui/widget/loose_obj_details/formatters/
commit.rs

1use crate::git::loose_object::CommitObject;
2use ratatui::style::{Modifier, Style};
3use ratatui::text::Line;
4
5pub struct CommitFormatter<'a> {
6    commit: &'a CommitObject,
7}
8
9impl<'a> CommitFormatter<'a> {
10    pub fn new(commit: &'a CommitObject) -> Self {
11        Self { commit }
12    }
13
14    pub fn format_commit(&self, lines: &mut Vec<Line<'static>>) {
15        lines.push(Line::styled(
16            "COMMIT DETAILS",
17            Style::default().add_modifier(Modifier::BOLD),
18        ));
19        lines.push(Line::from("─".repeat(40)));
20        lines.push(Line::from(""));
21
22        // Tree
23        lines.push(Line::from(format!("Tree: {}", self.commit.tree)));
24        lines.push(Line::from(""));
25
26        // Parents
27        if self.commit.parents.is_empty() {
28            lines.push(Line::from("Parents: none (root commit)"));
29        } else {
30            lines.push(Line::from("Parents:"));
31            for parent in &self.commit.parents {
32                lines.push(Line::from(format!("  {parent}")));
33            }
34        }
35        lines.push(Line::from(""));
36
37        // Author
38        lines.push(Line::styled(
39            "Author Information:",
40            Style::default().add_modifier(Modifier::BOLD),
41        ));
42        lines.push(Line::from(format!("  Name: {}", self.commit.author)));
43        lines.push(Line::from(format!(
44            "  Date: {}",
45            self.format_timestamp(&self.commit.author_date)
46        )));
47        lines.push(Line::from(""));
48
49        // Committer
50        lines.push(Line::styled(
51            "Committer Information:",
52            Style::default().add_modifier(Modifier::BOLD),
53        ));
54        lines.push(Line::from(format!("  Name: {}", self.commit.committer)));
55        lines.push(Line::from(format!(
56            "  Date: {}",
57            self.format_timestamp(&self.commit.committer_date)
58        )));
59        lines.push(Line::from(""));
60
61        // Message
62        lines.push(Line::styled(
63            "Commit Message:",
64            Style::default().add_modifier(Modifier::BOLD),
65        ));
66        lines.push(Line::from("─".repeat(20)));
67
68        // Split message into lines and add them
69        for line in self.commit.message.lines() {
70            lines.push(Line::from(line.to_string()));
71        }
72
73        if self.commit.message.is_empty() {
74            lines.push(Line::from("(no message)"));
75        }
76    }
77
78    fn format_timestamp(&self, timestamp: &str) -> String {
79        // For now, just return the raw timestamp
80        // In the future, we could parse Unix timestamps manually
81        timestamp.to_string()
82    }
83}