diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-02-13 19:37:00 +0000 |
|---|---|---|
| committer | Ellie Huxtable <e@elm.sh> | 2021-02-13 19:37:31 +0000 |
| commit | 099afe66ecfb569a8a04b66425ded29665e6a37c (patch) | |
| tree | 7bb1baadb9304fa0d4f353d0849f962e2af209e3 /src/main.rs | |
| parent | Record command exit code and duration (diff) | |
| download | atuin-099afe66ecfb569a8a04b66425ded29665e6a37c.zip | |
Implement history import
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 68 |
1 files changed, 7 insertions, 61 deletions
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 <e@elm.sh>", @@ -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<String> }, - - #[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(); |
