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
use ansi_term::Colour::{Green, Red};
use ansi_term::Style;
use difference::{Changeset, Difference};
#[macro_export]
macro_rules! paint {
($f:ident, $with_color:ident, $colour:expr, $fmt:expr, $($args:tt)*) => (
if $with_color {
write!($f, "{}", $colour.paint(format!($fmt, $($args)*)))?;
} else {
write!($f, "{}", format!($fmt, $($args)*))?;
}
)
}
const SIGN_RIGHT: char = '+';
const SIGN_LEFT: char = '-';
fn lines_of(d: &Difference) -> impl Iterator<Item = &str> {
let buf = match d {
Difference::Same(b) | Difference::Rem(b) | Difference::Add(b) => b,
};
buf.split('\n')
}
fn print_context<'a>(
lines: Option<(
Option<String>,
impl Iterator<Item = &'a str>,
Option<String>,
)>,
f: &mut impl std::io::Write,
use_color: bool,
) -> std::io::Result<()> {
match lines {
None => {}
Some((before, lines, after)) => {
if let Some(before) = before {
paint!(f, use_color, Style::new().dimmed(), "{}\n", before,);
}
for line in lines {
writeln!(f, "{}", line)?;
}
if let Some(after) = after {
paint!(f, use_color, Style::new().dimmed(), "{}\n", after,);
}
}
};
Ok(())
}
pub fn format_changeset(
mut t: impl std::io::Write,
use_color: bool,
changeset: &Changeset,
) -> std::io::Result<()> {
let diffs = &changeset.diffs;
if use_color {
writeln!(
t,
"{} {} / {} :",
Style::new().bold().paint("Diff"),
Red.paint(format!("{} removed", SIGN_LEFT)),
Green.paint(format!("added {}", SIGN_RIGHT))
)?;
} else {
writeln!(
t,
"Diff {} / {} :",
format!("{} removed", SIGN_LEFT),
format!("added {}", SIGN_RIGHT)
)?;
}
let is_different = |d: &_| {
if let Difference::Same(_) = d {
false
} else {
true
}
};
let first_changed_hunk = diffs.iter().position(is_different);
let last_changed_hunk = diffs.iter().rposition(is_different);
let context = 2;
{
let hunk_before_first_change = first_changed_hunk.and_then(|l| l.checked_sub(1));
print_context(
hunk_before_first_change.map(|l| {
let lines_outside_of_context =
lines_of(&diffs[l]).by_ref().count().saturating_sub(context);
(
if lines_outside_of_context > 0 {
Some(format!("[…skipped {} lines…]", lines_outside_of_context))
} else {
None
},
lines_of(&diffs[l]).skip(lines_outside_of_context),
None,
)
}),
&mut t,
use_color,
)?;
}
if let (Some(first_changed_hunk), Some(last_changed_hunk)) =
(first_changed_hunk, last_changed_hunk)
{
for diff in diffs
.iter()
.take(last_changed_hunk + 1)
.skip(first_changed_hunk)
{
match diff {
Difference::Same(x) => {
writeln!(t, " {}", x)?;
}
Difference::Add(x) => {
paint!(t, use_color, Green, "{}{}\n", SIGN_RIGHT, x);
}
Difference::Rem(x) => {
paint!(t, use_color, Red, "{}{}\n", SIGN_LEFT, x);
}
}
}
}
{
let hunk_after_last_change =
last_changed_hunk.map(|l| (l + 1).min(diffs.len().saturating_sub(1)));
print_context(
hunk_after_last_change.map(|l| {
let skipped_lines_note = lines_of(&diffs[l])
.count()
.checked_sub(context)
.map(|skipped| format!("[…skipped {} lines…]", skipped));
(None, lines_of(&diffs[l]).take(context), skipped_lines_note)
}),
&mut t,
use_color,
)?;
}
Ok(())
}