From f33128b3af36fc246e6e5266ec7802938470ed53 Mon Sep 17 00:00:00 2001 From: Marcelo Hernandez Lopez Date: Mon, 20 May 2024 19:51:52 -0400 Subject: [PATCH] new: --previous option Will attempt (hehe) to open last modified project --- src/cli.rs | 5 +++- src/lib.rs | 24 ++++++++--------- src/main.rs | 77 ++++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 32 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 743d9f9..7e8f87a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,7 +12,7 @@ pub struct Atmpt { pub required: RequiredArgs, #[arg(short, long, env = EDITOR_KEY, help = "Editor to use")] - pub editor: Option, + pub editor: String, #[arg( short = 'n', @@ -51,6 +51,9 @@ pub struct RequiredArgs { #[arg(group = "main", short, long, help = "Retry last template")] pub retry: bool, + + #[arg(group = "main", short, long, help = "Open last modified attempt")] + pub previous: bool, } #[derive(Debug, PartialEq, Eq)] diff --git a/src/lib.rs b/src/lib.rs index d43f564..2f6f2fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,6 +60,18 @@ pub fn try_template( Ok(()) } +pub fn summon_and_wait(editor: &str, cwd: &Path) -> anyhow::Result<()> { + Command::new(editor) + .current_dir(cwd) + .arg(".") + .spawn() + .context("Failed to launch editor!")? + .wait() + .context("Failed waiting for editor!")?; + + Ok(()) +} + fn should_keep(action: Option) -> anyhow::Result { match action { Some(action) => Ok(action == AfterAction::Keep), @@ -75,18 +87,6 @@ fn remove_attempt(tmp_dir: &Path) -> anyhow::Result<()> { Ok(()) } -fn summon_and_wait(editor: &str, cwd: &Path) -> anyhow::Result<()> { - Command::new(editor) - .current_dir(cwd) - .arg(".") - .spawn() - .context("Failed to launch editor!")? - .wait() - .context("Failed waiting for editor!")?; - - Ok(()) -} - /// Will print out a question and wait for user input in the form of `y` or `n` /// (returning true if `y`). Any capitalization works. Defaults to `n` on a /// blank character (e.g. just pressing Enter). diff --git a/src/main.rs b/src/main.rs index 892107e..19cf8f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ -use std::{borrow::Cow, fs::File, io::BufReader, path::PathBuf}; +use std::{ + borrow::Cow, + fs::{self, File}, + io::BufReader, + path::PathBuf, +}; use anyhow::{bail, Context}; -use atmpt::{get_session_path, session::Session, templates::Templates, Atmpt}; +use atmpt::{get_atmpt_dir, get_session_path, session::Session, templates::Templates, Atmpt}; use clap::Parser; use directories::ProjectDirs; @@ -18,27 +23,61 @@ fn main() -> anyhow::Result<()> { data_dir = Cow::Owned(PathBuf::from(new_dir)); }; - 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"); - }; + if args.required.list_template_dir { + println!("{}", data_dir.display()); + } else if args.required.list_templates { + println!("{}", Templates::try_from(data_dir.as_ref())?); + } else if args.required.previous { + atmpt::summon_and_wait(&args.editor, &last_modified_attempt()?)?; + } else { + let template = match args.required.template { + Some(t) => t, + // assume retry option + None => { + let file = File::open(get_session_path()) + .context("Could not open session file, have you run atmpt recently?")?; + let session: Session = serde_json::from_reader(BufReader::new(file)) + .context("Failed to read session file!")?; - atmpt::try_template(&template, &editor, &data_dir, action)?; - } else if args.required.retry { - let file = File::open(get_session_path()) - .context("Could not open session file, have you run atmpt recently?")?; - let session: Session = - serde_json::from_reader(BufReader::new(file)).context("Failed to read session file")?; - let Some(editor) = args.editor else { - bail!("No editor to use! Set your $VISUAL variable or pass a command to --editor"); + session.last_template + } }; - atmpt::try_template(&session.last_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())?); + atmpt::try_template(&template, &args.editor, &data_dir, action)?; } Ok(()) } + +fn last_modified_attempt() -> anyhow::Result { + let atmpt_dir = get_atmpt_dir(); + if !atmpt_dir.exists() { + bail!("Could not find atmpt folder, have you run atmpt recently?"); + } + + let entries = fs::read_dir(atmpt_dir)?; + let mut last = None; + for entry in entries { + let entry = entry?; + + let metadata = entry.metadata()?; + if !metadata.is_dir() { + continue; + } + + let new_time = metadata.modified()?; + if let Some((_, time)) = last { + if new_time <= time { + continue; + } + } + + last = Some((entry.path(), new_time)); + } + + if let Some((path, _)) = last { + Ok(path) + } else { + bail!("Could not find last modified attempt, have you saved any?") + } +}