gitkyl/components/
diff.rs1use crate::git::{ChangeType, ChangedFile, DiffHunk, DiffLine, DiffLineType, FileStats};
4use maud::{Markup, html};
5
6pub fn file_stats_summary(files: &[ChangedFile]) -> Markup {
8 let total_additions: usize = files.iter().map(|f| f.stats.additions).sum();
9 let total_deletions: usize = files.iter().map(|f| f.stats.deletions).sum();
10
11 html! {
12 div class="diff-stats" {
13 span class="diff-stats-count" {
14 (files.len()) " file" (if files.len() != 1 { "s" } else { "" }) " changed"
15 }
16 @if total_additions > 0 {
17 span class="diff-stats-additions" {
18 "+" (total_additions)
19 }
20 }
21 @if total_deletions > 0 {
22 span class="diff-stats-deletions" {
23 "-" (total_deletions)
24 }
25 }
26 }
27 }
28}
29
30pub fn changed_files_list(files: &[ChangedFile]) -> Markup {
32 html! {
33 div class="changed-files" {
34 @for file in files {
35 div class="changed-file-entry" {
36 a href=(format!("#diff-{}", sanitize_path(&file.path))) class="file-link" {
37 span class="file-path" { (file.path) }
38 }
39 span class="file-stats" {
40 @if file.change_type == ChangeType::Binary {
41 span class="binary-badge" { "Binary" }
42 } @else {
43 @if file.stats.additions > 0 {
44 span class="stat-additions" { "+" (file.stats.additions) }
45 }
46 @if file.stats.deletions > 0 {
47 span class="stat-deletions" { "-" (file.stats.deletions) }
48 }
49 }
50 }
51 }
52 }
53 }
54 }
55}
56
57pub fn diff_view(files: &[ChangedFile]) -> Markup {
59 html! {
60 div class="diff-container" {
61 @for file in files {
62 (file_diff(file))
63 }
64 }
65 }
66}
67
68fn file_diff(file: &ChangedFile) -> Markup {
70 html! {
71 div class="file-diff" id=(format!("diff-{}", sanitize_path(&file.path))) {
72 div class="diff-file-header" {
73 span class="diff-file-path" { (file.path) }
74 (file_stats(&file.stats, &file.change_type))
75 }
76 @if file.change_type == ChangeType::Binary {
77 div class="binary-diff-notice" {
78 "Binary file changed"
79 }
80 } @else {
81 @for hunk in &file.hunks {
82 (diff_hunk(hunk))
83 }
84 }
85 }
86 }
87}
88
89fn file_stats(stats: &FileStats, change_type: &ChangeType) -> Markup {
91 html! {
92 span class="file-stats-badge" {
93 @match change_type {
94 ChangeType::Added => {
95 span class="stat-additions" { "+" (stats.additions) }
96 }
97 ChangeType::Deleted => {
98 span class="stat-deletions" { "-" (stats.deletions) }
99 }
100 ChangeType::Modified => {
101 @if stats.additions > 0 {
102 span class="stat-additions" { "+" (stats.additions) }
103 }
104 @if stats.deletions > 0 {
105 span class="stat-deletions" { "-" (stats.deletions) }
106 }
107 }
108 ChangeType::Binary => {
109 span class="binary-badge" { "Binary" }
110 }
111 }
112 }
113 }
114}
115
116fn diff_hunk(hunk: &DiffHunk) -> Markup {
118 html! {
119 div class="diff-hunk" {
120 div class="hunk-header" {
121 (hunk.header)
122 }
123 table class="hunk-content" {
124 tbody {
125 @for line in &hunk.lines {
126 (diff_line(line))
127 }
128 }
129 }
130 }
131 }
132}
133
134fn diff_line(line: &DiffLine) -> Markup {
136 let (line_class, prefix) = match line.line_type {
137 DiffLineType::Addition => ("diff-line-add", "+"),
138 DiffLineType::Deletion => ("diff-line-del", "-"),
139 DiffLineType::Context => ("diff-line-ctx", " "),
140 };
141
142 html! {
143 tr class=(line_class) {
144 @if let Some(old_num) = line.old_line_num {
145 td class="line-num old-line-num" { (old_num) }
146 } @else {
147 td class="line-num old-line-num empty" {}
148 }
149 @if let Some(new_num) = line.new_line_num {
150 td class="line-num new-line-num" { (new_num) }
151 } @else {
152 td class="line-num new-line-num empty" {}
153 }
154 td class="line-prefix" { (prefix) }
155 td class="line-content" { (line.content) }
156 }
157 }
158}
159
160fn sanitize_path(path: &str) -> String {
162 path.replace(['/', '.', ' '], "-")
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 #[test]
170 fn test_sanitize_path() {
171 assert_eq!(sanitize_path("src/main.rs"), "src-main-rs");
172 assert_eq!(sanitize_path("path/to/file.txt"), "path-to-file-txt");
173 assert_eq!(sanitize_path("test file.md"), "test-file-md");
174 }
175
176 #[test]
177 fn test_file_stats_summary_single_file() {
178 let files = vec![ChangedFile {
179 path: "test.rs".to_string(),
180 change_type: ChangeType::Modified,
181 stats: FileStats {
182 additions: 5,
183 deletions: 3,
184 },
185 hunks: vec![],
186 }];
187
188 let html = file_stats_summary(&files).into_string();
189 assert!(html.contains("1 file changed"));
190 assert!(html.contains("+5"));
191 assert!(html.contains("-3"));
192 }
193
194 #[test]
195 fn test_file_stats_summary_multiple_files() {
196 let files = vec![
197 ChangedFile {
198 path: "test1.rs".to_string(),
199 change_type: ChangeType::Modified,
200 stats: FileStats {
201 additions: 5,
202 deletions: 3,
203 },
204 hunks: vec![],
205 },
206 ChangedFile {
207 path: "test2.rs".to_string(),
208 change_type: ChangeType::Added,
209 stats: FileStats {
210 additions: 10,
211 deletions: 0,
212 },
213 hunks: vec![],
214 },
215 ];
216
217 let html = file_stats_summary(&files).into_string();
218 assert!(html.contains("2 files changed"));
219 assert!(html.contains("+15"));
220 assert!(html.contains("-3"));
221 }
222
223 #[test]
224 fn test_binary_file_display() {
225 let file = ChangedFile {
226 path: "image.png".to_string(),
227 change_type: ChangeType::Binary,
228 stats: FileStats {
229 additions: 0,
230 deletions: 0,
231 },
232 hunks: vec![],
233 };
234
235 let html = file_diff(&file).into_string();
236 assert!(html.contains("image.png"));
237 assert!(html.contains("Binary file changed"));
238 }
239}