Skip to content

Commit

Permalink
new: --previous option
Browse files Browse the repository at this point in the history
Will attempt (hehe) to open last modified project
  • Loading branch information
marcelohdez committed May 20, 2024
1 parent 4f692d0 commit f33128b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Atmpt {
pub required: RequiredArgs,

#[arg(short, long, env = EDITOR_KEY, help = "Editor to use")]
pub editor: Option<String>,
pub editor: String,

#[arg(
short = 'n',
Expand Down Expand Up @@ -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)]
Expand Down
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AfterAction>) -> anyhow::Result<bool> {
match action {
Some(action) => Ok(action == AfterAction::Keep),
Expand All @@ -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).
Expand Down
77 changes: 58 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<PathBuf> {
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?")
}
}

0 comments on commit f33128b

Please sign in to comment.