Skip to content

Commit 993e426

Browse files
committed
feat: add additional no-diff option to color
Add additional option `no-diff` to `--color`. This option disables the diff coloring, but keeps output coloring. Closes #56
1 parent fc0733b commit 993e426

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub enum SubCommands {
114114
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
115115
pub enum Color {
116116
Always,
117+
NoDiff,
117118
Never,
118119
Auto,
119120
}

src/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ fn parse_arguments() -> Result<Config> {
139139
Printer::silent()
140140
} else {
141141
match common.color {
142-
crate::cli::Color::Always => Printer::color(),
142+
crate::cli::Color::Always => Printer::color(true),
143+
crate::cli::Color::NoDiff => Printer::color(false),
143144
crate::cli::Color::Never => Printer::no_color(),
144145
crate::cli::Color::Auto => detect_output_color(),
145146
}
@@ -171,7 +172,7 @@ fn detect_output_color() -> Printer {
171172
if stdout.is_terminal() {
172173
#[cfg(not(windows))]
173174
{
174-
Printer::color()
175+
Printer::color(true)
175176
}
176177
// Enable color support for Windows 10
177178
#[cfg(windows)]

src/output.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::Path;
88
enum PrinterMode {
99
Silent,
1010
NoColor,
11-
Color,
11+
Color { diff: bool },
1212
}
1313

1414
pub struct Printer {
@@ -28,7 +28,7 @@ pub struct Colors {
2828

2929
impl Printer {
3030
/// Return a printer configured to colorize output
31-
pub fn color() -> Printer {
31+
pub fn color(diff: bool) -> Printer {
3232
let colors = Colors {
3333
info: Style::default().bold(),
3434
warn: Style::from(Yellow),
@@ -40,7 +40,7 @@ impl Printer {
4040

4141
Printer {
4242
colors,
43-
mode: PrinterMode::Color,
43+
mode: PrinterMode::Color { diff },
4444
}
4545
}
4646

@@ -81,7 +81,7 @@ impl Printer {
8181
/// Print string to Stdout when printer is not in silent mode
8282
pub fn print(&self, message: &str) {
8383
match self.mode {
84-
PrinterMode::Color | PrinterMode::NoColor => {
84+
PrinterMode::Color { diff: _ } | PrinterMode::NoColor => {
8585
println!("{}", message);
8686
}
8787
PrinterMode::Silent => {}
@@ -91,7 +91,7 @@ impl Printer {
9191
/// Print string to Stderr when printer is not in silent mode
9292
pub fn eprint(&self, message: &str) {
9393
match self.mode {
94-
PrinterMode::Color | PrinterMode::NoColor => {
94+
PrinterMode::Color { diff: _ } | PrinterMode::NoColor => {
9595
eprintln!("{}", message);
9696
}
9797
PrinterMode::Silent => {}
@@ -122,14 +122,20 @@ impl Printer {
122122
let mut target_parent = target.parent().unwrap().to_string_lossy().to_string();
123123
let mut target_name = target.file_name().unwrap().to_string_lossy().to_string();
124124

125-
// Avoid diffing if not coloring output
126-
if self.mode == PrinterMode::Color {
127-
target_name = self.string_diff(
128-
&source_name,
129-
&target_name,
130-
self.colors.target,
131-
self.colors.highlight,
132-
)
125+
// Avoid diffing if not coloring output or disabled
126+
match self.mode {
127+
PrinterMode::Color { diff: true } => {
128+
target_name = self.string_diff(
129+
&source_name,
130+
&target_name,
131+
self.colors.target,
132+
self.colors.highlight,
133+
)
134+
}
135+
PrinterMode::Color { diff: false } => {
136+
target_name = self.colors.target.paint(&target_name).to_string();
137+
}
138+
_ => {}
133139
}
134140

135141
source_name = self.colors.source.paint(&source_name).to_string();

src/renamer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ mod test {
260260
dump_prefix: "rnr-".to_string(),
261261
run_mode: RunMode::Simple(vec![]),
262262
replace_mode: ReplaceMode::None,
263-
printer: Printer::color(),
263+
printer: Printer::color(true),
264264
}
265265
}
266266
}

0 commit comments

Comments
 (0)