Skip to content

Commit

Permalink
feat: interactive print one item a time
Browse files Browse the repository at this point in the history
BREAKING CHANGE: sg run -p xx -r yy will print only one item
instead of all. fix #444 #580
  • Loading branch information
HerringtonDarkholme committed Sep 8, 2023
1 parent a9551fb commit 09af2b8
Showing 1 changed file with 22 additions and 39 deletions.
61 changes: 22 additions & 39 deletions crates/cli/src/print/interactive_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ impl<P: Printer> InteractivePrinter<P> {
}

fn rewrite_action(&self, diffs: Vec<Diff<'_>>, path: &PathBuf) -> Result<()> {
if diffs.is_empty() {
return Ok(());
}
let new_content = apply_rewrite(diffs);
if self.from_stdin {
println!("{new_content}");
Expand Down Expand Up @@ -99,13 +102,20 @@ impl<P: Printer> Printer for InteractivePrinter<P> {
if self.accept_all.load(Ordering::SeqCst) {
return self.rewrite_action(diffs.collect(), &path);
}
utils::run_in_alternate_screen(|| {
let all = print_diffs_and_prompt_action(self, &path, diffs.collect())?;
let mut confirmed = vec![];
let mut all = false;
for diff in diffs {
if all {
self.accept_all.store(true, Ordering::SeqCst);
confirmed.push(diff);
continue;
}
Ok(())
})
all = print_rule_diff_and_prompt_action(self, &path, (diff, None), &mut confirmed)?;
}
self.rewrite_action(confirmed, &path)?;
if all {
self.accept_all.store(true, Ordering::SeqCst);
}
Ok(())
}
fn print_rule_diffs(
&self,
Expand All @@ -123,7 +133,7 @@ impl<P: Printer> Printer for InteractivePrinter<P> {
confirmed.push(diff);
continue;
}
all = print_rule_diff_and_prompt_action(self, &path, (diff, rule), &mut confirmed)?;
all = print_rule_diff_and_prompt_action(self, &path, (diff, Some(rule)), &mut confirmed)?;
}
self.rewrite_action(confirmed, &path)?;
if all {
Expand All @@ -136,12 +146,16 @@ impl<P: Printer> Printer for InteractivePrinter<P> {
fn print_rule_diff_and_prompt_action<'a>(
interactive: &InteractivePrinter<impl Printer>,
path: &PathBuf,
(diff, rule): (Diff<'a>, &RuleConfig<SgLang>),
(diff, rule): (Diff<'a>, Option<&RuleConfig<SgLang>>),
confirmed: &mut Vec<Diff<'a>>,
) -> Result<bool> {
let printer = &interactive.inner;
utils::run_in_alternate_screen(|| {
printer.print_rule_diffs(vec![(diff.clone(), rule)], path)?;
if let Some(rule) = rule {
printer.print_rule_diffs(vec![(diff.clone(), rule)], path)?;
} else {
printer.print_diffs(std::iter::once(diff.clone()), path)?;
}
match interactive.prompt_edit() {
'y' => {
confirmed.push(diff);
Expand All @@ -163,37 +177,6 @@ fn print_rule_diff_and_prompt_action<'a>(
})
}

/// returns if accept_all is chosen
fn print_diffs_and_prompt_action(
interactive: &InteractivePrinter<impl Printer>,
path: &PathBuf,
diffs: Vec<Diff<'_>>,
) -> Result<bool> {
let printer = &interactive.inner;
let first_match = match diffs.first() {
Some(n) => n.node_match.start_pos().0,
None => return Ok(false),
};
printer.print_diffs(diffs.clone().into_iter(), path)?;
match interactive.prompt_edit() {
'y' => {
interactive.rewrite_action(diffs, path)?;
Ok(false)
}
'a' => {
interactive.rewrite_action(diffs, path)?;
Ok(true)
}
'n' => Ok(false),
'e' => {
open_in_editor(path, first_match)?;
Ok(false)
}
'q' => Err(anyhow::anyhow!("Exit interactive editing")),
_ => Ok(false),
}
}

fn print_matches_and_confirm_next<'a>(
interactive: &InteractivePrinter<impl Printer>,
matches: Matches!('a),
Expand Down

0 comments on commit 09af2b8

Please sign in to comment.