Skip to content

Commit

Permalink
new: --keep/-y option
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelohdez committed Feb 21, 2024
1 parent 350aaa5 commit 22fba0a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
39 changes: 38 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;

pub const EDITOR_KEY: &str = "VISUAL";
pub const ALWAYS_DELETE_KEY: &str = "ATMPT_ALWAYS_DELETE";
pub const ALWAYS_KEEP_KEY: &str = "ATMPT_ALWAYS_KEEP";
pub const DATA_DIR_KEY: &str = "ATMPT_DATA_DIR";

#[derive(Debug, Parser)]
Expand All @@ -13,9 +14,25 @@ pub struct Atmpt {
#[arg(short, long, env = EDITOR_KEY, help = "Editor to use")]
pub editor: Option<String>,

#[arg(short = 'n', long, hide_env = true, env = ALWAYS_DELETE_KEY, help = "Delete project on exit")]
#[arg(
short = 'n',
long,
hide_env = true,
conflicts_with = "keep",
env = ALWAYS_DELETE_KEY,
help = "Delete project on exit"
)]
pub delete: bool,

#[arg(
short = 'y',
long,
hide_env = true,
env = ALWAYS_KEEP_KEY,
help = "Keep project on exit"
)]
pub keep: bool,

#[arg(long, hide_env = true, env = DATA_DIR_KEY, help = "Override data directory")]
pub data_dir: Option<String>,
}
Expand All @@ -32,3 +49,23 @@ pub struct RequiredArgs {
#[arg(group = "main", short, long, help = "List available templates")]
pub list_templates: bool,
}

#[derive(Debug, PartialEq, Eq)]
pub enum AfterAction {
Keep,
Delete,
}

impl Atmpt {
pub fn after_action(&self) -> Option<AfterAction> {
if self.keep {
return Some(AfterAction::Keep);
}

if self.delete {
return Some(AfterAction::Delete);
}

None
}
}
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use chrono::Local;
pub fn try_template(
template: &str,
editor: &str,
delete: bool,
data_dir: &Path,
action: Option<AfterAction>,
) -> anyhow::Result<()> {
let templates = Templates::try_from(data_dir)?;
let wanted_dir = templates.find(template)?;
Expand All @@ -26,7 +26,7 @@ pub fn try_template(

let res = summon_and_wait(editor, &tmp_dir);

if res.is_ok() && !delete && ask_y_n("Would you like to keep this project?")? {
if res.is_ok() && should_keep(action)? {
println!("Saved as {tmp_dir:?}.");
} else {
fs::remove_dir_all(&tmp_dir)
Expand All @@ -42,6 +42,13 @@ pub fn try_template(
Ok(())
}

fn should_keep(action: Option<AfterAction>) -> anyhow::Result<bool> {
match action {
Some(action) => Ok(action == AfterAction::Keep),
None => ask_y_n("Would you like to keep this code?"),
}
}

fn summon_and_wait(editor: &str, cwd: &Path) -> anyhow::Result<()> {
Command::new(editor)
.current_dir(cwd)
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ fn main() -> anyhow::Result<()> {
};

let args = Atmpt::parse();
let req = args.required;
let action = args.after_action();

let mut data_dir = Cow::Borrowed(dirs.data_dir());
if let Some(new_dir) = args.data_dir {
if let Some(new_dir) = &args.data_dir {
data_dir = Cow::Owned(PathBuf::from(new_dir));
};

if let Some(template) = req.template {
if let Some(template) = args.required.template {
let Some(editor) = args.editor else {
bail!("No editor to use! Set your $VISUAL variable or pass a command to --editor");
};

atmpt::try_template(&template, &editor, args.delete, &data_dir)?;
} else if req.list_template_dir {
atmpt::try_template(&template, &editor, &data_dir, action)?;
} else if args.required.list_template_dir {
println!("{}", data_dir.display());
} else {
println!("{}", Templates::try_from(data_dir.as_ref())?);
Expand Down
7 changes: 6 additions & 1 deletion tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ fn fail_on_conflicting_opts() {

#[test]
fn fail_on_conflicting_opts_with_template() {
cmd().args(["c", "-l", "-d"]).assert().failure();
cmd().args(["c", "-ld"]).assert().failure();
}

#[test]
fn fail_on_keep_and_delete() {
cmd().args(["python", "-ny"]).assert().failure();
}

#[test]
Expand Down

0 comments on commit 22fba0a

Please sign in to comment.