aboutsummaryrefslogtreecommitdiffstats
path: root/src/local
diff options
context:
space:
mode:
Diffstat (limited to 'src/local')
-rw-r--r--src/local/database.rs58
-rw-r--r--src/local/history.rs30
-rw-r--r--src/local/import.rs6
3 files changed, 65 insertions, 29 deletions
diff --git a/src/local/database.rs b/src/local/database.rs
index 2a4cc582..e9882fd7 100644
--- a/src/local/database.rs
+++ b/src/local/database.rs
@@ -8,7 +8,7 @@ use rusqlite::{params, Connection};
use crate::History;
pub trait Database {
- fn save(&self, h: History) -> Result<()>;
+ fn save(&mut self, h: History) -> Result<()>;
fn save_bulk(&mut self, h: &Vec<History>) -> Result<()>;
fn load(&self, id: &str) -> Result<History>;
fn list(&self) -> Result<()>;
@@ -53,6 +53,8 @@ impl SqliteDatabase {
exit integer not null,
command text not null,
cwd text not null,
+ session text not null,
+ hostname text not null,
unique(timestamp, cwd, command)
)",
@@ -64,22 +66,11 @@ impl SqliteDatabase {
}
impl Database for SqliteDatabase {
- fn save(&self, h: History) -> Result<()> {
+ fn save(&mut self, h: History) -> Result<()> {
debug!("saving history to sqlite");
+ let v = vec![h];
- self.conn.execute(
- "insert or ignore into history (
- id,
- timestamp,
- duration,
- exit,
- command,
- cwd
- ) values (?1, ?2, ?3, ?4, ?5, ?6)",
- params![h.id, h.timestamp, h.duration, h.exit, h.command, h.cwd],
- )?;
-
- Ok(())
+ self.save_bulk(&v)
}
fn save_bulk(&mut self, h: &Vec<History>) -> Result<()> {
@@ -95,9 +86,20 @@ impl Database for SqliteDatabase {
duration,
exit,
command,
- cwd
- ) values (?1, ?2, ?3, ?4, ?5, ?6)",
- params![i.id, i.timestamp, i.duration, i.exit, i.command, i.cwd],
+ cwd,
+ session,
+ hostname
+ ) values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
+ params![
+ i.id,
+ i.timestamp,
+ i.duration,
+ i.exit,
+ i.command,
+ i.cwd,
+ i.session,
+ i.hostname
+ ],
)?;
}
@@ -110,7 +112,7 @@ impl Database for SqliteDatabase {
debug!("loading history item");
let mut stmt = self.conn.prepare(
- "select id, timestamp, duration, exit, command, cwd from history
+ "select id, timestamp, duration, exit, command, cwd, session, hostname from history
where id = ?1",
)?;
@@ -122,6 +124,8 @@ impl Database for SqliteDatabase {
exit: row.get(3)?,
command: row.get(4)?,
cwd: row.get(5)?,
+ session: row.get(6)?,
+ hostname: row.get(7)?,
})
})?;
@@ -137,9 +141,9 @@ impl Database for SqliteDatabase {
self.conn.execute(
"update history
- set timestamp = ?2, duration = ?3, exit = ?4, command = ?5, cwd = ?6
+ set timestamp = ?2, duration = ?3, exit = ?4, command = ?5, cwd = ?6, session = ?7, hostname = ?8
where id = ?1",
- params![h.id, h.timestamp, h.duration, h.exit, h.command, h.cwd],
+ params![h.id, h.timestamp, h.duration, h.exit, h.command, h.cwd, h.session, h.hostname],
)?;
Ok(())
@@ -148,9 +152,9 @@ impl Database for SqliteDatabase {
fn list(&self) -> Result<()> {
debug!("listing history");
- let mut stmt = self
- .conn
- .prepare("SELECT id, timestamp, duration, exit, command, cwd FROM history")?;
+ let mut stmt = self.conn.prepare(
+ "SELECT id, timestamp, duration, exit, command, cwd, session, hostname FROM history",
+ )?;
let history_iter = stmt.query_map(params![], |row| {
Ok(History {
@@ -160,6 +164,8 @@ impl Database for SqliteDatabase {
exit: row.get(3)?,
command: row.get(4)?,
cwd: row.get(5)?,
+ session: row.get(6)?,
+ hostname: row.get(7)?,
})
})?;
@@ -167,8 +173,8 @@ impl Database for SqliteDatabase {
let h = h.unwrap();
println!(
- "{} | {} | {} | {} | {}",
- h.timestamp, h.cwd, h.duration, h.exit, h.command
+ "{} | {} | {} | {} | {} | {} | {}",
+ h.timestamp, h.hostname, h.session, h.cwd, h.duration, h.exit, h.command
);
}
diff --git a/src/local/history.rs b/src/local/history.rs
index 00109621..bb8b9123 100644
--- a/src/local/history.rs
+++ b/src/local/history.rs
@@ -1,4 +1,6 @@
-use chrono;
+use std::env;
+
+use hostname;
use uuid::Uuid;
#[derive(Debug)]
@@ -9,10 +11,32 @@ pub struct History {
pub exit: i64,
pub command: String,
pub cwd: String,
+ pub session: String,
+ pub hostname: String,
}
impl History {
- pub fn new(timestamp: i64, command: String, cwd: String, exit: i64, duration: i64) -> History {
+ pub fn new(
+ timestamp: i64,
+ command: String,
+ cwd: String,
+ exit: i64,
+ duration: i64,
+ session: Option<String>,
+ hostname: Option<String>,
+ ) -> History {
+ // get the current session or just generate a random string
+ let env_session =
+ env::var("ATUIN_SESSION").unwrap_or(Uuid::new_v4().to_simple().to_string());
+
+ // best attempt at getting the current hostname, or just unknown
+ let os_hostname = hostname::get().unwrap();
+ let os_hostname = os_hostname.to_str().unwrap();
+ let os_hostname = String::from(os_hostname);
+
+ let session = session.unwrap_or(env_session);
+ let hostname = hostname.unwrap_or(os_hostname);
+
History {
id: Uuid::new_v4().to_simple().to_string(),
timestamp,
@@ -20,6 +44,8 @@ impl History {
cwd,
exit,
duration,
+ session,
+ hostname,
}
}
}
diff --git a/src/local/import.rs b/src/local/import.rs
index ce141c52..0b237814 100644
--- a/src/local/import.rs
+++ b/src/local/import.rs
@@ -70,11 +70,13 @@ fn parse_extended(line: String) -> History {
// use nanos, because why the hell not? we won't display them.
History::new(
- Utc.timestamp(time, 0).timestamp_nanos(),
+ time * 1_000_000_000,
trim_newline(command),
String::from("unknown"),
-1,
duration * 1_000_000_000,
+ None,
+ None,
)
}
@@ -104,6 +106,8 @@ impl Iterator for ImportZsh {
String::from("unknown"),
-1,
-1,
+ None,
+ None,
)))
}
}