diff options
| author | Jonas Maier <> | 2026-03-08 11:58:38 +0100 |
|---|---|---|
| committer | Jonas Maier <> | 2026-03-08 11:58:38 +0100 |
| commit | 36b934d543f8cfedb0a74c52de0620be7d5fb106 (patch) | |
| tree | 2082d908f0cbc80b2c83070142c7da15a29e1907 /src/history.rs | |
| parent | f005472ded09dc910598f0ce03d6318722ed2c5a (diff) | |
| download | pish-36b934d543f8cfedb0a74c52de0620be7d5fb106.tar.gz | |
persistent history
Diffstat (limited to 'src/history.rs')
| -rw-r--r-- | src/history.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/history.rs b/src/history.rs index e69de29..2e06133 100644 --- a/src/history.rs +++ b/src/history.rs @@ -0,0 +1,77 @@ +use sqlite::Connection; + +use crate::BString; +use crate::date::DateTime; +use std::env::current_dir; +use std::path::PathBuf; + +fn db_file() -> PathBuf { + crate::basedir::data_dir().join("history.db") +} + +#[derive(Clone)] +pub struct HistoryEntry { + /// time of execution + pub time: DateTime, + + /// absolute path where the command was executed + pub loc: BString, + + /// the command + pub cmd: BString, +} + +impl HistoryEntry { + pub fn new(cmd: BString) -> Self { + Self { + time: DateTime::now(), + loc: current_dir() + .unwrap() + .as_os_str() + .as_encoded_bytes() + .to_vec(), + cmd, + } + } +} + +fn try_db() -> sqlite::Result<Connection> { + sqlite::open(db_file()) +} + +fn db() -> Connection { + try_db().unwrap() +} + +pub fn setup() { + let db = db(); + + db.execute( + " + CREATE TABLE IF NOT EXISTS history ( + ts INTEGER NOT NULL, + loc BLOB NOT NULL, + cmd BLOB NOT NULL + ) + ", + ) + .unwrap(); + + db.execute("CREATE INDEX IF NOT EXISTS idx_history_ts ON history(ts)") + .unwrap(); +} + +fn try_persist(entry: &HistoryEntry) -> sqlite::Result<()> { + let db = try_db()?; + let mut s = db.prepare("INSERT INTO history (ts, loc, cmd) VALUES (?, ?, ?)")?; + s.bind((1, entry.time.unix() as i64))?; + s.bind((2, entry.loc.as_slice()))?; + s.bind((3, entry.cmd.as_slice()))?; + s.next()?; + Ok(()) +} + +pub fn persist(entry: &HistoryEntry) { + // keep quiet in case db fails + let _ = try_persist(entry); +} |
