Ensure --color is still respected

This was broken in the port to owo_colors. owo_colors does not have a
global override: the override function only affects if_supports_color.
html_output
Wilfred Hughes 2022-02-13 16:54:57 +07:00
parent 714edd8f0c
commit 3c1c5649f4
7 changed files with 109 additions and 50 deletions

19
Cargo.lock generated

@ -232,12 +232,6 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "is_ci"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"
[[package]]
name = "itertools"
version = "0.10.3"
@ -334,9 +328,6 @@ name = "owo-colors"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20448fd678ec04e6ea15bbe0476874af65e98a01515d667aa49f1434dc44ebf4"
dependencies = [
"supports-color",
]
[[package]]
name = "pretty_assertions"
@ -459,16 +450,6 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "supports-color"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4872ced36b91d47bae8a214a683fe54e7078875b399dfa251df346c9b547d1f9"
dependencies = [
"atty",
"is_ci",
]
[[package]]
name = "syn"
version = "1.0.84"

@ -39,7 +39,7 @@ radix-heap = "0.4.1"
walkdir = "2.3.2"
term_size = "0.3.2"
const_format = "0.2.22"
owo-colors = { version = "3.2.0", features = ["supports-colors"]}
owo-colors = "3.2.0"
[dev-dependencies]
pretty_assertions = "1.0.0"

@ -17,10 +17,17 @@ pub fn print(
hunks: &[Hunk],
display_path: &str,
lang_name: &str,
use_color: bool,
background: BackgroundColor,
) {
let lhs_colored = apply_colors(lhs_src, true, background, lhs_positions);
let rhs_colored = apply_colors(rhs_src, false, background, rhs_positions);
let (lhs_colored, rhs_colored) = if use_color {
(
apply_colors(lhs_src, true, background, lhs_positions),
apply_colors(rhs_src, false, background, rhs_positions),
)
} else {
(lhs_src.to_string(), rhs_src.to_string())
};
let lhs_lines: Vec<_> = lhs_colored.lines().collect();
let rhs_lines: Vec<_> = rhs_colored.lines().collect();
@ -31,7 +38,14 @@ pub fn print(
for (i, hunk) in hunks.iter().enumerate() {
println!(
"{}",
style::header(display_path, i + 1, hunks.len(), lang_name, background)
style::header(
display_path,
i + 1,
hunks.len(),
lang_name,
use_color,
background
)
);
let hunk_lines = hunk.lines.clone();

@ -44,7 +44,7 @@ use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use options::{configure_color, Mode};
use options::{should_use_color, Mode};
use sliders::fix_all_sliders;
use std::{env, path::Path};
use style::BackgroundColor;
@ -129,7 +129,7 @@ fn main() {
rhs_path,
..
} => {
configure_color(color_output);
let use_color = should_use_color(color_output);
let lhs_path = Path::new(&lhs_path);
let rhs_path = Path::new(&rhs_path);
@ -140,6 +140,7 @@ fn main() {
{
print_diff_result(
display_width,
use_color,
background_color,
print_unchanged,
&diff_result,
@ -156,6 +157,7 @@ fn main() {
);
print_diff_result(
display_width,
use_color,
background_color,
print_unchanged,
&diff_result,
@ -337,6 +339,7 @@ fn diff_directories(
// TODO: factor out a DiffOptions struct.
fn print_diff_result(
display_width: usize,
use_color: bool,
background: BackgroundColor,
print_unchanged: bool,
summary: &DiffResult,
@ -360,7 +363,7 @@ fn print_diff_result(
if print_unchanged {
println!(
"{}",
style::header(&summary.path, 1, 1, &lang_name, background)
style::header(&summary.path, 1, 1, &lang_name, use_color, background)
);
if lang_name == "Text" {
// TODO: there are other Text names now, so
@ -382,12 +385,14 @@ fn print_diff_result(
&hunks,
&summary.path,
&lang_name,
use_color,
background,
);
} else {
side_by_side::print(
&hunks,
display_width,
use_color,
background,
&summary.path,
&lang_name,
@ -403,7 +408,7 @@ fn print_diff_result(
if print_unchanged || changed {
println!(
"{}",
style::header(&summary.path, 1, 1, "binary", background)
style::header(&summary.path, 1, 1, "binary", use_color, background)
);
if changed {
println!("Binary contents changed.")
@ -416,7 +421,7 @@ fn print_diff_result(
// We're diffing a binary file against a text file.
println!(
"{}",
style::header(&summary.path, 1, 1, "binary", background)
style::header(&summary.path, 1, 1, "binary", use_color, background)
);
println!("Binary contents changed.")
}

@ -289,8 +289,8 @@ fn detect_display_width() -> usize {
term_size::dimensions().map(|(w, _)| w).unwrap_or(80)
}
pub fn configure_color(color_output: ColorOutput) {
let enable_color = match color_output {
pub fn should_use_color(color_output: ColorOutput) -> bool {
match color_output {
ColorOutput::Always => true,
ColorOutput::Auto => {
// Always enable colour if stdout is a TTY or if the git pager is active.
@ -299,9 +299,7 @@ pub fn configure_color(color_output: ColorOutput) {
atty::is(Stream::Stdout) || env::var("GIT_PAGER_IN_USE").is_ok()
}
ColorOutput::Never => false,
};
owo_colors::set_override(enable_color);
}
}
#[cfg(test)]

@ -70,15 +70,26 @@ fn display_single_column(
lang_name: &str,
src: &str,
is_lhs: bool,
use_color: bool,
background: BackgroundColor,
) -> String {
let column_width = format_line_num(src.lines().count().into()).len();
let mut result = String::with_capacity(src.len());
result.push_str(&style::header(display_path, 1, 1, lang_name, background));
result.push_str(&style::header(
display_path,
1,
1,
lang_name,
use_color,
background,
));
result.push('\n');
let style = novel_style(Style::new(), is_lhs, background);
let mut style = Style::new();
if use_color {
style = novel_style(Style::new(), is_lhs, background);
}
for (i, line) in src.lines().enumerate() {
result.push_str(
@ -97,6 +108,7 @@ fn display_line_nums(
lhs_line_num: Option<LineNumber>,
rhs_line_num: Option<LineNumber>,
source_dims: &SourceDimensions,
use_color: bool,
background: BackgroundColor,
lhs_has_novel: bool,
rhs_has_novel: bool,
@ -106,7 +118,7 @@ fn display_line_nums(
let display_lhs_line_num: String = match lhs_line_num {
Some(line_num) => {
let s = format_line_num_padded(line_num, source_dims.lhs_line_nums_width);
if lhs_has_novel {
if lhs_has_novel && use_color {
// TODO: factor out applying colours to line numbers.
match background {
BackgroundColor::Dark => s.bright_red().to_string(),
@ -125,7 +137,7 @@ fn display_line_nums(
let display_rhs_line_num: String = match rhs_line_num {
Some(line_num) => {
let s = format_line_num_padded(line_num, source_dims.rhs_line_nums_width);
if rhs_has_novel {
if rhs_has_novel && use_color {
match background {
BackgroundColor::Dark => s.bright_green().to_string(),
BackgroundColor::Light => s.green().to_string(),
@ -271,6 +283,7 @@ fn highlight_as_novel(
pub fn print(
hunks: &[Hunk],
display_width: usize,
use_color: bool,
background: BackgroundColor,
display_path: &str,
lang_name: &str,
@ -279,26 +292,51 @@ pub fn print(
lhs_mps: &[MatchedPos],
rhs_mps: &[MatchedPos],
) {
let lhs_colored_src = apply_colors(lhs_src, true, background, lhs_mps);
let rhs_colored_src = apply_colors(rhs_src, false, background, rhs_mps);
let (lhs_colored_src, rhs_colored_src) = if use_color {
(
apply_colors(lhs_src, true, background, lhs_mps),
apply_colors(rhs_src, false, background, rhs_mps),
)
} else {
(lhs_src.to_string(), rhs_src.to_string())
};
if lhs_src.is_empty() {
println!(
"{}",
display_single_column(display_path, lang_name, &rhs_colored_src, false, background)
display_single_column(
display_path,
lang_name,
&rhs_colored_src,
false,
use_color,
background
)
);
return;
}
if rhs_src.is_empty() {
println!(
"{}",
display_single_column(display_path, lang_name, &lhs_colored_src, true, background)
display_single_column(
display_path,
lang_name,
&lhs_colored_src,
true,
use_color,
background
)
);
return;
}
// TODO: this is largely duplicating the `apply_colors` logic.
let (lhs_highlights, rhs_highlights) = highlight_positions(background, lhs_mps, rhs_mps);
let (lhs_highlights, rhs_highlights) = if use_color {
highlight_positions(background, lhs_mps, rhs_mps)
} else {
(HashMap::new(), HashMap::new())
};
let lhs_lines = split_on_newlines(lhs_src);
let rhs_lines = split_on_newlines(rhs_src);
let lhs_colored_lines = split_on_newlines(&lhs_colored_src);
@ -314,7 +352,14 @@ pub fn print(
for (i, hunk) in hunks.iter().enumerate() {
println!(
"{}",
style::header(display_path, i + 1, hunks.len(), lang_name, background)
style::header(
display_path,
i + 1,
hunks.len(),
lang_name,
use_color,
background
)
);
let aligned_lines = matched_lines_for_hunk(&matched_lines, hunk);
@ -342,6 +387,7 @@ pub fn print(
lhs_line_num,
rhs_line_num,
&source_dims,
use_color,
background,
lhs_line_novel,
rhs_line_novel,
@ -391,6 +437,7 @@ pub fn print(
Some(lhs_line_num) => split_and_apply(
lhs_lines[lhs_line_num.0],
source_dims.lhs_content_width,
use_color,
lhs_highlights.get(&lhs_line_num).unwrap_or(&vec![]),
),
None => vec![" ".repeat(source_dims.lhs_content_width)],
@ -399,6 +446,7 @@ pub fn print(
Some(rhs_line_num) => split_and_apply(
rhs_lines[rhs_line_num.0],
source_dims.rhs_content_width,
use_color,
rhs_highlights.get(&rhs_line_num).unwrap_or(&vec![]),
),
None => vec!["".into()],
@ -530,6 +578,7 @@ mod tests {
"Python",
"print(123)\n",
false,
true,
BackgroundColor::Dark,
);
assert!(res.len() > 10);
@ -601,6 +650,7 @@ mod tests {
print(
&hunks,
80,
true,
BackgroundColor::Dark,
"foo.el",
"Emacs Lisp",

@ -68,13 +68,20 @@ fn highlight_missing_style_bug(s: &str) -> String {
pub fn split_and_apply(
line: &str,
max_len: usize,
use_color: bool,
styles: &[(SingleLineSpan, Style)],
) -> Vec<String> {
if styles.is_empty() && !line.is_empty() {
// Missing styles is a bug, so highlight in purple to make this obvious.
return split_string(line, max_len)
.into_iter()
.map(|part| highlight_missing_style_bug(&part))
.map(|part| {
if use_color {
highlight_missing_style_bug(&part)
} else {
part
}
})
.collect();
}
@ -295,18 +302,22 @@ pub fn header(
hunk_num: usize,
hunk_total: usize,
language_name: &str,
use_color: bool,
background: BackgroundColor,
) -> String {
format!(
"{} --- {}/{} --- {}",
let file_name_pretty = if use_color {
match background {
BackgroundColor::Dark => file_name.bright_yellow().to_string(),
BackgroundColor::Light => file_name.yellow().to_string(),
}
.bold(),
hunk_num,
hunk_total,
language_name
.bold()
.to_string()
} else {
file_name.to_string()
};
format!(
"{} --- {}/{} --- {}",
file_name_pretty, hunk_num, hunk_total, language_name
)
}