Skip to content

Commit 4c7ab48

Browse files
committed
Pass DisplayOptions as a single argument to print functions
1 parent 12ef8f9 commit 4c7ab48

File tree

3 files changed

+81
-52
lines changed

3 files changed

+81
-52
lines changed

src/inline.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@ use crate::{
44
context::{calculate_after_context, calculate_before_context, opposite_positions},
55
hunks::Hunk,
66
lines::{format_line_num, MaxLine},
7-
style::{self, apply_colors, BackgroundColor},
7+
options::DisplayOptions,
8+
style::{self, apply_colors},
89
syntax::MatchedPos,
910
};
1011
use owo_colors::colored::*;
1112

12-
// TODO: take display options
1313
pub fn print(
1414
lhs_src: &str,
1515
rhs_src: &str,
16+
display_options: &DisplayOptions,
1617
lhs_positions: &[MatchedPos],
1718
rhs_positions: &[MatchedPos],
1819
hunks: &[Hunk],
19-
syntax_highlight: bool,
2020
display_path: &str,
2121
lang_name: &str,
22-
use_color: bool,
23-
background: BackgroundColor,
2422
) {
25-
let (lhs_colored, rhs_colored) = if use_color {
23+
let (lhs_colored, rhs_colored) = if display_options.use_color {
2624
(
27-
apply_colors(lhs_src, true, syntax_highlight, background, lhs_positions),
28-
apply_colors(rhs_src, false, syntax_highlight, background, rhs_positions),
25+
apply_colors(
26+
lhs_src,
27+
true,
28+
display_options.syntax_highlight,
29+
display_options.background_color,
30+
lhs_positions,
31+
),
32+
apply_colors(
33+
rhs_src,
34+
false,
35+
display_options.syntax_highlight,
36+
display_options.background_color,
37+
rhs_positions,
38+
),
2939
)
3040
} else {
3141
(lhs_src.to_string(), rhs_src.to_string())
@@ -45,8 +55,8 @@ pub fn print(
4555
i + 1,
4656
hunks.len(),
4757
lang_name,
48-
use_color,
49-
background
58+
display_options.use_color,
59+
display_options.background_color
5060
)
5161
);
5262

src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,24 +431,18 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
431431
inline::print(
432432
lhs_src,
433433
rhs_src,
434+
display_options,
434435
&summary.lhs_positions,
435436
&summary.rhs_positions,
436437
&hunks,
437-
display_options.syntax_highlight,
438438
&summary.path,
439439
&lang_name,
440-
display_options.use_color,
441-
display_options.background_color,
442440
);
443441
}
444442
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
445443
side_by_side::print(
446444
&hunks,
447-
display_options.display_width,
448-
display_options.use_color,
449-
display_options.syntax_highlight,
450-
display_options.display_mode,
451-
display_options.background_color,
445+
display_options,
452446
&summary.path,
453447
&lang_name,
454448
lhs_src,

src/side_by_side.rs

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
context::all_matched_lines_filled,
1212
hunks::{matched_lines_for_hunk, Hunk},
1313
lines::{codepoint_len, format_line_num, LineNumber},
14-
options::DisplayMode,
14+
options::{DisplayMode, DisplayOptions},
1515
positions::SingleLineSpan,
1616
style::{self, apply_colors, color_positions, novel_style, split_and_apply, BackgroundColor},
1717
syntax::{zip_pad_shorter, MatchedPos},
@@ -301,25 +301,32 @@ fn highlight_as_novel(
301301
false
302302
}
303303

304-
// TODO: pass display options here.
305304
pub fn print(
306305
hunks: &[Hunk],
307-
display_width: usize,
308-
use_color: bool,
309-
syntax_highlight: bool,
310-
display_mode: DisplayMode,
311-
background: BackgroundColor,
306+
display_options: &DisplayOptions,
312307
display_path: &str,
313308
lang_name: &str,
314309
lhs_src: &str,
315310
rhs_src: &str,
316311
lhs_mps: &[MatchedPos],
317312
rhs_mps: &[MatchedPos],
318313
) {
319-
let (lhs_colored_src, rhs_colored_src) = if use_color {
314+
let (lhs_colored_src, rhs_colored_src) = if display_options.use_color {
320315
(
321-
apply_colors(lhs_src, true, syntax_highlight, background, lhs_mps),
322-
apply_colors(rhs_src, false, syntax_highlight, background, rhs_mps),
316+
apply_colors(
317+
lhs_src,
318+
true,
319+
display_options.syntax_highlight,
320+
display_options.background_color,
321+
lhs_mps,
322+
),
323+
apply_colors(
324+
rhs_src,
325+
false,
326+
display_options.syntax_highlight,
327+
display_options.background_color,
328+
rhs_mps,
329+
),
323330
)
324331
} else {
325332
(lhs_src.to_string(), rhs_src.to_string())
@@ -333,8 +340,8 @@ pub fn print(
333340
lang_name,
334341
&rhs_colored_src,
335342
false,
336-
use_color,
337-
background
343+
display_options.use_color,
344+
display_options.background_color
338345
)
339346
);
340347
return;
@@ -347,16 +354,21 @@ pub fn print(
347354
lang_name,
348355
&lhs_colored_src,
349356
true,
350-
use_color,
351-
background
357+
display_options.use_color,
358+
display_options.background_color
352359
)
353360
);
354361
return;
355362
}
356363

357364
// TODO: this is largely duplicating the `apply_colors` logic.
358-
let (lhs_highlights, rhs_highlights) = if use_color {
359-
highlight_positions(background, syntax_highlight, lhs_mps, rhs_mps)
365+
let (lhs_highlights, rhs_highlights) = if display_options.use_color {
366+
highlight_positions(
367+
display_options.background_color,
368+
display_options.syntax_highlight,
369+
lhs_mps,
370+
rhs_mps,
371+
)
360372
} else {
361373
(HashMap::new(), HashMap::new())
362374
};
@@ -381,8 +393,8 @@ pub fn print(
381393
i + 1,
382394
hunks.len(),
383395
lang_name,
384-
use_color,
385-
background
396+
display_options.use_color,
397+
display_options.background_color
386398
)
387399
);
388400

@@ -391,8 +403,12 @@ pub fn print(
391403
let no_rhs_changes = hunk.novel_rhs.is_empty();
392404
let same_lines = aligned_lines.iter().all(|(l, r)| l == r);
393405

394-
let source_dims =
395-
SourceDimensions::new(display_width, &aligned_lines, &lhs_lines, &rhs_lines);
406+
let source_dims = SourceDimensions::new(
407+
display_options.display_width,
408+
&aligned_lines,
409+
&lhs_lines,
410+
&rhs_lines,
411+
);
396412
for (lhs_line_num, rhs_line_num) in aligned_lines {
397413
let lhs_line_novel = highlight_as_novel(
398414
lhs_line_num,
@@ -411,15 +427,18 @@ pub fn print(
411427
lhs_line_num,
412428
rhs_line_num,
413429
&source_dims,
414-
use_color,
415-
background,
430+
display_options.use_color,
431+
display_options.background_color,
416432
lhs_line_novel,
417433
rhs_line_novel,
418434
prev_lhs_line_num,
419435
prev_rhs_line_num,
420436
);
421437

422-
let show_both = matches!(display_mode, DisplayMode::SideBySideShowBoth);
438+
let show_both = matches!(
439+
display_options.display_mode,
440+
DisplayMode::SideBySideShowBoth
441+
);
423442
if no_lhs_changes && !show_both {
424443
match rhs_line_num {
425444
Some(rhs_line_num) => {
@@ -462,7 +481,7 @@ pub fn print(
462481
Some(lhs_line_num) => split_and_apply(
463482
lhs_lines[lhs_line_num.0],
464483
source_dims.lhs_content_width,
465-
use_color,
484+
display_options.use_color,
466485
lhs_highlights.get(&lhs_line_num).unwrap_or(&vec![]),
467486
Side::Left,
468487
),
@@ -472,7 +491,7 @@ pub fn print(
472491
Some(rhs_line_num) => split_and_apply(
473492
rhs_lines[rhs_line_num.0],
474493
source_dims.rhs_content_width,
475-
use_color,
494+
display_options.use_color,
476495
rhs_highlights.get(&rhs_line_num).unwrap_or(&vec![]),
477496
Side::Right,
478497
),
@@ -494,11 +513,11 @@ pub fn print(
494513
.unwrap_or_else(|| prev_lhs_line_num.unwrap_or_else(|| 10.into())),
495514
&source_dims,
496515
true,
497-
use_color,
516+
display_options.use_color,
498517
);
499518
if let Some(line_num) = lhs_line_num {
500519
if lhs_lines_with_novel.contains(&line_num) {
501-
s = if background.is_dark() {
520+
s = if display_options.background_color.is_dark() {
502521
s.bright_red().to_string()
503522
} else {
504523
s.red().to_string()
@@ -515,11 +534,11 @@ pub fn print(
515534
.unwrap_or_else(|| prev_rhs_line_num.unwrap_or_else(|| 10.into())),
516535
&source_dims,
517536
false,
518-
use_color,
537+
display_options.use_color,
519538
);
520539
if let Some(line_num) = rhs_line_num {
521540
if rhs_lines_with_novel.contains(&line_num) {
522-
s = if background.is_dark() {
541+
s = if display_options.background_color.is_dark() {
523542
s.bright_green().to_string()
524543
} else {
525544
s.green().to_string()
@@ -690,14 +709,20 @@ mod tests {
690709
lines: vec![(Some(0.into()), Some(0.into()))],
691710
}];
692711

712+
let display_options = DisplayOptions {
713+
background_color: BackgroundColor::Dark,
714+
use_color: true,
715+
display_mode: DisplayMode::SideBySide,
716+
print_unchanged: true,
717+
tab_width: 8,
718+
display_width: 80,
719+
syntax_highlight: true,
720+
};
721+
693722
// Simple smoke test.
694723
print(
695724
&hunks,
696-
80,
697-
true,
698-
true,
699-
DisplayMode::SideBySide,
700-
BackgroundColor::Dark,
725+
&display_options,
701726
"foo.el",
702727
"Emacs Lisp",
703728
"foo",

0 commit comments

Comments
 (0)