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
|
// A module for constructing and writing the hunk header.
//
// The structure of the hunk header output by delta is
// ```
// (file):(line-number): (code-fragment)
// ```
//
// The code fragment and line number derive from a line of git/diff output that looks like
// ```
// @@ -119,12 +119,7 @@ fn write_to_output_buffer(
// ```
//
// Whether or not file and line-number are included is controlled by the presence of the special
// style attributes 'file' and 'line-number' in the hunk-header-style string. For example, delta
// might output the above hunk header as
// ```
// ───────────────────────────────────────────────────┐
// src/hunk_header.rs:119: fn write_to_output_buffer( │
// ───────────────────────────────────────────────────┘
// ```
use std::fmt::Write as FmtWrite;
use lazy_static::lazy_static;
use regex::Regex;
use super::draw;
use crate::config::Config;
use crate::delta::{self, State, StateMachine};
use crate::features;
use crate::paint::Painter;
use crate::style::DecorationStyle;
impl<'a> StateMachine<'a> {
#[inline]
fn test_hunk_header_line(&self) -> bool {
self.line.starts_with("@@")
}
pub fn handle_hunk_header_line(&mut self) -> std::io::Result<bool> {
if !self.test_hunk_header_line() {
return Ok(false);
}
self.state = State::HunkHeader(self.line.clone(), self.raw_line.clone());
Ok(true)
}
/// Emit the hunk header, with any requested decoration.
pub fn emit_hunk_header_line(&mut self, line: &str, raw_line: &str) -> std::io::Result<bool> {
self.painter.paint_buffered_minus_and_plus_lines();
self.painter.set_highlighter();
self.painter.emit()?;
let (code_fragment, line_numbers) = parse_hunk_header(line);
if self.config.line_numbers {
self.painter
.line_numbers_data
.initialize_hunk(&line_numbers, self.plus_file.to_string());
}
if self.config.hunk_header_style.is_raw {
write_hunk_header_raw(&mut self.painter, line, raw_line, self.config)?;
} else if self.config.hunk_header_style.is_omitted {
writeln!(self.painter.writer)?;
} else {
// Add a blank line below the hunk-header-line for readability, unless
// color_only mode is active.
if !self.config.color_only {
writeln!(self.painter.writer)?;
}
write_hunk_header(
&code_fragment,
&line_numbers,
&mut self.painter,
line,
if self.plus_file == "/dev/null" {
&self.minus_file
} else {
&self.plus_file
},
self.config,
)?;
};
self.painter.set_highlighter();
Ok(true)
}
}
lazy_static! {
static ref HUNK_HEADER_REGEX: Regex = Regex::new(r"@+ ([^@]+)@+(.*\s?)").unwrap();
}
// Parse unified diff hunk header format. See
// https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html
// https://www.artima.com/weblogs/viewpost.jsp?thread=164293
lazy_static! {
static ref HUNK_HEADER_FILE_COORDINATE_REGEX: Regex = Regex::new(
r"(?x)
[-+]
(\d+) # 1. Hunk start line number
(?: # Start optional hunk length section (non-capturing)
, # Literal comma
(\d+) # 2. Optional hunk length (defaults to 1)
)?"
)
.unwrap();
}
/// Given input like
/// "@@ -74,15 +74,14 @@ pub fn delta("
/// Return " pub fn delta(" and a vector of (line_number, hunk_length) tuples.
fn parse_hunk_header(line: &str) -> (String, Vec<(usize, usize)>) {
let caps = HUNK_HEADER_REGEX.captures(line).unwrap();
let file_coordinates = &caps[1];
let line_numbers_and_hunk_lengths = HUNK_HEADER_FILE_COORDINATE_REGEX
.captures_iter(file_coordinates)
.map(|caps| {
(
caps[1].parse::<usize>().unwrap(),
caps.get(2)
.map(|m| m.as_str())
// Per the specs linked above, if the hunk length is absent then it is 1.
.unwrap_or("1")
.parse::<usize>()
.unwrap(),
)
})
.collect();
let code_fragment = &caps[2];
(code_fragment.to_string(), line_numbers_and_hunk_lengths)
}
fn write_hunk_header_raw(
painter: &mut Painter,
line: &str,
raw_line: &str,
config: &Config,
) -> std::io::Result<()> {
let (mut draw_fn, pad, decoration_ansi_term_style) =
draw::get_draw_function(config.hunk_header_style.decoration_style);
if config.hunk_header_style.decoration_style != DecorationStyle::NoDecoration {
writeln!(painter.writer)?;
}
draw_fn(
painter.writer,
&format!("{}{}", line, if pad { " " } else { "" }),
&format!("{}{}", raw_line, if pad { " " } else { "" }),
&config.decorations_width,
config.hunk_header_style,
decoration_ansi_term_style,
)?;
Ok(())
}
fn write_hunk_header(
code_fragment: &str,
line_numbers: &[(usize, usize)],
painter: &mut Painter,
line: &str,
plus_file: &str,
config: &Config,
) -> std::io::Result<()> {
let (mut draw_fn, _, decoration_ansi_term_style) =
draw::get_draw_function(config.hunk_header_style.decoration_style);
let line = if config.color_only {
format!(" {}", &line)
} else if !code_fragment.is_empty() {
format!("{} ", code_fragment)
} else {
"".to_string()
};
let file_with_line_number = get_painted_file_with_line_number(line_numbers, plus_file, config);
if !line.is_empty() || !file_with_line_number.is_empty() {
write_to_output_buffer(&file_with_line_number, line, painter, config);
draw_fn(
painter.writer,
&painter.output_buffer,
&painter.output_buffer,
&config.decorations_width,
config.null_style,
decoration_ansi_term_style,
)?;
painter.output_buffer.clear();
}
Ok(())
}
fn get_painted_file_with_line_number(
line_numbers: &[(usize, usize)],
plus_file: &str,
config: &Config,
) -> String {
let mut file_with_line_number = Vec::new();
let plus_line_number = line_numbers[line_numbers.len() - 1].0;
if config.hunk_header_style_include_file_path {
file_with_line_number.push(config.hunk_header_file_style.paint(plus_file))
};
<
|