summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 769c3235d964a78d5f30135e8ab40dbef72c2b66 (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
use std::env;

use structopt::StructOpt;
use eyre::Result;

#[macro_use] extern crate log;
use pretty_env_logger;

mod local;

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

#[derive(StructOpt)]
#[structopt(
    author="Ellie Huxtable <e@elm.sh>",
    version="0.1.0",
    about="Keep your shell history in sync"
)]
enum Shync {
    #[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 shync server",
    )]
    Server,
}

impl Shync {
    fn run(self, db: SqliteDatabase) -> Result<()> {
        match self {
            Shync::History(history) => history.run(db),
            _ => Ok(())
        }
    }
}

#[derive(StructOpt)]
enum HistoryCmd {
    #[structopt(
        about="add a new command to the history",
        aliases=&["a", "ad"],
    )]
    Add {
        command: Vec<String>,
    },

    #[structopt(
        about="list all items in history",
        aliases=&["l", "li", "lis"],
    )]
    List,
}

impl HistoryCmd {
    fn run(self, db: SqliteDatabase) -> Result<()> {
        match self {
            HistoryCmd::Add{command: words} => {
                let command = words.join(" ");

                let cwd = env::current_dir()?;
                let h = History::new(
                    command.as_str(),
                    cwd.display().to_string().as_str(),
                );

                debug!("adding history: {:?}", h);
                db.save(h)?;
                debug!("saved history to sqlite");
                Ok(())
            }

            HistoryCmd::List => db.list()
        }
    }
}

fn main() -> Result<()> {
    pretty_env_logger::init();

    let db = SqliteDatabase::new("~/.history.db")?;
    Shync::from_args().run(db)
}