Skip to content

Commit

Permalink
feat: move interactive printer
Browse files Browse the repository at this point in the history
fix #135
  • Loading branch information
HerringtonDarkholme committed Dec 31, 2022
1 parent 5670bb6 commit 33ca593
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
3 changes: 2 additions & 1 deletion crates/cli/src/print/colored_print.rs
Expand Up @@ -57,7 +57,8 @@ pub struct ColoredPrinter {
}

impl ColoredPrinter {
pub fn color(color: ColorChoice) -> Self {
pub fn color<C: Into<ColorChoice>>(color: C) -> Self {
let color = color.into();
Self {
writer: StandardStream::stdout(color),
styles: PrintStyles::from(color),
Expand Down
8 changes: 6 additions & 2 deletions crates/cli/src/print/interactive_print.rs
Expand Up @@ -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 {
Expand Down
21 changes: 12 additions & 9 deletions crates/cli/src/scan.rs
Expand Up @@ -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 })
Expand Down Expand Up @@ -222,16 +224,17 @@ impl<P: Printer + Sync> Worker for RunWithSpecificLang<P> {
}

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)
}
Expand Down

0 comments on commit 33ca593

Please sign in to comment.