aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/history.rs
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-02-13 19:37:00 +0000
committerEllie Huxtable <e@elm.sh>2021-02-13 19:37:31 +0000
commit099afe66ecfb569a8a04b66425ded29665e6a37c (patch)
tree7bb1baadb9304fa0d4f353d0849f962e2af209e3 /src/command/history.rs
parentRecord command exit code and duration (diff)
downloadatuin-099afe66ecfb569a8a04b66425ded29665e6a37c.zip
Implement history import
Diffstat (limited to 'src/command/history.rs')
-rw-r--r--src/command/history.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/command/history.rs b/src/command/history.rs
new file mode 100644
index 00000000..72f821c5
--- /dev/null
+++ b/src/command/history.rs
@@ -0,0 +1,63 @@
+use std::env;
+
+use eyre::Result;
+use structopt::StructOpt;
+
+use crate::local::database::{Database, SqliteDatabase};
+use crate::local::history::History;
+
+#[derive(StructOpt)]
+pub 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 {
+ pub 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(chrono::Utc::now().timestamp_nanos(), 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_nanos() - h.timestamp;
+
+ db.update(h)?;
+
+ Ok(())
+ }
+
+ HistoryCmd::List => db.list(),
+ }
+ }
+}