From 099afe66ecfb569a8a04b66425ded29665e6a37c Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sat, 13 Feb 2021 19:37:00 +0000 Subject: Implement history import --- src/main.rs | 68 +++++++------------------------------------------------------ 1 file changed, 7 insertions(+), 61 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 57688a4a..19357cbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::env; use std::path::PathBuf; use directories::ProjectDirs; @@ -9,11 +8,13 @@ use structopt::StructOpt; extern crate log; use pretty_env_logger; -mod local; - +use command::{history::HistoryCmd, import::ImportCmd}; use local::database::{Database, SqliteDatabase}; use local::history::History; +mod command; +mod local; + #[derive(StructOpt)] #[structopt( author = "Ellie Huxtable ", @@ -37,7 +38,7 @@ enum AtuinCmd { History(HistoryCmd), #[structopt(about = "import shell history from file")] - Import, + Import(ImportCmd), #[structopt(about = "start a atuin server")] Server, @@ -62,71 +63,16 @@ impl Atuin { } }; - let db = SqliteDatabase::new(db_path)?; + let mut db = SqliteDatabase::new(db_path)?; match self.atuin { AtuinCmd::History(history) => history.run(db), + AtuinCmd::Import(import) => import.run(&mut db), _ => Ok(()), } } } -#[derive(StructOpt)] -enum HistoryCmd { - #[structopt( - about="begins a new command in the history", - aliases=&["s", "st", "sta", "star"], - )] - Start { command: Vec }, - - #[structopt( - about="finishes a new command in the history (adds time, exit code)", - aliases=&["e", "en"], - )] - End { - id: String, - #[structopt(long, short)] - exit: i64, - }, - - #[structopt( - about="list all items in history", - aliases=&["l", "li", "lis"], - )] - List, -} - -impl HistoryCmd { - fn run(&self, db: SqliteDatabase) -> Result<()> { - match self { - HistoryCmd::Start { command: words } => { - let command = words.join(" "); - let cwd = env::current_dir()?.display().to_string(); - - let h = History::new(command, cwd, -1, -1); - - // print the ID - // we use this as the key for calling end - println!("{}", h.id); - db.save(h)?; - Ok(()) - } - - HistoryCmd::End { id, exit } => { - let mut h = db.load(id)?; - h.exit = *exit; - h.duration = chrono::Utc::now().timestamp_millis() - h.timestamp; - - db.update(h)?; - - Ok(()) - } - - HistoryCmd::List => db.list(), - } - } -} - fn main() -> Result<()> { pretty_env_logger::init(); -- cgit v1.3.1