Replace remaining is_lhs booleans with Side arguments

pull/356/merge
Wilfred Hughes 2022-10-28 19:27:46 +07:00
parent 7ea4b96a41
commit 1d6c7923e3
3 changed files with 43 additions and 48 deletions

@ -26,7 +26,7 @@ pub fn print(
(
apply_colors(
lhs_src,
true,
Side::Left,
display_options.syntax_highlight,
language,
display_options.background_color,
@ -34,7 +34,7 @@ pub fn print(
),
apply_colors(
rhs_src,
false,
Side::Right,
display_options.syntax_highlight,
language,
display_options.background_color,

@ -34,19 +34,17 @@ fn format_line_num_padded(line_num: LineNumber, column_width: usize) -> String {
fn format_missing_line_num(
prev_num: LineNumber,
source_dims: &SourceDimensions,
is_lhs: bool, // Side here
side: Side,
use_color: bool,
) -> String {
let column_width = if is_lhs {
source_dims.lhs_line_nums_width
} else {
source_dims.rhs_line_nums_width
let column_width = match side {
Side::Left => source_dims.lhs_line_nums_width,
Side::Right => source_dims.rhs_line_nums_width,
};
let after_end = if is_lhs {
prev_num >= source_dims.lhs_max_line
} else {
prev_num >= source_dims.rhs_max_line
let after_end = match side {
Side::Left => prev_num >= source_dims.lhs_max_line,
Side::Right => prev_num >= source_dims.rhs_max_line,
};
let mut style = Style::new();
@ -70,7 +68,7 @@ fn display_single_column(
rhs_display_path: &str,
lang_name: &str,
src_lines: &[String],
is_lhs: bool,
side: Side,
display_options: &DisplayOptions,
) -> Vec<String> {
let column_width = format_line_num((src_lines.len() as u32).into()).len();
@ -91,7 +89,7 @@ fn display_single_column(
let mut style = Style::new();
if display_options.use_color {
style = novel_style(Style::new(), is_lhs, display_options.background_color);
style = novel_style(Style::new(), side, display_options.background_color);
}
for (i, line) in src_lines.iter().enumerate() {
@ -126,7 +124,7 @@ fn display_line_nums(
None => format_missing_line_num(
prev_lhs_line_num.unwrap_or_else(|| 1.into()),
source_dims,
true,
Side::Left,
display_options.use_color,
),
};
@ -138,7 +136,7 @@ fn display_line_nums(
None => format_missing_line_num(
prev_rhs_line_num.unwrap_or_else(|| 1.into()),
source_dims,
false,
Side::Right,
display_options.use_color,
),
};
@ -249,7 +247,8 @@ fn highlight_positions(
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>>,
) {
let lhs_positions = color_positions(true, background, syntax_highlight, language, lhs_mps);
let lhs_positions =
color_positions(Side::Left, background, syntax_highlight, language, lhs_mps);
// Preallocate the hashmap assuming the average line will have 2 items on it.
let mut lhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = FxHashMap::default();
for (span, style) in lhs_positions {
@ -257,7 +256,8 @@ fn highlight_positions(
styles.push((span, style));
}
let rhs_positions = color_positions(false, background, syntax_highlight, language, rhs_mps);
let rhs_positions =
color_positions(Side::Right, background, syntax_highlight, language, rhs_mps);
let mut rhs_styles: FxHashMap<LineNumber, Vec<(SingleLineSpan, Style)>> = FxHashMap::default();
for (span, style) in rhs_positions {
let styles = rhs_styles.entry(span.line).or_insert_with(Vec::new);
@ -307,7 +307,7 @@ pub fn print(
(
apply_colors(
lhs_src,
true,
Side::Left,
display_options.syntax_highlight,
language,
display_options.background_color,
@ -315,7 +315,7 @@ pub fn print(
),
apply_colors(
rhs_src,
false,
Side::Right,
display_options.syntax_highlight,
language,
display_options.background_color,
@ -341,7 +341,7 @@ pub fn print(
rhs_display_path,
lang_name,
&rhs_colored_lines,
false,
Side::Right,
display_options,
) {
print!("{}", line);
@ -355,7 +355,7 @@ pub fn print(
rhs_display_path,
lang_name,
&lhs_colored_lines,
true,
Side::Left,
display_options,
) {
print!("{}", line);
@ -525,7 +525,7 @@ pub fn print(
lhs_line_num
.unwrap_or_else(|| prev_lhs_line_num.unwrap_or_else(|| 10.into())),
&source_dims,
true,
Side::Left,
display_options.use_color,
);
if let Some(line_num) = lhs_line_num {
@ -547,7 +547,7 @@ pub fn print(
rhs_line_num
.unwrap_or_else(|| prev_rhs_line_num.unwrap_or_else(|| 10.into())),
&source_dims,
false,
Side::Right,
display_options.use_color,
);
if let Some(line_num) = rhs_line_num {
@ -610,11 +610,11 @@ mod tests {
);
assert_eq!(
format_missing_line_num(0.into(), &source_dims, true, true),
format_missing_line_num(0.into(), &source_dims, Side::Left, true),
". ".dimmed().to_string()
);
assert_eq!(
format_missing_line_num(0.into(), &source_dims, true, false),
format_missing_line_num(0.into(), &source_dims, Side::Left, false),
". ".to_string()
);
}
@ -632,11 +632,11 @@ mod tests {
);
assert_eq!(
format_missing_line_num(1.into(), &source_dims, true, true),
format_missing_line_num(1.into(), &source_dims, Side::Left, true),
" ".dimmed().to_string()
);
assert_eq!(
format_missing_line_num(1.into(), &source_dims, true, false),
format_missing_line_num(1.into(), &source_dims, Side::Left, false),
" ".to_string()
);
}
@ -661,7 +661,7 @@ mod tests {
"foo.py",
"Python",
&["print(123)\n".to_string()],
false,
Side::Right,
&display_options,
);
let res = res_lines.join("");

@ -257,23 +257,22 @@ fn style_lines(lines: &[&str], styles: &[(SingleLineSpan, Style)]) -> Vec<String
res
}
// TODO: Side here
pub fn novel_style(style: Style, is_lhs: bool, background: BackgroundColor) -> Style {
pub fn novel_style(style: Style, side: Side, background: BackgroundColor) -> Style {
if background.is_dark() {
if is_lhs {
style.bright_red()
} else {
style.bright_green()
match side {
Side::Left => style.bright_red(),
Side::Right => style.bright_green(),
}
} else if is_lhs {
style.red()
} else {
style.green()
match side {
Side::Left => style.red(),
Side::Right => style.green(),
}
}
}
pub fn color_positions(
is_lhs: bool,
side: Side,
background: BackgroundColor,
syntax_highlight: bool,
language: Option<Language>,
@ -312,7 +311,7 @@ pub fn color_positions(
}
}
MatchKind::Novel { highlight, .. } => {
style = novel_style(style, is_lhs, background);
style = novel_style(style, side, background);
if syntax_highlight
&& matches!(
highlight,
@ -328,7 +327,7 @@ pub fn color_positions(
}
}
MatchKind::NovelWord { highlight } => {
style = novel_style(style, is_lhs, background).bold();
style = novel_style(style, side, background).bold();
// Underline novel words inside comments in code, but
// don't apply it to every single line in plaintext.
@ -341,7 +340,7 @@ pub fn color_positions(
}
}
MatchKind::NovelLinePart { highlight, .. } => {
style = novel_style(style, is_lhs, background);
style = novel_style(style, side, background);
if syntax_highlight && matches!(highlight, TokenKind::Atom(AtomKind::Comment)) {
style = style.italic();
}
@ -354,13 +353,13 @@ pub fn color_positions(
pub fn apply_colors(
s: &str,
is_lhs: bool,
side: Side,
syntax_highlight: bool,
language: Option<Language>,
background: BackgroundColor,
positions: &[MatchedPos],
) -> Vec<String> {
let styles = color_positions(is_lhs, background, syntax_highlight, language, positions);
let styles = color_positions(side, background, syntax_highlight, language, positions);
let lines = split_on_newlines(s);
style_lines(&lines, &styles)
}
@ -388,11 +387,7 @@ pub(crate) fn apply_line_number_color(
if display_options.use_color {
let mut style = Style::new();
if is_novel {
style = novel_style(
style,
matches!(side, Side::Left),
display_options.background_color,
);
style = novel_style(style, side, display_options.background_color);
}
// TODO: dimmed