2020-03-13 00:49:37 +00:00
|
|
|
use crate::util;
|
2020-03-27 11:41:26 +00:00
|
|
|
|
2020-03-13 00:49:37 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
2020-03-28 18:21:46 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
2020-03-13 00:49:37 +00:00
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
|
#[structopt(about = "Remove a directory")]
|
|
|
|
|
pub struct Remove {
|
2020-04-06 03:11:23 +00:00
|
|
|
#[structopt(required_unless("interactive"))]
|
|
|
|
|
path: Option<PathBuf>,
|
|
|
|
|
#[structopt(short, long, help = "Opens an interactive selection menu using fzf")]
|
|
|
|
|
interactive: bool,
|
2020-03-13 00:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Remove {
|
2020-03-27 19:08:36 +00:00
|
|
|
pub fn run(&self) -> Result<()> {
|
2020-04-06 03:11:23 +00:00
|
|
|
if self.interactive {
|
|
|
|
|
let mut db = util::get_db()?;
|
|
|
|
|
let dirs = db.query_all();
|
|
|
|
|
let now = util::get_current_time()?;
|
|
|
|
|
|
|
|
|
|
if let Some(path_bytes) = util::fzf_helper(now, dirs)? {
|
|
|
|
|
let path = util::bytes_to_path(&path_bytes)?;
|
|
|
|
|
db.remove_exact(path)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
// structopt guarantees that unwrap is safe here
|
|
|
|
|
let path = self.path.as_ref().unwrap();
|
|
|
|
|
util::get_db()?.remove(path)
|
|
|
|
|
}
|
2020-03-13 00:49:37 +00:00
|
|
|
}
|
|
|
|
|
}
|