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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
use itertools::Itertools;
use syntect::highlighting::Style as SyntectStyle;
use unicode_segmentation::UnicodeSegmentation;
use crate::ansi;
use crate::cli;
use crate::config::Config;
use crate::delta::State;
use crate::features::line_numbers;
use crate::features::OptionValueFunction;
use crate::minusplus::*;
use crate::paint::Painter;
use crate::paint::{BgFillMethod, BgShouldFill};
use crate::style::Style;
use crate::wrapping::wrap_zero_block;
pub type LineSegments<'a, S> = Vec<(S, &'a str)>;
pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
builtin_feature!([
(
"side-by-side",
bool,
None,
_opt => true
),
("features", bool, None, _opt => "line-numbers"),
("line-numbers-left-format", String, None, _opt => "│{nm:^4}│".to_string()),
("line-numbers-right-format", String, None, _opt => "│{np:^4}│".to_string())
])
}
// Aliases for Minus/Plus because Left/Right and PanelSide makes
// more sense in a side-by-side context.
pub use crate::minusplus::MinusPlusIndex as PanelSide;
pub use MinusPlusIndex::Minus as Left;
pub use MinusPlusIndex::Plus as Right;
#[derive(Debug)]
pub struct Panel {
pub width: usize,
}
pub type LeftRight<T> = MinusPlus<T>;
pub type SideBySideData = LeftRight<Panel>;
impl SideBySideData {
/// Create a [`LeftRight<Panel>`](LeftRight<Panel>) named [`SideBySideData`].
pub fn new_sbs(decorations_width: &cli::Width, available_terminal_width: &usize) -> Self {
let panel_width = match decorations_width {
cli::Width::Fixed(w) => w / 2,
_ => available_terminal_width / 2,
};
SideBySideData::new(Panel { width: panel_width }, Panel { width: panel_width })
}
}
pub fn available_line_width(
config: &Config,
data: &line_numbers::LineNumbersData,
) -> line_numbers::SideBySideLineWidth {
let linennumbers_width = data.formatted_width();
// The width can be reduced by the line numbers and/or a possibly kept 1-wide "+/-/ " prefix.
let line_width = |side: PanelSide| {
config.side_by_side_data[side]
.width
.saturating_sub(linennumbers_width[side])
.saturating_sub(config.keep_plus_minus_markers as usize)
};
LeftRight::new(line_width(Left), line_width(Right))
}
pub fn line_is_too_long(line: &str, line_width: usize) -> bool {
let line_sum = line.graphemes(true).count();
// `line_sum` is too large, because both a leading "+/-/ " and a trailing
// newline are present, counted, but are never printed. So allow two more
// characters.
line_sum > line_width + 2
}
/// Return whether any of the input lines is too long, and a data
/// structure indicating which are too long. This avoids
/// calculating the length again later.
pub fn has_long_lines(
lines: &LeftRight<&Vec<(String, State)>>,
line_width: &line_numbers::SideBySideLineWidth,
) -> (bool, LeftRight<Vec<bool>>) {
let mut wrap_any = LeftRight::default();
let mut wrapping_lines = LeftRight::default();
let mut check_if_too_long = |side| {
let lines_side: &Vec<(String, State)> = lines[side];
wrapping_lines[side] = lines_side
.iter()
.map(|(line, _)| line_is_too_long(line, line_width[side]))
.inspect(|b| wrap_any[side] |= b)
.collect();
};
check_if_too_long(Left);
check_if_too_long(Right);
(wrap_any[Left] || wrap_any[Right], wrapping_lines)
}
/// Emit a sequence of minus and plus lines in side-by-side mode.
#[allow(clippy::too_many_arguments)]
pub fn paint_minus_and_plus_lines_side_by_side<'a>(
syntax_left_right: LeftRight<Vec<LineSegments<'a, SyntectStyle>>>,
diff_left_right: LeftRight<Vec<LineSegments<'a, Style>>>,
states_left_right: LeftRight<Vec<State>>,
line_alignment: Vec<(Option<usize>, Option<usize>)>,
output_buffer: &mut String,
config: &Config,
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
background_color_extends_to_terminal_width: LeftRight<BgShouldFill>,
) {
for (minus_line_index, plus_line_index) in line_alignment {
output_buffer.push_str(&paint_left_panel_minus_line(
minus_line_index,
&syntax_left_right[Left],
&diff_left_right[Left],
match minus_line_index {
Some(i) => &states_left_right[Left][i],
None => &State::HunkMinus(None),
},
line_numbers_data,
background_color_extends_to_terminal_width[Left],
config,
));
output_buffer.push_str(&paint_right_panel_plus_line(
plus_line_index,
&syntax_left_right[Right],
&diff_left_right[Right],
match plus_line_index {
Some(i) => &states_left_right[Right][i],
None => &State::HunkPlus(None),
},
line_numbers_data,
background_color_extends_to_terminal_width[Right],
config,
));
output_buffer.push('\n');
}
}
#[allow(clippy::too_many_arguments)]
pub fn paint_zero_lines_side_by_side<'a>(
raw_line: &str,
syntax_style_sections: Vec<LineSegments<'a, SyntectStyle>>,
diff_style_sections: Vec<LineSegments<'a, Style>>,
output_buffer: &mut String,
config: &Config,
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
painted_prefix: Option<ansi_term::ANSIString>,
background_color_extends_to_terminal_width: BgShouldFill,
) {
let states = vec![State::HunkZero];
let (states, syntax_style_sections, diff_style_sections) = wrap_zero_block(
config,
raw_line,
states,
syntax_style_sections,
diff_style_sections,
line_numbers_data,
);
for (line_index, ((syntax_sections, diff_sections), state)) in syntax_style_sections
.into_iter()
.zip_eq(diff_style_sections.iter())
.zip_eq(states.into_iter())
.enumerate()
{
for panel_side in &[Left, Right] {
let (mut panel_line, panel_line_is_empty) = Painter::paint_line(
&syntax_sections,
diff_sections,
&state,
line_numbers_data,
Some(*panel_side),
painted_prefix.clone(),
config,
);
pad_panel_line_to_width(
&mut panel_line,
panel_line_is_empty,
Some(line_index),
&diff_style_sections,
&state,
*panel_side,
background_color_extends_to_terminal_width,
config,
);
output_buffer.push_str(&panel_line);
}
output_buffer.push('\n');
}
}
#[allow(clippy::too_many_arguments)]
fn paint_left_panel_minus_line<'a>(
line_index: Option<usize>,
syntax_style_sections: &[LineSegments<'a, SyntectStyle>],
diff_style_sections: &[LineSegments<'a, Style>],
state: &'a State,
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
background_color_extends_to_terminal_width: BgShouldFill,
config: &Config,
) -> String {
let (mut panel_line, panel_line_is_empty) = paint_minus_or_plus_panel_line(
line_index,
syntax_style_sections,
diff_style_sections,
state,
line_numbers_data,
Left,
config,
);
pad_panel_line_to_width(
&mut panel_line,
panel_line_is_empty,
line_index,
diff_style_sections,
state,
Left,
background_color_extends_to_terminal_width,
config,
);
panel_line
}
#[allow(clippy::too_many_arguments)]
fn paint_right_panel_plus_line<'a>(
line_index: Option<usize>,
syntax_style_sections: &[LineSegments<'a, SyntectStyle>],
diff_style_sections: &[LineSegments<'a, Style>],
state: &'a State,
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
background_color_extends_to_terminal_width: BgShouldFill,
config: &Config,
) -> String {
let (mut panel_line, panel_line_is_empty) = paint_minus_or_plus_panel_line(
line_index,
syntax_style_sections,
diff_style_sections,
state,
line_numbers_data,
Right,
config,
);
pad_panel_line_to_width(
&mut panel_line,
panel_line_is_empty,
line_index,
diff_style_sections,
state,
Right,
background_color_extends_to_terminal_width,
config,
);
panel_line
}
fn get_right_fill_style_for_panel<'a>(
line_is_empty: bool,
line_index: Option<usize>,
diff_style_sections: &[LineSegments<'a, Style>],
state: &State,
panel_side: PanelSide,
background_color_extends_to_terminal_width: BgShouldFill,
config: &Config,
) -> (Option<BgFillMethod>, Style) {
// If in the the left panel then it must be filled with spaces.
let none_or_override = if panel_side == Left {
Some(BgFillMethod::Spaces)
} else {
None
};
match (line_is_empty, line_index) {
(true, _) => (none_or_override, config.null_style),
(false, None) => (none_or_override, config.null_style),
(false, Some(index)) => {
let (bg_fill_mode, fill_style) =
Painter::get_should_right_fill_background_color_and_fill_style(
&diff_style_sections[index],
state,
background_color_extends_to_terminal_width,
config,
);
match bg_fill_mode {
None => (none_or_override, config.null_style),
_ if panel_side == Left => (Some(BgFillMethod::Spaces), fill_style),
_ => (bg_fill_mode, fill_style),
}
}
}
}
/// Construct half of a minus or plus line under side-by-side mode, i.e. the half line that
/// goes in one or other panel. Return a tuple `(painted_half_line, is_empty)`.
// Suppose the line being displayed is a minus line with a paired plus line. Then both times
// this function is called, `line_index` will be `Some`. This case proceeds as one would
// expect: on the first call, we are constructing the left panel line, and we are passed
// `(Some(index), HunkMinus, Left)`. We pass `(HunkMinus, Left)` to
// `paint_line`. This has two consequences:
// 1. `format_and_paint_line_numbers` will increment the minus line number.
// 2. `format_and_paint_line_numbers` will emit the left line number field, and not the right.
//
// The second call does the analogous thing for the plus line to be displayed in the right panel:
// we are passed `(Some(index), HunkPlus, Right)` and we pass `(HunkPlus, Right)` to `paint_line`,
// causing it to increment the plus line number and emit the right line number field.
//
// Now consider the case where the line being displayed is a minus line with no paired plus line.
// The first call is as before. On the second call, we are passed `(None, HunkPlus, Right)` and we
// wish to display the right panel, with its line number container, but without any line number
// (and without any line contents). We do this by passing (HunkMinus, Right) to `paint_line`, since
// what this will do is set the line number pair in that function to `(Some(minus_number), None)`,
// and then only emit the right field (which has a None number, i.e. blank). However, it will also
// increment the minus line number, so we need to knock that back down.
#[allow(clippy::too_many_arguments)]
fn paint_minus_or_plus_panel_line<'a>(
line_index: Option<usize>,
syntax_style_sections: &[LineSegments<'a, SyntectStyle>],
diff_style_sections: &[LineSegments<'a, Style>],
state: &State,
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>,
panel_side: PanelSide,
config: &Config,
) -> (String, bool) {
let (empty_line_syntax_sections, empty_line_diff_sections) = (Vec::new(), Vec::new());
let (line_syntax_sections, line_diff_sections, state_for_line_numbers_field) =
if let Some(index) = line_index {
(
&syntax_style_sections[index],
&diff_style_sections[index],
state.clone(),
)
} else {
let opposite_state = match state {
State::HunkMinus(x) => State::HunkPlus(x.clone()),
State::HunkPlus(x) => State::HunkMinus(x.clone()),
_ => unreachable!(),
};
(
&empty_line_syntax_sections,
&empty_line_diff_sections,
opposite_state,
)
};
let painted_prefix = match (config.keep_plus_minus_markers, panel_side, state) {
(true, _, State::HunkPlusWrapped) => Some(config.plus_style.paint(" ")),
(true, _, State::HunkMinusWrapped) => Some(config.minus_style.paint(" ")),
(true, Left, _) => Some(config.minus_style.paint("-")),
(true, Right, _) => Some(config.plus_style.paint("+")),
_ => None,
};
let (line, line_is_empty) = Painter::paint_line(
line_syntax_sections,
line_diff_sections,
&state_for_line_numbers_field,
line_numbers_data,
Some(panel_side),
painted_prefix,
config,
);
(line, line_is_empty)
}
/// Right-fill the background color of a line in a panel. If in the left panel this is always
/// done with spaces. The right panel can be filled with spaces or using ANSI sequences
/// instructing the terminal emulator to fill the background color rightwards.
#[allow(clippy::too_many_arguments, clippy::comparison_chain)]
fn pad_panel_line_to_width<'a>(
panel_line: &mut String,
panel_line_is_empty: bool,
line_index: Option<usize>,
diff_style_sections: &[LineSegments<'a, Style>],
state: &State,
panel_side: PanelSide,
background_color_extends_to_terminal_width: BgShouldFill,
config: &Config,
) {
// Emit empty line marker if the panel line is empty but not empty-by-construction. IOW if the
// other panel contains a real line, and we are currently emitting an empty counterpart panel
// to form the other half of the line, then don't emit the empty line marker.
if panel_line_is_empty && line_index.is_some() {
match state {
State::HunkMinus(_) => Painter::mark_empty_line(
&config.minus_empty_line_marker_style,
panel_line,
Some(" "),
),
State::HunkPlus(_) => Painter::mark_empty_line(
&config.plus_empty_line_marker_style,
panel_line,
Some(" "),
),
State::HunkZero => {}
_ => unreachable!(),
};
};
let text_width = ansi::measure_text_width(panel_line);
let panel_width = config.side_by_side_data[panel_side].width;
if text_width > panel_width {
*panel_line =
ansi::truncate_str(panel_line, panel_width, &config.truncation_symbol).to_string();
}
let (bg_fill_mode, fill_style) = get_right_fill_style_for_panel(
panel_line_is_empty,
line_index,
diff_style_sections,
state,
panel_side,
background_color_extends_to_terminal_width,
config,
);
match bg_fill_mode {
Some(BgFillMethod::TryAnsiSequence) => {
Painter::right_fill_background_color(panel_line, fill_style)
}
Some(BgFillMethod::Spaces) if text_width >= panel_width => (),
Some(BgFillMethod::Spaces) => panel_line.push_str(
&fill_style
.paint(" ".repeat(panel_width - text_width))
.to_string(),
),
None => (),
}
}
pub mod ansifill {
use super::SideBySideData;
use crate::config::Config;
use crate::paint::BgFillMethod;
pub const ODD_PAD_CHAR: char = ' ';
// Panels in side-by-side mode always sum up to an even number, so when the terminal
// has an odd width an extra column is left over.
// If the background color is extended with an ANSI sequence (which only knows "fill
// this row until the end") instead of spaces (see `BgFillMethod`), then the coloring
// extends into that column. This becomes noticeable when the displayed content reaches
// the right side of the right panel to be truncated or wrapped.
// However using an ANSI sequence instead of spaces is generally preferable because
// small changes to the terminal width are less noticeable.
/// The solution in this case is to add `ODD_PAD_CHAR` before the first line number in
/// the right panel and increasing its width by one, thus using the full terminal width
/// with the two panels.
/// This also means line numbers can not be disabled in side-by-side mode, but they may
/// not actually paint numbers.
#[derive(Clone, Debug)]
pub struct UseFullPanelWidth(pub bool);
impl UseFullPanelWidth {
pub fn new(config: &Config) -> Self {
Self(
config.side_by_side
&& Self::is_odd_with_ansi(&config.decorations_width, &config.line_fill_method),
)
}
pub fn sbs_odd_fix(
width: &crate::cli::Width,
method: &BgFillMethod,
sbs_data: SideBySideData,
) -> SideBySideData {
if Self::is_odd_with_ansi(width, method) {
Self::adapt_sbs_data(sbs_data)
} else {
sbs_data
}
}
pub fn pad_width(&self) -> bool {
self.0
}
fn is_odd_with_ansi(width: &crate::cli::Width, method: &BgFillMethod) -> bool {
method == &BgFillMethod::TryAnsiSequence
&& matches!(&width, crate::cli::Width::Fixed(width) if width % 2 == 1)
}
fn adapt_sbs_data(mut sbs_data: SideBySideData) -> SideBySideData {
sbs_data[super::Right].width += 1;
sbs_data
}
}
}
#[cfg(test)]
pub mod tests {
use crate::ansi::strip_ansi_codes;
use crate::features::line_numbers::tests::*;
use crate::tests::integration_test_utils::{make_config_from_args, run_delta};
#[test]
fn test_two_minus_lines() {
let config = make_config_from_args(&["--side-by-side", "--width", "40"]);
let output = run_delta(TWO_MINUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!("│ 1 │a = 1 │ │", strip_ansi_codes(line_1));
assert_eq!("│ 2 │b = 23456 │ │", strip_ansi_codes(line_2));
}
#[test]
fn test_two_minus_lines_truncated() {
let mut config = make_config_from_args(&[
"--side-by-side",
"--wrap-max-lines",
"0",
"--width",
"28",
"--line-fill-method=spaces",
]);
config.truncation_symbol = ">".into();
let output = run_delta(TWO_MINUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!("│ 1 │a = 1 │ │", strip_ansi_codes(line_1));
assert_eq!("│ 2 │b = 234>│ │", strip_ansi_codes(line_2));
}
#[test]
fn test_two_plus_lines() {
let config = make_config_from_args(&[
"--side-by-side",
"--width",
"41",
"--line-fill-method=spaces",
]);
let output = run_delta(TWO_PLUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
let sac = strip_ansi_codes; // alias to help with `cargo fmt`-ing:
assert_eq!("│ │ │ 1 │a = 1 ", sac(line_1));
assert_eq!("│ │ │ 2 │b = 234567 ", sac(line_2));
}
#[test]
fn test_two_plus_lines_truncated() {
let mut config = make_config_from_args(&[
"--side-by-side",
"--wrap-max-lines",
"0",
"--width",
"30",
"--line-fill-method=spaces",
]);
config.truncation_symbol = ">".into();
let output = run_delta(TWO_PLUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
assert_eq!("│ │ │ 1 │a = 1 ", strip_ansi_codes(line_1));
assert_eq!("│ │ │ 2 │b = 2345>", strip_ansi_codes(line_2));
}
#[test]
fn test_two_plus_lines_exact_fit() {
let config =
make_config_from_args(&["--side-by-side", "--width", "33", "--line-fill-method=ansi"]);
let output = run_delta(TWO_PLUS_LINES_DIFF, &config);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap());
let sac = strip_ansi_codes; // alias to help with `cargo fmt`-ing:
assert_eq!("│ │ │ 1 │a = 1", sac(line_1));
assert_eq!("│ │ │ 2 │b = 234567", sac(line_2));
}
#[test]
fn test_one_minus_one_plus_line() {
let config = make_config_from_args(&[
"--side-by-side",
"--width",
"40",
"--line-fill-method=spaces",
]);
let output = run_delta(ONE_MINUS_ONE_PLUS_LINE_DIFF, &config);
let output = strip_ansi_codes(&output);
let mut lines = output.lines().skip(crate::config::HEADER_LEN);
let mut lnu = move || lines.next().unwrap(); // for cargo fmt
assert_eq!("│ 1 │a = 1 │ 1 │a = 1", lnu());
assert_eq!("│ 2 │b = 2 │ 2 │bb = 2 ", lnu());
}
}
|