Skip to content

Commit

Permalink
Pass DisplayOptions as a single argument to print functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Apr 29, 2022
1 parent 12ef8f9 commit 4c7ab48
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 52 deletions.
30 changes: 20 additions & 10 deletions src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@ use crate::{
context::{calculate_after_context, calculate_before_context, opposite_positions},
hunks::Hunk,
lines::{format_line_num, MaxLine},
style::{self, apply_colors, BackgroundColor},
options::DisplayOptions,
style::{self, apply_colors},
syntax::MatchedPos,
};
use owo_colors::colored::*;

// TODO: take display options
pub fn print(
lhs_src: &str,
rhs_src: &str,
display_options: &DisplayOptions,
lhs_positions: &[MatchedPos],
rhs_positions: &[MatchedPos],
hunks: &[Hunk],
syntax_highlight: bool,
display_path: &str,
lang_name: &str,
use_color: bool,
background: BackgroundColor,
) {
let (lhs_colored, rhs_colored) = if use_color {
let (lhs_colored, rhs_colored) = if display_options.use_color {
(
apply_colors(lhs_src, true, syntax_highlight, background, lhs_positions),
apply_colors(rhs_src, false, syntax_highlight, background, rhs_positions),
apply_colors(
lhs_src,
true,
display_options.syntax_highlight,
display_options.background_color,
lhs_positions,
),
apply_colors(
rhs_src,
false,
display_options.syntax_highlight,
display_options.background_color,
rhs_positions,
),
)
} else {
(lhs_src.to_string(), rhs_src.to_string())
Expand All @@ -45,8 +55,8 @@ pub fn print(
i + 1,
hunks.len(),
lang_name,
use_color,
background
display_options.use_color,
display_options.background_color
)
);

Expand Down
10 changes: 2 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,18 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
inline::print(
lhs_src,
rhs_src,
display_options,
&summary.lhs_positions,
&summary.rhs_positions,
&hunks,
display_options.syntax_highlight,
&summary.path,
&lang_name,
display_options.use_color,
display_options.background_color,
);
}
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
side_by_side::print(
&hunks,
display_options.display_width,
display_options.use_color,
display_options.syntax_highlight,
display_options.display_mode,
display_options.background_color,
display_options,
&summary.path,
&lang_name,
lhs_src,
Expand Down
93 changes: 59 additions & 34 deletions src/side_by_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
context::all_matched_lines_filled,
hunks::{matched_lines_for_hunk, Hunk},
lines::{codepoint_len, format_line_num, LineNumber},
options::DisplayMode,
options::{DisplayMode, DisplayOptions},
positions::SingleLineSpan,
style::{self, apply_colors, color_positions, novel_style, split_and_apply, BackgroundColor},
syntax::{zip_pad_shorter, MatchedPos},
Expand Down Expand Up @@ -301,25 +301,32 @@ fn highlight_as_novel(
false
}

// TODO: pass display options here.
pub fn print(
hunks: &[Hunk],
display_width: usize,
use_color: bool,
syntax_highlight: bool,
display_mode: DisplayMode,
background: BackgroundColor,
display_options: &DisplayOptions,
display_path: &str,
lang_name: &str,
lhs_src: &str,
rhs_src: &str,
lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos],
) {
let (lhs_colored_src, rhs_colored_src) = if use_color {
let (lhs_colored_src, rhs_colored_src) = if display_options.use_color {
(
apply_colors(lhs_src, true, syntax_highlight, background, lhs_mps),
apply_colors(rhs_src, false, syntax_highlight, background, rhs_mps),
apply_colors(
lhs_src,
true,
display_options.syntax_highlight,
display_options.background_color,
lhs_mps,
),
apply_colors(
rhs_src,
false,
display_options.syntax_highlight,
display_options.background_color,
rhs_mps,
),
)
} else {
(lhs_src.to_string(), rhs_src.to_string())
Expand All @@ -333,8 +340,8 @@ pub fn print(
lang_name,
&rhs_colored_src,
false,
use_color,
background
display_options.use_color,
display_options.background_color
)
);
return;
Expand All @@ -347,16 +354,21 @@ pub fn print(
lang_name,
&lhs_colored_src,
true,
use_color,
background
display_options.use_color,
display_options.background_color
)
);
return;
}

// TODO: this is largely duplicating the `apply_colors` logic.
let (lhs_highlights, rhs_highlights) = if use_color {
highlight_positions(background, syntax_highlight, lhs_mps, rhs_mps)
let (lhs_highlights, rhs_highlights) = if display_options.use_color {
highlight_positions(
display_options.background_color,
display_options.syntax_highlight,
lhs_mps,
rhs_mps,
)
} else {
(HashMap::new(), HashMap::new())
};
Expand All @@ -381,8 +393,8 @@ pub fn print(
i + 1,
hunks.len(),
lang_name,
use_color,
background
display_options.use_color,
display_options.background_color
)
);

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

let source_dims =
SourceDimensions::new(display_width, &aligned_lines, &lhs_lines, &rhs_lines);
let source_dims = SourceDimensions::new(
display_options.display_width,
&aligned_lines,
&lhs_lines,
&rhs_lines,
);
for (lhs_line_num, rhs_line_num) in aligned_lines {
let lhs_line_novel = highlight_as_novel(
lhs_line_num,
Expand All @@ -411,15 +427,18 @@ pub fn print(
lhs_line_num,
rhs_line_num,
&source_dims,
use_color,
background,
display_options.use_color,
display_options.background_color,
lhs_line_novel,
rhs_line_novel,
prev_lhs_line_num,
prev_rhs_line_num,
);

let show_both = matches!(display_mode, DisplayMode::SideBySideShowBoth);
let show_both = matches!(
display_options.display_mode,
DisplayMode::SideBySideShowBoth
);
if no_lhs_changes && !show_both {
match rhs_line_num {
Some(rhs_line_num) => {
Expand Down Expand Up @@ -462,7 +481,7 @@ pub fn print(
Some(lhs_line_num) => split_and_apply(
lhs_lines[lhs_line_num.0],
source_dims.lhs_content_width,
use_color,
display_options.use_color,
lhs_highlights.get(&lhs_line_num).unwrap_or(&vec![]),
Side::Left,
),
Expand All @@ -472,7 +491,7 @@ pub fn print(
Some(rhs_line_num) => split_and_apply(
rhs_lines[rhs_line_num.0],
source_dims.rhs_content_width,
use_color,
display_options.use_color,
rhs_highlights.get(&rhs_line_num).unwrap_or(&vec![]),
Side::Right,
),
Expand All @@ -494,11 +513,11 @@ pub fn print(
.unwrap_or_else(|| prev_lhs_line_num.unwrap_or_else(|| 10.into())),
&source_dims,
true,
use_color,
display_options.use_color,
);
if let Some(line_num) = lhs_line_num {
if lhs_lines_with_novel.contains(&line_num) {
s = if background.is_dark() {
s = if display_options.background_color.is_dark() {
s.bright_red().to_string()
} else {
s.red().to_string()
Expand All @@ -515,11 +534,11 @@ pub fn print(
.unwrap_or_else(|| prev_rhs_line_num.unwrap_or_else(|| 10.into())),
&source_dims,
false,
use_color,
display_options.use_color,
);
if let Some(line_num) = rhs_line_num {
if rhs_lines_with_novel.contains(&line_num) {
s = if background.is_dark() {
s = if display_options.background_color.is_dark() {
s.bright_green().to_string()
} else {
s.green().to_string()
Expand Down Expand Up @@ -690,14 +709,20 @@ mod tests {
lines: vec![(Some(0.into()), Some(0.into()))],
}];

let display_options = DisplayOptions {
background_color: BackgroundColor::Dark,
use_color: true,
display_mode: DisplayMode::SideBySide,
print_unchanged: true,
tab_width: 8,
display_width: 80,
syntax_highlight: true,
};

// Simple smoke test.
print(
&hunks,
80,
true,
true,
DisplayMode::SideBySide,
BackgroundColor::Dark,
&display_options,
"foo.el",
"Emacs Lisp",
"foo",
Expand Down

0 comments on commit 4c7ab48

Please sign in to comment.