summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 57688a4a3b77ec986e7372aa73c3725c52b29d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::env;
use std::path::PathBuf;

use directories::ProjectDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;

#[macro_use]
extern crate log;
use pretty_env_logger;

mod local;

use local::database::{Database, SqliteDatabase};
use local::history::History;

#[derive(StructOpt)]
#[structopt(
    author = "Ellie Huxtable <e@elm.sh>",
    version = "0.1.0",
    about = "Keep your shell history in sync"
)]
struct Atuin {
    #[structopt(long, parse(from_os_str), help = "db file path")]
    db: Option<PathBuf>,

    #[structopt(subcommand)]
    atuin: AtuinCmd,
}

#[derive(StructOpt)]
enum AtuinCmd {
    #[structopt(
        about="manipulate shell history",
        aliases=&["h", "hi", "his", "hist", "histo", "histor"],
    )]
    History(HistoryCmd),

    #[structopt(about = "import shell history from file")]
    Import,

    #[structopt(about = "start a atuin server")]
    Server,
}

impl Atuin {
    fn run(self) -> Result<()> {
        let db_path = match self.db {
            Some(db_path) => {
                let path = db_path
                    .to_str()
                    .ok_or(eyre!("path {:?} was not valid UTF-8", db_path))?;
                let path = shellexpand::full(path)?;
                PathBuf::from(path.as_ref())
            }
            None => {
                let project_dirs = ProjectDirs::from("com", "elliehuxtable", "atuin").ok_or(
                    eyre!("could not determine db file location\nspecify one using the --db flag"),
                )?;
                let root = project_dirs.data_dir();
                root.join("history.db")
            }
        };

        let db = SqliteDatabase::new(db_path)?;

        match self.atuin {
            AtuinCmd::History(history) => history.run(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();

    Atuin::from_args().run()
}