Skip to content

Commit

Permalink
fix: previous attempt would be saved even when not keeping attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelohdez committed May 24, 2024
1 parent 04b3889 commit e582f25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ pub fn try_template(
bail!(e);
}

if should_keep(action)? {
// wrap project dir in Option<PathBuf> to save to session file
let project_dir = if should_keep(action)? {
println!("Saved as {project_dir:?}.");
Some(project_dir)
} else {
remove_attempt(&project_dir)?;
}
None
};

// save session data to file
let file = File::create(get_session_path(tmp_dir))?;
Expand Down
20 changes: 12 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ fn main() -> anyhow::Result<()> {
} else if args.required.list_templates {
println!("{}", Templates::try_from(data_dir.as_ref())?);
} else if args.required.previous {
let session = Session::from_file(&get_session_path(&tmp_dir))?;
if !session.previous_attempt.exists() {
bail!(
"Last modified attempt, {:?} does not exist! Did it move?\nMake a new attempt!",
session.previous_attempt
);
match Session::from_file(&get_session_path(&tmp_dir))?.previous_attempt {
Some(path) => {
if !path.exists() {
bail!(
"Last saved attempt, {:?} does not exist! Did it move?",
path
);
}

atmpt::summon_and_wait(&args.editor, &path)?;
}
None => bail!("No previous attempt found in session file!"),
}

atmpt::summon_and_wait(&args.editor, &session.previous_attempt)?;
} else {
let template = match args.required.template {
Some(t) => t,
Expand Down
2 changes: 1 addition & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Session {
pub last_template: String,
pub previous_attempt: PathBuf,
pub previous_attempt: Option<PathBuf>,
}

impl Session {
Expand Down

0 comments on commit e582f25

Please sign in to comment.