Skip to content

Commit

Permalink
feat: don't report error when cancelling risedev configure
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan committed Feb 2, 2023
1 parent e987106 commit ef5ae5e
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions src/risedevtool/src/bin/risedev-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use std::fs::OpenOptions;
use std::io::{BufRead, BufReader, BufWriter, Write};

use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use clap::{ArgEnum, Parser, Subcommand};
use console::style;
use dialoguer::MultiSelect;
Expand Down Expand Up @@ -209,28 +209,16 @@ Required if you want to create CDC source from external Databases.
}
}

fn configure(chosen: &[Components]) -> Result<Vec<Components>> {
fn configure(chosen: &[Components]) -> Result<Option<Vec<Components>>> {
println!("=== Configure RiseDev ===");
println!();
println!("RiseDev includes several components. You can select the ones you need, so as to reduce build time.");
println!();
println!(
"Use {} to navigate between up / down, use {} to go to next page,\nand use {} to select an item. Press {} to continue.",
style("arrow up / down").bold(),
style("arrow left / right").bold(),
style("space").bold(),
style("enter").bold()
);
println!();

let all_components = all::<Components>().collect_vec();

const ITEMS_PER_PAGE: usize = 6;

let items = all_components
.iter()
.enumerate()
.map(|(idx, c)| {
.map(|c| {
let title = c.title();
let desc = style(
("\n".to_string() + c.description().trim())
Expand All @@ -239,33 +227,31 @@ fn configure(chosen: &[Components]) -> Result<Vec<Components>> {
)
.dim();

let instruction = if (idx + 1) % ITEMS_PER_PAGE == 0 || idx == all_components.len() - 1
{
format!(
"\n\n page {}/{}",
style(((idx + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE).to_string()).bold(),
(all_components.len() + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE,
)
} else {
String::new()
};

(format!("{title}{desc}{instruction}",), chosen.contains(c))
(format!("{title}{desc}",), chosen.contains(c))
})
.collect_vec();

let chosen_indices: Vec<usize> = MultiSelect::new()
let Some(chosen_indices) = MultiSelect::new()
.with_prompt(
format!(
"RiseDev includes several components. You can select the ones you need, so as to reduce build time\n\n{}: navigate\n{}: confirm and save {}: quit without saving\n\nPick items with {}",
style("↑ / ↓ / ← / → ").reverse(),
style("Enter").reverse(),
style("Esc / q").reverse(),
style("Space").reverse(),
))
.items_checked(&items)
.max_length(ITEMS_PER_PAGE)
.interact_opt()?
.ok_or_else(|| anyhow!("no selection made"))?;
.interact_opt()? else {
return Ok(None);
};

let chosen = chosen_indices
.into_iter()
.map(|i| all_components[i])
.collect_vec();

Ok(chosen)
Ok(Some(chosen))
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -323,7 +309,14 @@ fn main() -> Result<()> {
Some(Commands::Disable { component }) => {
chosen.into_iter().filter(|x| x != component).collect()
}
None => configure(&chosen)?,
None => match configure(&chosen)? {
Some(chosen) => chosen,
None => {
println!("Quit without saving");
println!("=========================");
return Ok(());
}
},
};

println!("=== Enabled Components ===");
Expand Down

0 comments on commit ef5ae5e

Please sign in to comment.