atuin/atuin-client/src/history.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2021-02-13 13:21:49 -07:00
use std::env;
use chrono::Utc;
use atuin_common::utils::uuid_v4;
2022-04-22 12:24:38 -06:00
use serde::{Deserialize, Serialize};
// Any new fields MUST be Optional<>!
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, sqlx::FromRow)]
pub struct History {
2021-02-13 10:02:52 -07:00
pub id: String,
pub timestamp: chrono::DateTime<Utc>,
2021-02-13 10:02:52 -07:00
pub duration: i64,
pub exit: i64,
pub command: String,
pub cwd: String,
2021-02-13 13:21:49 -07:00
pub session: String,
pub hostname: String,
}
impl History {
2021-02-13 13:21:49 -07:00
pub fn new(
timestamp: chrono::DateTime<Utc>,
2021-02-13 13:21:49 -07:00
command: String,
cwd: String,
exit: i64,
duration: i64,
session: Option<String>,
hostname: Option<String>,
2021-02-14 08:15:26 -07:00
) -> Self {
let session = session
.or_else(|| env::var("ATUIN_SESSION").ok())
.unwrap_or_else(uuid_v4);
2021-02-14 10:18:02 -07:00
let hostname =
hostname.unwrap_or_else(|| format!("{}:{}", whoami::hostname(), whoami::username()));
2021-02-13 13:21:49 -07:00
2021-02-14 08:15:26 -07:00
Self {
id: uuid_v4(),
2021-02-13 12:37:00 -07:00
timestamp,
command,
cwd,
2021-02-13 10:02:52 -07:00
exit,
duration,
2021-02-13 13:21:49 -07:00
session,
hostname,
}
}
2022-04-25 00:13:30 -06:00
pub fn success(&self) -> bool {
self.exit == 0 || self.duration == -1
}
}