diff --git a/crates/cli/src/print/colored_print.rs b/crates/cli/src/print/colored_print.rs index b0b4545994..8a4b6850af 100644 --- a/crates/cli/src/print/colored_print.rs +++ b/crates/cli/src/print/colored_print.rs @@ -57,7 +57,8 @@ pub struct ColoredPrinter { } impl ColoredPrinter { - pub fn color(color: ColorChoice) -> Self { + pub fn color>(color: C) -> Self { + let color = color.into(); Self { writer: StandardStream::stdout(color), styles: PrintStyles::from(color), diff --git a/crates/cli/src/print/interactive_print.rs b/crates/cli/src/print/interactive_print.rs index 286dd11289..2da85847d9 100644 --- a/crates/cli/src/print/interactive_print.rs +++ b/crates/cli/src/print/interactive_print.rs @@ -29,12 +29,16 @@ pub struct InteractivePrinter { inner: ColoredPrinter, } impl InteractivePrinter { - pub fn new(accept_all: bool) -> Self { + pub fn new(inner: ColoredPrinter) -> Self { Self { accept_all: false, - inner: ColoredPrinter::color(codespan_reporting::term::termcolor::ColorChoice::Auto), + inner, } } + + pub fn accept_all(self, accept_all: bool) -> Self { + Self { accept_all, ..self } + } } impl Printer for InteractivePrinter { diff --git a/crates/cli/src/scan.rs b/crates/cli/src/scan.rs index 3c603c2728..7f4565abd7 100644 --- a/crates/cli/src/scan.rs +++ b/crates/cli/src/scan.rs @@ -107,17 +107,19 @@ pub struct ScanArg { // Every run will include Search or Replace // Search or Replace by arguments `pattern` and `rewrite` passed from CLI pub fn run_with_pattern(arg: RunArg) -> Result<()> { + if arg.json { + return run_pattern_with_printer(arg, JSONPrinter::new()); + } + let printer = ColoredPrinter::color(arg.color).heading(arg.heading); let interactive = arg.interactive || arg.accept_all; if interactive { - let printer = InteractivePrinter::new(arg.accept_all); + let printer = InteractivePrinter::new(printer).accept_all(arg.accept_all); run_pattern_with_printer(arg, printer) - } else if arg.json { - run_pattern_with_printer(arg, JSONPrinter::new()) } else { - let printer = ColoredPrinter::color(arg.color.into()).heading(arg.heading); run_pattern_with_printer(arg, printer) } } + fn run_pattern_with_printer(arg: RunArg, printer: impl Printer + Sync) -> Result<()> { if arg.lang.is_some() { run_worker(RunWithSpecificLang { arg, printer }) @@ -222,16 +224,17 @@ impl Worker for RunWithSpecificLang

{ } pub fn run_with_config(arg: ScanArg) -> Result<()> { + if arg.json { + let worker = ScanWithConfig::try_new(arg, JSONPrinter::new())?; + return run_worker(worker); + } + let printer = ColoredPrinter::color(arg.color).style(arg.report_style); let interactive = arg.interactive || arg.accept_all; if interactive { - let printer = InteractivePrinter::new(arg.accept_all); + let printer = InteractivePrinter::new(printer).accept_all(arg.accept_all); let worker = ScanWithConfig::try_new(arg, printer)?; run_worker(worker) - } else if arg.json { - let worker = ScanWithConfig::try_new(arg, JSONPrinter::new())?; - run_worker(worker) } else { - let printer = ColoredPrinter::color(arg.color.into()).style(arg.report_style); let worker = ScanWithConfig::try_new(arg, printer)?; run_worker(worker) }