2020-10-04 17:59:28 -06:00
|
|
|
use std::env;
|
2020-10-05 10:20:48 -06:00
|
|
|
use std::path::PathBuf;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
use directories::ProjectDirs;
|
|
|
|
use eyre::{eyre, Result};
|
2020-10-05 04:52:03 -06:00
|
|
|
use structopt::StructOpt;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2020-10-04 17:59:28 -06:00
|
|
|
use pretty_env_logger;
|
|
|
|
|
|
|
|
mod local;
|
|
|
|
|
|
|
|
use local::database::{Database, SqliteDatabase};
|
2020-10-05 10:20:48 -06:00
|
|
|
use local::history::History;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 04:52:03 -06:00
|
|
|
#[derive(StructOpt)]
|
|
|
|
#[structopt(
|
2020-10-05 10:20:48 -06:00
|
|
|
author = "Ellie Huxtable <e@elm.sh>",
|
|
|
|
version = "0.1.0",
|
|
|
|
about = "Keep your shell history in sync"
|
2020-10-05 04:52:03 -06:00
|
|
|
)]
|
2021-02-13 05:58:40 -07:00
|
|
|
struct Atuin {
|
2020-10-05 10:34:28 -06:00
|
|
|
#[structopt(long, parse(from_os_str), help = "db file path")]
|
2020-10-05 10:20:48 -06:00
|
|
|
db: Option<PathBuf>,
|
|
|
|
|
|
|
|
#[structopt(subcommand)]
|
2021-02-13 05:58:40 -07:00
|
|
|
atuin: AtuinCmd,
|
2020-10-05 10:20:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt)]
|
2021-02-13 05:58:40 -07:00
|
|
|
enum AtuinCmd {
|
2020-10-05 04:52:03 -06:00
|
|
|
#[structopt(
|
|
|
|
about="manipulate shell history",
|
|
|
|
aliases=&["h", "hi", "his", "hist", "histo", "histor"],
|
|
|
|
)]
|
|
|
|
History(HistoryCmd),
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
#[structopt(about = "import shell history from file")]
|
2020-10-05 04:52:03 -06:00
|
|
|
Import,
|
|
|
|
|
2021-02-13 05:58:40 -07:00
|
|
|
#[structopt(about = "start a atuin server")]
|
2020-10-05 04:52:03 -06:00
|
|
|
Server,
|
|
|
|
}
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2021-02-13 05:58:40 -07:00
|
|
|
impl Atuin {
|
2020-10-05 10:20:48 -06:00
|
|
|
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 => {
|
2021-02-13 05:58:40 -07:00
|
|
|
let project_dirs = ProjectDirs::from("com", "elliehuxtable", "atuin").ok_or(
|
|
|
|
eyre!("could not determine db file location\nspecify one using the --db flag"),
|
|
|
|
)?;
|
2020-10-05 10:20:48 -06:00
|
|
|
let root = project_dirs.data_dir();
|
|
|
|
root.join("history.db")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let db = SqliteDatabase::new(db_path)?;
|
|
|
|
|
2021-02-13 05:58:40 -07:00
|
|
|
match self.atuin {
|
|
|
|
AtuinCmd::History(history) => history.run(db),
|
2020-10-05 10:20:48 -06:00
|
|
|
_ => Ok(()),
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
2020-10-05 04:52:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(StructOpt)]
|
|
|
|
enum HistoryCmd {
|
|
|
|
#[structopt(
|
2021-02-13 10:02:52 -07:00
|
|
|
about="begins a new command in the history",
|
|
|
|
aliases=&["s", "st", "sta", "star"],
|
2020-10-05 04:52:03 -06:00
|
|
|
)]
|
2021-02-13 10:02:52 -07:00
|
|
|
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,
|
|
|
|
},
|
2020-10-05 04:52:03 -06:00
|
|
|
|
|
|
|
#[structopt(
|
|
|
|
about="list all items in history",
|
|
|
|
aliases=&["l", "li", "lis"],
|
|
|
|
)]
|
|
|
|
List,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HistoryCmd {
|
2021-02-13 10:02:52 -07:00
|
|
|
fn run(&self, db: SqliteDatabase) -> Result<()> {
|
2020-10-05 04:52:03 -06:00
|
|
|
match self {
|
2021-02-13 10:02:52 -07:00
|
|
|
HistoryCmd::Start { command: words } => {
|
2020-10-05 04:52:03 -06:00
|
|
|
let command = words.join(" ");
|
2020-10-05 10:20:48 -06:00
|
|
|
let cwd = env::current_dir()?.display().to_string();
|
2020-10-05 04:52:03 -06:00
|
|
|
|
2021-02-13 10:02:52 -07:00
|
|
|
let h = History::new(command, cwd, -1, -1);
|
|
|
|
|
|
|
|
// print the ID
|
|
|
|
// we use this as the key for calling end
|
|
|
|
println!("{}", h.id);
|
2020-10-05 04:52:03 -06:00
|
|
|
db.save(h)?;
|
2021-02-13 10:02:52 -07:00
|
|
|
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)?;
|
|
|
|
|
2020-10-05 04:52:03 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
HistoryCmd::List => db.list(),
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 04:52:03 -06:00
|
|
|
}
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 04:52:03 -06:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
pretty_env_logger::init();
|
|
|
|
|
2021-02-13 05:58:40 -07:00
|
|
|
Atuin::from_args().run()
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|