Skip to content

Commit

Permalink
feat: add confirmation prompt before saving a configuration file that…
Browse files Browse the repository at this point in the history
… has interactive mode enabled + add `--yes` to skip prompts
  • Loading branch information
cestef committed Jul 14, 2024
1 parent 44804d9 commit 9525600
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions braise.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#:schema https://raw.githubusercontent.com/cestef/braise/main/schema/braise.schema.json
quiet = 1
default = "run"

[run]
command = "cargo run --quiet --"
Expand Down
17 changes: 15 additions & 2 deletions src/cli/interactive/commands/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Command for SaveCommand {
_engine: Arc<Mutex<Engine>>,
_scope: Arc<Mutex<Scope<'_>>>,
) -> Result<()> {
let state = state.lock().await;
let mut state = state.lock().await;
let output = if args.len() == 1 {
PathBuf::from(args[0])
} else if let Some(home) = dirs::home_dir() {
Expand All @@ -46,9 +46,22 @@ impl Command for SaveCommand {
return Ok(());
};
debug!("Saving configuration to {}", output.to_string_lossy());
if state.opts.interactive && !state.opts.yes {
let mut rl = rl.lock().await;
let response = rl.readline(&format!(
"{}: Interactive mode is set to {}. {}. Keep this setting? [y/N]: ",
"Warning".yellow().bold(),
"true".bold(),
"This will make rwalk open in interactive mode by default".underline(),
))?;
const YES: [&str; 2] = ["y", "yes"];
if !YES.contains(&response.trim().to_lowercase().as_str()) {
state.opts.interactive = false;
}
}
let content = toml::to_string_pretty(&state.opts)?;
// If the file already exists, prompt the user to confirm overwriting it
if output.exists() {
if output.exists() && !state.opts.yes {
let mut rl = rl.lock().await;
let response = rl.readline(&format!(
"File {} already exists. Overwrite? [y/N]: ",
Expand Down
6 changes: 6 additions & 0 deletions src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ pub struct Opts {
#[merge(strategy = merge::bool::overwrite_false)]
#[serde(default)]
pub capture: bool,

/// Skip all confirmation prompts
#[clap(short, long, help_heading = Some("Interactive"), env, hide_env=true)]
#[merge(strategy = merge::bool::overwrite_false)]
#[serde(default)]
pub yes: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ pub async fn _main(opts: Opts) -> Result<Tree<TreeData>> {
// Wait for the abort signal if aborted has been set
if aborted.load(Ordering::Relaxed) {
while rx.try_recv().is_err() {
// Sleep for a short time to avoid busy waiting
// Sleep for a short time to avoid busy waiting and hogging the CPU
tokio::time::sleep(Duration::from_millis(100)).await;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub fn init_logger() {
"hyper_util::client::legacy::connect::dns",
log::LevelFilter::Warn,
)
.filter_module("rustyline::keymap", log::LevelFilter::Warn)
.filter_module("rustyline::undo", log::LevelFilter::Warn)
.filter_module("rustyline::edit", log::LevelFilter::Warn)
.filter_module("rustyline::tty::unix", log::LevelFilter::Warn)
.filter_module("rustyline::tty::unix::termios_", log::LevelFilter::Warn)
.format(|buf, record| {
let mut style = buf.style();
match record.level() {
Expand Down

0 comments on commit 9525600

Please sign in to comment.