2021-02-13 13:21:49 -07:00
|
|
|
use std::env;
|
2021-03-19 18:50:31 -06:00
|
|
|
use std::hash::{Hash, Hasher};
|
2021-02-13 13:21:49 -07:00
|
|
|
|
2021-02-14 11:40:51 -07:00
|
|
|
use crate::command::uuid_v4;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2021-03-19 18:50:31 -06:00
|
|
|
#[derive(Debug, Clone)]
|
2020-10-04 17:59:28 -06:00
|
|
|
pub struct History {
|
2021-02-13 10:02:52 -07:00
|
|
|
pub id: String,
|
2020-10-04 17:59:28 -06:00
|
|
|
pub timestamp: i64,
|
2021-02-13 10:02:52 -07:00
|
|
|
pub duration: i64,
|
|
|
|
pub exit: i64,
|
2020-10-04 17:59:28 -06:00
|
|
|
pub command: String,
|
|
|
|
pub cwd: String,
|
2021-02-13 13:21:49 -07:00
|
|
|
pub session: String,
|
|
|
|
pub hostname: String,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl History {
|
2021-02-13 13:21:49 -07:00
|
|
|
pub fn new(
|
|
|
|
timestamp: i64,
|
|
|
|
command: String,
|
|
|
|
cwd: String,
|
|
|
|
exit: i64,
|
|
|
|
duration: i64,
|
|
|
|
session: Option<String>,
|
|
|
|
hostname: Option<String>,
|
2021-02-14 08:15:26 -07:00
|
|
|
) -> Self {
|
2021-02-14 11:00:41 -07:00
|
|
|
let session = session
|
|
|
|
.or_else(|| env::var("ATUIN_SESSION").ok())
|
2021-02-14 11:40:51 -07:00
|
|
|
.unwrap_or_else(uuid_v4);
|
2021-02-14 10:18:02 -07:00
|
|
|
let hostname =
|
|
|
|
hostname.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
|
2021-02-13 13:21:49 -07:00
|
|
|
|
2021-02-14 08:15:26 -07:00
|
|
|
Self {
|
2021-02-14 11:40:51 -07:00
|
|
|
id: uuid_v4(),
|
2021-02-13 12:37:00 -07:00
|
|
|
timestamp,
|
2020-10-05 10:20:48 -06:00
|
|
|
command,
|
|
|
|
cwd,
|
2021-02-13 10:02:52 -07:00
|
|
|
exit,
|
|
|
|
duration,
|
2021-02-13 13:21:49 -07:00
|
|
|
session,
|
|
|
|
hostname,
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-19 18:50:31 -06:00
|
|
|
|
|
|
|
impl PartialEq for History {
|
|
|
|
// for the sakes of listing unique history only, we do not care about
|
|
|
|
// anything else
|
|
|
|
// obviously this does not refer to the *same* item of history, but when
|
|
|
|
// we only render the command, it looks the same
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.command == other.command
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for History {}
|
|
|
|
|
|
|
|
impl Hash for History {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.command.hash(state);
|
|
|
|
}
|
|
|
|
}
|