Skip to main content

pxh/
lib.rs

1use std::{
2    collections::HashMap,
3    env,
4    fmt::Write as FmtWrite,
5    fs::File,
6    io,
7    io::{BufReader, BufWriter, Read, Write as IoWrite},
8    os::unix::{
9        ffi::{OsStrExt, OsStringExt},
10        fs::MetadataExt,
11    },
12    path::{Path, PathBuf},
13    str,
14    sync::Arc,
15    time::{Duration, Instant},
16};
17
18use anstyle::{AnsiColor, Style};
19use anyhow::Context;
20use bstr::{BString, ByteSlice, io::BufReadExt};
21use jiff::{Timestamp, tz::TimeZone};
22use regex::bytes::Regex;
23use rusqlite::{
24    Connection, Error, ErrorCode, Result, Row, Transaction, TransactionBehavior,
25    functions::FunctionFlags,
26};
27use serde::{Deserialize, Serialize};
28use unicode_width::UnicodeWidthStr;
29
30type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
31
32pub mod config;
33pub mod recall;
34pub mod secrets_patterns;
35pub mod sync;
36pub mod ui;
37
38pub fn get_setting(conn: &Connection, key: &str) -> anyhow::Result<Option<BString>> {
39    let mut stmt = conn.prepare("SELECT value FROM settings WHERE key = ?")?;
40    let mut rows = stmt.query([key])?;
41
42    if let Some(row) = rows.next()? {
43        let value: Vec<u8> = row.get(0)?;
44        Ok(Some(BString::from(value)))
45    } else {
46        Ok(None)
47    }
48}
49
50pub fn set_setting(conn: &Connection, key: &str, value: &BString) -> anyhow::Result<()> {
51    conn.execute(
52        "INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
53        (key, value.as_bytes()),
54    )?;
55    Ok(())
56}
57
58const TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
59
60/// Seconds since the Unix epoch; 0 if the clock is before it.
61pub fn unix_now() -> i64 {
62    std::time::SystemTime::now()
63        .duration_since(std::time::UNIX_EPOCH)
64        .map_or(0, |d| d.as_secs() as i64)
65}
66
67/// A Unix timestamp rendered in the system time zone with a strftime
68/// format, or `None` when it is outside jiff's representable range.
69pub fn format_local_time(unix_seconds: i64, format: &str) -> Option<String> {
70    let ts = Timestamp::from_second(unix_seconds).ok()?;
71    Some(ts.to_zoned(TimeZone::system()).strftime(format).to_string())
72}
73
74pub fn get_hostname() -> BString {
75    let hostname =
76        env::var_os("PXH_HOSTNAME").unwrap_or_else(|| hostname::get().unwrap_or_default());
77
78    // Extract short hostname (before first dot). The shell integration already
79    // sets PXH_HOSTNAME via `hostname -s`, but when falling back to
80    // hostname::get() the result may be an FQDN, so strip the domain suffix.
81    let hostname_bytes = hostname.as_bytes();
82    if let Some(dot_pos) = hostname_bytes.iter().position(|&b| b == b'.') {
83        BString::from(&hostname_bytes[..dot_pos])
84    } else {
85        BString::from(hostname_bytes)
86    }
87}
88
89/// Resolve symlinks in a path, even if the final target doesn't exist yet.
90/// Walks components left-to-right: canonicalizes each prefix that exists on disk,
91/// follows symlinks whose targets don't exist yet, and appends remaining components.
92fn resolve_through_symlinks(path: &Path) -> PathBuf {
93    let mut resolved = PathBuf::new();
94    for component in path.components() {
95        resolved.push(component);
96        if let Ok(canonical) = std::fs::canonicalize(&resolved) {
97            resolved = canonical;
98        } else if let Ok(target) = std::fs::read_link(&resolved) {
99            // Symlink exists but target doesn't -- follow it anyway
100            if target.is_absolute() {
101                resolved = target;
102            } else {
103                resolved.pop();
104                resolved.push(target);
105            }
106        }
107    }
108    resolved
109}
110
111/// Resolve hostname: config > DB setting (legacy "original_hostname") > live hostname.
112pub fn resolve_hostname(config: &config::Config, conn: &Connection) -> BString {
113    if let Some(ref h) = config.host.hostname {
114        return BString::from(h.as_bytes());
115    }
116    get_setting(conn, "original_hostname").ok().flatten().unwrap_or_else(get_hostname)
117}
118
119/// Build the set of hostnames that count as "this host" for recall filtering.
120/// Returns {current_hostname} ∪ {aliases}, deduped.
121pub fn effective_host_set(config: &config::Config) -> Vec<BString> {
122    let current = get_hostname();
123    let mut hosts = vec![current];
124    for alias in &config.host.aliases {
125        let b = BString::from(alias.as_bytes());
126        if !hosts.contains(&b) {
127            hosts.push(b);
128        }
129    }
130    hosts
131}
132
133/// Migrate host settings from DB legacy storage to config file.
134/// Called from install and config commands, not on every connection.
135///
136/// - If config lacks hostname, read from DB (legacy "original_hostname"), then delete from DB.
137/// - If config hostname doesn't match live hostname, move old to aliases and update.
138/// - If config lacks machine_id, generate one.
139pub fn migrate_host_settings(conn: &Connection) {
140    if config::Config::has_parse_error() {
141        ui::warn("config.toml has parse errors; skipping host-settings migration");
142        return;
143    }
144    let config = config::Config::load();
145    let mut updates: Vec<(&str, toml_edit::Item)> = Vec::new();
146    let live_hostname = get_hostname();
147
148    // Step 1: Establish config hostname (from DB legacy or live)
149    let config_hostname = if let Some(ref h) = config.host.hostname {
150        BString::from(h.as_bytes())
151    } else if let Ok(Some(hostname)) = get_setting(conn, "original_hostname") {
152        updates.push(("host.hostname", toml_edit::value(hostname.to_string())));
153        hostname
154    } else {
155        updates.push(("host.hostname", toml_edit::value(live_hostname.to_string())));
156        live_hostname.clone()
157    };
158
159    // Step 2: If hostname changed, move old to aliases and update
160    if config_hostname != live_hostname {
161        let mut aliases = config.host.aliases.clone();
162        let old_str = config_hostname.to_string();
163        if !aliases.contains(&old_str) {
164            aliases.push(old_str);
165        }
166        let alias_array = toml_edit::Array::from_iter(aliases.iter().map(String::as_str));
167        updates.push(("host.aliases", toml_edit::value(alias_array)));
168        updates.push(("host.hostname", toml_edit::value(live_hostname.to_string())));
169    }
170
171    // Step 3: Generate machine_id if missing
172    if config.host.machine_id.is_none() {
173        let id = rand::random::<u64>();
174        // Mask to 63 bits so the value stays positive in TOML (signed i64).
175        // A negative i64 can't deserialize into Option<u64>, which would cause
176        // the entire config file to fail to parse.
177        let id = id & i64::MAX as u64;
178        updates.push(("host.machine_id", toml_edit::value(id as i64)));
179    }
180
181    if !updates.is_empty()
182        && let Err(e) = config::Config::update_default_config(&updates)
183    {
184        ui::warn(&format!("failed to migrate host settings to config: {e}"));
185        return;
186    }
187
188    // Clear legacy original_hostname from DB after successful config write
189    if config.host.hostname.is_none() {
190        let _ = conn.execute("DELETE FROM settings WHERE key = 'original_hostname'", []);
191    }
192
193    // Mirror machine_id into the DB so sync can identify this database by its
194    // machine identity (independent of file path). Re-load config to pick up
195    // any value we just wrote above.
196    if let Some(machine_id) = config::Config::load().host.machine_id {
197        let bs = BString::from(machine_id.to_string());
198        let _ = set_setting(conn, "local_machine_id", &bs);
199    }
200}
201/// Return the pxh data directory. Prefers XDG, falls back to `~/.pxh` if it exists.
202/// New installs default to `$XDG_DATA_HOME/pxh` (`~/.local/share/pxh`).
203pub fn pxh_data_dir() -> Option<PathBuf> {
204    let home = env::home_dir()?;
205    let xdg_data =
206        env::var("XDG_DATA_HOME").map_or_else(|_| home.join(".local/share"), PathBuf::from);
207    let xdg_dir = xdg_data.join("pxh");
208    if xdg_dir.exists() {
209        return Some(xdg_dir);
210    }
211    let legacy = home.join(".pxh");
212    if legacy.exists() {
213        return Some(legacy);
214    }
215    Some(xdg_dir)
216}
217
218/// Return the pxh config directory. Prefers XDG, falls back to `~/.pxh` if it exists.
219/// New installs default to `$XDG_CONFIG_HOME/pxh` (`~/.config/pxh`).
220pub fn pxh_config_dir() -> Option<PathBuf> {
221    let home = env::home_dir()?;
222    let xdg_config =
223        env::var("XDG_CONFIG_HOME").map_or_else(|_| home.join(".config"), PathBuf::from);
224    let xdg_dir = xdg_config.join("pxh");
225    if xdg_dir.exists() {
226        return Some(xdg_dir);
227    }
228    let legacy = home.join(".pxh");
229    if legacy.exists() {
230        return Some(legacy);
231    }
232    Some(xdg_dir)
233}
234
235/// Return the default database path (`pxh_data_dir()/pxh.db`).
236pub fn default_db_path() -> Option<PathBuf> {
237    Some(pxh_data_dir()?.join("pxh.db"))
238}
239
240/// Initialize base schema (persistent tables and indexes only).
241/// Safe to call on foreign databases (scan/scrub --dir) -- all DDL is idempotent.
242pub fn initialize_base_schema(conn: &Connection) -> anyhow::Result<()> {
243    conn.execute_batch(include_str!("base_schema.sql"))?;
244    Ok(())
245}
246
247/// Set up the in-memory memdb tables and register the REGEXP function.
248/// Only needed by commands that use `memdb.show_results` or REGEXP (show, scrub).
249pub fn initialize_full_schema(conn: &Connection) -> anyhow::Result<()> {
250    conn.execute_batch(
251        "ATTACH DATABASE ':memory:' AS memdb;
252         CREATE TABLE memdb.show_results (
253             ch_rowid INTEGER NOT NULL,
254             ch_start_unix_timestamp INTEGER,
255             ch_id INTEGER NOT NULL
256         );
257         CREATE INDEX memdb.result_timestamp ON show_results(ch_start_unix_timestamp, ch_id);",
258    )?;
259    conn.create_scalar_function("regexp", 2, FunctionFlags::SQLITE_DETERMINISTIC, move |ctx| {
260        assert_eq!(ctx.len(), 2, "called with unexpected number of arguments");
261        let regexp: Arc<Regex> = ctx
262            .get_or_create_aux(0, |vr| -> Result<_, BoxError> { Ok(Regex::new(vr.as_str()?)?) })?;
263        let is_match = {
264            let text = ctx.get_raw(1).as_bytes().map_err(|e| Error::UserFunctionError(e.into()))?;
265            regexp.is_match(text)
266        };
267        Ok(is_match)
268    })?;
269    Ok(())
270}
271
272/// Current schema version -- bump when adding new migrations below.
273pub const CURRENT_SCHEMA_VERSION: i32 = 3;
274
275/// Run versioned schema migrations tracked via PRAGMA user_version.
276pub fn run_schema_migrations(conn: &Connection) -> anyhow::Result<()> {
277    let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0))?;
278
279    if version < 1 {
280        // Column may already exist on databases created before version tracking.
281        match conn.execute("ALTER TABLE command_history ADD COLUMN machine_id INTEGER", []) {
282            Ok(_) => {}
283            Err(e) if e.to_string().contains("duplicate column name") => {}
284            Err(e) => return Err(e.into()),
285        }
286        conn.pragma_update(None, "user_version", 1)?;
287    }
288
289    if version < 2 {
290        // Composite index for fast MAX(id) lookups in seal's UPDATE query.
291        conn.execute_batch(
292            "CREATE INDEX IF NOT EXISTS idx_session_id_desc ON command_history(session_id, id DESC)",
293        )?;
294        conn.pragma_update(None, "user_version", 2)?;
295    }
296
297    if version < 3 {
298        // v3 introduces settings.local_machine_id so sync can identify a DB by
299        // its machine identity rather than by file path. The actual write
300        // happens in migrate_host_settings (Install/Config of the local DB
301        // only) -- doing it here would contaminate foreign databases that
302        // also pass through run_schema_migrations during the sync merge.
303        conn.pragma_update(None, "user_version", 3)?;
304    }
305
306    Ok(())
307}
308
309/// Open a lightweight connection (persistent schema only, no memdb/regexp).
310/// Use for hot-path commands like insert and seal.
311pub fn sqlite_connection(path: &Option<PathBuf>) -> anyhow::Result<Connection> {
312    sqlite_connection_inner(path, false)
313}
314
315/// Open a full connection with memdb tables and REGEXP function.
316/// Use for commands that need `memdb.show_results` or REGEXP (show, scrub, scan).
317pub fn sqlite_connection_full(path: &Option<PathBuf>) -> anyhow::Result<Connection> {
318    sqlite_connection_inner(path, true)
319}
320
321fn sqlite_connection_inner(path: &Option<PathBuf>, full: bool) -> anyhow::Result<Connection> {
322    let path = path.as_ref().context("Database not defined; use --db or PXH_DB_PATH")?;
323    if let Some(parent) = path.parent() {
324        // Follow symlinks so create_dir_all creates the real target directory
325        // rather than conflicting with an existing symlink entry.
326        let resolved = resolve_through_symlinks(parent);
327        std::fs::create_dir_all(resolved)?;
328    }
329    // No path here: rusqlite's own message already names the file.
330    let conn = Connection::open(path).context("open history database")?;
331
332    // Ensure the database file is only readable by the owner
333    use std::os::unix::fs::PermissionsExt;
334    if let Ok(metadata) = std::fs::metadata(path) {
335        let mode = metadata.permissions().mode();
336        if mode & 0o077 != 0 {
337            std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
338        }
339    }
340
341    conn.busy_timeout(Duration::from_millis(5000))?;
342    conn.pragma_update(None, "journal_mode", "WAL")?;
343    conn.pragma_update(None, "temp_store", "MEMORY")?;
344    conn.pragma_update(None, "cache_size", -65536_i64)?; // 64 MB (negative = KiB)
345    conn.pragma_update(None, "mmap_size", 268435456_i64)?; // 256 MB
346    conn.pragma_update(None, "synchronous", "NORMAL")?;
347
348    // Schema DDL is idempotent but not free -- skip it once the database is at
349    // the current version. Hot-path commands (insert, seal) open a fresh
350    // connection per invocation, so this saves a parse + several PRAGMA calls
351    // every time.
352    let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0))?;
353    if version != CURRENT_SCHEMA_VERSION {
354        initialize_base_schema(&conn)?;
355        run_schema_migrations(&conn)?;
356    }
357
358    if full {
359        initialize_full_schema(&conn)?;
360    }
361
362    Ok(conn)
363}
364
365/// Run `f` against a write transaction, retrying on `SQLITE_BUSY`/`SQLITE_LOCKED`
366/// with exponential backoff and jitter. Each attempt uses `BEGIN IMMEDIATE` so
367/// contention is detected up front rather than mid-transaction. Gives up once
368/// `max_total` elapses and returns the final busy error.
369///
370/// Why this exists: SQLite's own `busy_timeout` retries are unfair under heavy
371/// write contention (a single waiter can spend the full timeout sleeping while
372/// other writers slip past). Jittered backoff at the caller gives fairness and
373/// a bounded worst-case latency suitable for the shell hot path.
374pub fn with_write_retry<T, F>(
375    conn: &mut Connection,
376    max_total: Duration,
377    mut f: F,
378) -> anyhow::Result<T>
379where
380    F: FnMut(&Transaction) -> Result<T>,
381{
382    let start = Instant::now();
383    let mut backoff = Duration::from_micros(500);
384    let cap = Duration::from_millis(50);
385    loop {
386        let attempt = (|| -> Result<T> {
387            let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
388            let value = f(&tx)?;
389            tx.commit()?;
390            Ok(value)
391        })();
392        match attempt {
393            Ok(v) => return Ok(v),
394            Err(e) if is_busy(&e) => {
395                let elapsed = start.elapsed();
396                if elapsed >= max_total {
397                    return Err(e.into());
398                }
399                let jitter_us = rand::random::<u64>() % backoff.as_micros().max(1) as u64;
400                let sleep = backoff + Duration::from_micros(jitter_us);
401                std::thread::sleep(sleep.min(max_total - elapsed));
402                backoff = (backoff * 2).min(cap);
403            }
404            Err(e) => return Err(e.into()),
405        }
406    }
407}
408
409fn is_busy(e: &Error) -> bool {
410    matches!(
411        e,
412        Error::SqliteFailure(err, _)
413            if matches!(err.code, ErrorCode::DatabaseBusy | ErrorCode::DatabaseLocked)
414    )
415}
416
417/// Read this database's `local_machine_id` setting, parsed as u64.
418/// Accepts either TEXT or BLOB storage so we tolerate values written by
419/// non-pxh tooling. Returns None if the setting is absent or unparseable --
420/// callers should fall back to non-watermarked behavior in that case.
421pub fn read_local_machine_id(conn: &Connection) -> Option<u64> {
422    conn.query_row("SELECT value FROM settings WHERE key = 'local_machine_id'", [], |row| {
423        let bytes = row.get_ref(0)?.as_bytes().unwrap_or(&[]);
424        Ok(str::from_utf8(bytes).ok().and_then(|s| s.parse::<u64>().ok()))
425    })
426    .ok()
427    .flatten()
428}
429
430#[derive(Debug, Default, Serialize, Deserialize)]
431pub struct Invocation {
432    pub command: BString,
433    pub shellname: String,
434    pub working_directory: Option<BString>,
435    pub hostname: Option<BString>,
436    pub username: Option<BString>,
437    pub exit_status: Option<i64>,
438    pub start_unix_timestamp: Option<i64>,
439    pub end_unix_timestamp: Option<i64>,
440    pub session_id: i64,
441    #[serde(default)]
442    pub machine_id: Option<u64>,
443}
444
445impl Invocation {
446    fn sameish(&self, other: &Self) -> bool {
447        self.command == other.command && self.start_unix_timestamp == other.start_unix_timestamp
448    }
449
450    pub fn insert(&self, tx: &Transaction) -> Result<()> {
451        tx.execute(
452            r#"
453INSERT OR IGNORE INTO command_history (
454    session_id,
455    full_command,
456    shellname,
457    hostname,
458    username,
459    working_directory,
460    exit_status,
461    start_unix_timestamp,
462    end_unix_timestamp,
463    machine_id
464)
465VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#,
466            (
467                self.session_id,
468                self.command.as_slice(),
469                self.shellname.clone(),
470                self.hostname.as_ref().map(|v| v.to_vec()),
471                self.username.as_ref().map(|v| v.to_vec()),
472                self.working_directory.as_ref().map(|v| v.to_vec()),
473                self.exit_status,
474                self.start_unix_timestamp,
475                self.end_unix_timestamp,
476                self.machine_id.map(|id| id as i64),
477            ),
478        )?;
479        Ok(())
480    }
481}
482
483// Try to generate a "stable" session id based on the file imported.
484// If that fails, just create a random one.
485fn generate_import_session_id(histfile: &Path) -> i64 {
486    if let Ok(st) = std::fs::metadata(histfile) {
487        ((st.ino() << 16) | st.dev()) as i64
488    } else {
489        (rand::random::<u64>() >> 1) as i64
490    }
491}
492
493/// Join backslash-continuation lines in zsh EXTENDED_HISTORY format.
494/// Lines ending with `\` are joined with their successor using `\n` as separator.
495pub fn join_continuation_lines(buf: &[u8]) -> Vec<Vec<u8>> {
496    let mut result: Vec<Vec<u8>> = Vec::new();
497    let mut current: Vec<u8> = Vec::new();
498    for line in buf.split(|&ch| ch == b'\n') {
499        if line.last() == Some(&b'\\') {
500            current.extend_from_slice(&line[..line.len() - 1]);
501            current.push(b'\n');
502        } else {
503            current.extend_from_slice(line);
504            if !current.is_empty() {
505                result.push(std::mem::take(&mut current));
506            }
507        }
508    }
509    if !current.is_empty() {
510        result.push(current);
511    }
512    result
513}
514
515/// Undo zsh's internal "metafication": bytes in its token range are stored as
516/// `0x83` followed by the byte XOR `0x20`. Most CJK and emoji UTF-8 sequences
517/// contain such bytes, so history written by zsh must be decoded before use.
518pub fn unmetafy(bytes: &[u8]) -> Vec<u8> {
519    let mut out = Vec::with_capacity(bytes.len());
520    let mut iter = bytes.iter();
521    while let Some(&b) = iter.next() {
522        match (b, iter.clone().next()) {
523            (0x83, Some(&next)) => {
524                out.push(next ^ 0x20);
525                iter.next();
526            }
527            _ => out.push(b),
528        }
529    }
530    out
531}
532
533pub fn import_zsh_history(
534    histfile: &Path,
535    hostname: Option<BString>,
536    username: Option<BString>,
537) -> anyhow::Result<Vec<Invocation>> {
538    let mut f = File::open(histfile)?;
539    let mut buf = Vec::new();
540    let _ = f.read_to_end(&mut buf)?;
541    let username = username
542        .or_else(|| uzers::get_current_username().map(|v| BString::from(v.into_vec())))
543        .unwrap_or_else(|| BString::from("unknown"));
544    let hostname = hostname.unwrap_or_else(get_hostname);
545    // Pre-join backslash-continuation lines: zsh EXTENDED_HISTORY writes
546    // multi-line commands as physical lines ending with '\'.
547    let logical_lines = join_continuation_lines(&buf);
548
549    let mut ret = vec![];
550    let mut skipped = 0usize;
551    let session_id = generate_import_session_id(histfile);
552    for (line_num, line) in logical_lines.iter().enumerate() {
553        let line = line.as_slice();
554        let Some((fields, command)) = line.split_once_str(";") else {
555            continue;
556        };
557        let Some((_skip, rest)) = fields.split_once_str(":") else {
558            continue;
559        };
560        let Some((start_time, duration_seconds)) = rest.split_once_str(":") else {
561            continue;
562        };
563        let Some(start_unix_timestamp) = str::from_utf8(start_time.get(1..).unwrap_or(&[]))
564            .ok()
565            .and_then(|s| s.parse::<i64>().ok())
566        else {
567            eprintln!(
568                "warning: {}: skipping line {}: bad timestamp {:?}",
569                histfile.display(),
570                line_num + 1,
571                BString::from(start_time),
572            );
573            skipped += 1;
574            continue;
575        };
576        let Some(duration) =
577            str::from_utf8(duration_seconds).ok().and_then(|s| s.parse::<i64>().ok())
578        else {
579            eprintln!(
580                "warning: {}: skipping line {}: bad duration {:?}",
581                histfile.display(),
582                line_num + 1,
583                BString::from(duration_seconds),
584            );
585            skipped += 1;
586            continue;
587        };
588        let invocation = Invocation {
589            command: BString::from(unmetafy(command)),
590            shellname: "zsh".into(),
591            hostname: Some(BString::from(hostname.as_bytes())),
592            username: Some(BString::from(username.as_bytes())),
593            start_unix_timestamp: Some(start_unix_timestamp),
594            end_unix_timestamp: Some(start_unix_timestamp + duration),
595            session_id,
596            ..Default::default()
597        };
598
599        ret.push(invocation);
600    }
601
602    if skipped > 0 {
603        ui::warn(&format!(
604            "{}: skipped {}",
605            histfile.display(),
606            ui::count(skipped, "malformed line")
607        ));
608    }
609
610    Ok(dedup_invocations(ret))
611}
612
613pub fn import_bash_history(
614    histfile: &Path,
615    hostname: Option<BString>,
616    username: Option<BString>,
617) -> anyhow::Result<Vec<Invocation>> {
618    let mut f = File::open(histfile)?;
619    let mut buf = Vec::new();
620    let _ = f.read_to_end(&mut buf)?;
621    let username = username
622        .or_else(|| uzers::get_current_username().map(|v| BString::from(v.as_bytes())))
623        .unwrap_or_else(|| BString::from("unknown"));
624    let hostname = hostname.unwrap_or_else(get_hostname);
625    let buf_iter = buf.split(|&ch| ch == b'\n').filter(|l| !l.is_empty());
626
627    let mut ret = vec![];
628    let session_id = generate_import_session_id(histfile);
629    let mut last_ts = None;
630    for line in buf_iter {
631        if line[0] == b'#'
632            && let Ok(ts) = str::parse::<i64>(str::from_utf8(&line[1..]).unwrap_or(""))
633        {
634            if ts > 0 {
635                last_ts = Some(ts);
636            }
637            continue;
638        }
639        let invocation = Invocation {
640            command: BString::from(line),
641            shellname: "bash".into(),
642            hostname: Some(BString::from(hostname.as_bytes())),
643            username: Some(BString::from(username.as_bytes())),
644            start_unix_timestamp: last_ts,
645            session_id,
646            ..Default::default()
647        };
648
649        ret.push(invocation);
650    }
651
652    Ok(dedup_invocations(ret))
653}
654
655pub fn import_json_history(histfile: &Path) -> anyhow::Result<Vec<Invocation>> {
656    let f = File::open(histfile)?;
657    let reader = BufReader::new(f);
658    Ok(serde_json::from_reader(reader)?)
659}
660
661fn dedup_invocations(mut invocations: Vec<Invocation>) -> Vec<Invocation> {
662    invocations.dedup_by(|a, b| a.sameish(b));
663    invocations
664}
665
666impl Invocation {
667    pub fn from_row(row: &Row) -> Result<Self, Error> {
668        Ok(Invocation {
669            session_id: row.get("session_id")?,
670            command: BString::from(row.get::<_, Vec<u8>>("full_command")?),
671            shellname: row.get("shellname")?,
672            working_directory: row
673                .get::<_, Option<Vec<u8>>>("working_directory")?
674                .map(BString::from),
675            hostname: row.get::<_, Option<Vec<u8>>>("hostname")?.map(BString::from),
676            username: row.get::<_, Option<Vec<u8>>>("username")?.map(BString::from),
677            exit_status: row.get("exit_status")?,
678            start_unix_timestamp: row.get("start_unix_timestamp")?,
679            end_unix_timestamp: row.get("end_unix_timestamp")?,
680            machine_id: row.get::<_, Option<i64>>("machine_id").ok().flatten().map(|v| v as u64),
681        })
682    }
683}
684
685// Create a pretty export string that gets serialized as an array of
686// bytes only if it isn't valid UTF-8; this makes the json export
687// prettier.
688#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
689#[serde(untagged)]
690enum PrettyExportString {
691    Readable(String),
692    Encoded(Vec<u8>),
693}
694
695impl From<&[u8]> for PrettyExportString {
696    fn from(bytes: &[u8]) -> Self {
697        match str::from_utf8(bytes) {
698            Ok(v) => Self::Readable(v.to_string()),
699            _ => Self::Encoded(bytes.to_vec()),
700        }
701    }
702}
703
704impl From<Option<&Vec<u8>>> for PrettyExportString {
705    fn from(bytes: Option<&Vec<u8>>) -> Self {
706        match bytes {
707            Some(v) => match str::from_utf8(v.as_slice()) {
708                Ok(s) => Self::Readable(s.to_string()),
709                _ => Self::Encoded(v.clone()),
710            },
711            None => Self::Readable(String::new()),
712        }
713    }
714}
715
716impl Invocation {
717    fn to_json_export(&self) -> serde_json::Value {
718        serde_json::json!({
719            "session_id": self.session_id,
720            "command": PrettyExportString::from(self.command.as_slice()),
721            "shellname": self.shellname,
722            "working_directory": self.working_directory.as_ref().map(
723                |b| PrettyExportString::from(b.as_slice())
724            ),
725            "hostname": self.hostname.as_ref().map(
726                |b| PrettyExportString::from(b.as_slice())
727            ),
728            "username": self.username.as_ref().map(
729                |b| PrettyExportString::from(b.as_slice())
730            ),
731            "exit_status": self.exit_status,
732            "start_unix_timestamp": self.start_unix_timestamp,
733            "end_unix_timestamp": self.end_unix_timestamp,
734            "machine_id": self.machine_id,
735        })
736    }
737}
738
739pub fn json_export(rows: &[Invocation]) -> anyhow::Result<()> {
740    let json_values: Vec<serde_json::Value> = rows.iter().map(Invocation::to_json_export).collect();
741    serde_json::to_writer(io::stdout(), &json_values)?;
742    Ok(())
743}
744
745/// One cell of the `show` table: text (possibly several lines) and its color.
746struct Cell {
747    text: String,
748    style: Style,
749}
750
751impl Cell {
752    fn new(text: impl Into<String>, style: Style) -> Self {
753        Self { text: text.into(), style }
754    }
755}
756
757/// Render rows as a borderless table: one space of padding each side of
758/// every cell, every cell padded to its column's width in terminal columns,
759/// a multi-line cell continued on the following lines, and each cell's
760/// style wrapping content and padding together. Colors are emitted
761/// unconditionally; the caller prints through `anstream`, which strips them
762/// when stdout is not a terminal.
763fn render_table(rows: &[Vec<Cell>]) -> String {
764    let columns = rows.iter().map(Vec::len).max().unwrap_or(0);
765    let widths: Vec<usize> = (0..columns)
766        .map(|col| {
767            rows.iter()
768                .filter_map(|row| row.get(col))
769                .flat_map(|cell| cell.text.lines())
770                .map(UnicodeWidthStr::width)
771                .max()
772                .unwrap_or(0)
773        })
774        .collect();
775
776    let mut out = String::new();
777    for row in rows {
778        let height = row.iter().map(|cell| cell.text.lines().count().max(1)).max().unwrap_or(1);
779        for line_no in 0..height {
780            for (col, width) in widths.iter().enumerate() {
781                let (text, style) = row.get(col).map_or(("", Style::new()), |cell| {
782                    (cell.text.lines().nth(line_no).unwrap_or(""), cell.style)
783                });
784                let last = col + 1 == widths.len();
785                let pad = if last { 0 } else { width - text.width() };
786                let _ = write!(out, " {style}{text}{:pad$}{style:#} ", "");
787            }
788            out.push('\n');
789        }
790    }
791    out
792}
793
794struct QueryResultColumnDisplayer {
795    header: &'static str,
796    header_style: Style,
797    displayer: Box<dyn Fn(&Invocation) -> Cell>,
798}
799
800fn time_display_helper(t: Option<i64>) -> String {
801    t.and_then(|t| format_local_time(t, TIME_FORMAT)).unwrap_or_else(|| "n/a".to_string())
802}
803
804fn binary_display_helper(v: &BString) -> String {
805    v.to_str_lossy().into_owned()
806}
807
808const WHITE: Style = AnsiColor::White.on_default();
809const GREEN: Style = AnsiColor::Green.on_default();
810const RED: Style = AnsiColor::Red.on_default();
811const MAGENTA: Style = AnsiColor::Magenta.on_default();
812const CYAN: Style = AnsiColor::Cyan.on_default();
813const DIM: Style = Style::new().dimmed();
814const BOLD_BLUE: Style = AnsiColor::Blue.on_default().bold();
815
816fn displayers() -> HashMap<&'static str, QueryResultColumnDisplayer> {
817    let mut ret = HashMap::new();
818    ret.insert(
819        "command",
820        QueryResultColumnDisplayer {
821            header: "Command",
822            header_style: WHITE,
823            displayer: Box::new(|row| Cell::new(binary_display_helper(&row.command), WHITE)),
824        },
825    );
826    ret.insert(
827        "start_time",
828        QueryResultColumnDisplayer {
829            header: "Start",
830            header_style: GREEN,
831            displayer: Box::new(|row| {
832                Cell::new(time_display_helper(row.start_unix_timestamp), GREEN)
833            }),
834        },
835    );
836    ret.insert(
837        "end_time",
838        QueryResultColumnDisplayer {
839            header: "End",
840            header_style: GREEN,
841            displayer: Box::new(|row| {
842                Cell::new(time_display_helper(row.end_unix_timestamp), GREEN)
843            }),
844        },
845    );
846    ret.insert(
847        "duration",
848        QueryResultColumnDisplayer {
849            header: "Duration",
850            header_style: MAGENTA,
851            displayer: Box::new(|row| {
852                let text = match (row.start_unix_timestamp, row.end_unix_timestamp) {
853                    (Some(start), Some(end)) => format!("{}s", end - start),
854                    _ => "n/a".into(),
855                };
856                Cell::new(text, MAGENTA)
857            }),
858        },
859    );
860    ret.insert(
861        "status",
862        QueryResultColumnDisplayer {
863            header: "Status",
864            header_style: RED,
865            displayer: Box::new(|row| match row.exit_status {
866                Some(0) => Cell::new("0", GREEN),
867                Some(s) => Cell::new(s.to_string(), RED),
868                None => Cell::new("n/a", DIM),
869            }),
870        },
871    );
872    // TODO: Make session similar to "context" and just print `.` when
873    // it is the current session.
874    ret.insert(
875        "session",
876        QueryResultColumnDisplayer {
877            header: "Session",
878            header_style: CYAN,
879            displayer: Box::new(|row| Cell::new(row.session_id.to_string(), CYAN)),
880        },
881    );
882    // Print context specially; the full output is $HOST:$PATH but if
883    // $HOST is the current host, the $HOST: is omitted.  If $PATH is
884    // the current working directory, it is replaced with `.`.
885    ret.insert(
886        "context",
887        QueryResultColumnDisplayer {
888            header: "Context",
889            header_style: BOLD_BLUE,
890            displayer: Box::new(|row| {
891                let current_hostname = get_hostname();
892                let row_hostname = row.hostname.clone().unwrap_or_default();
893                let mut ret = String::new();
894                if current_hostname != row_hostname {
895                    write!(ret, "{row_hostname}:").unwrap_or_default();
896                }
897                let current_directory = env::current_dir().unwrap_or_default();
898                ret.push_str(&row.working_directory.as_ref().map_or_else(String::new, |v| {
899                    let v = v.to_str_lossy();
900                    if v == current_directory.to_string_lossy() {
901                        String::from(".")
902                    } else {
903                        v.into_owned()
904                    }
905                }));
906
907                Cell::new(ret, BOLD_BLUE)
908            }),
909        },
910    );
911
912    ret
913}
914
915pub fn present_results_human_readable(
916    fields: &[&str],
917    rows: &[Invocation],
918    suppress_headers: bool,
919) -> anyhow::Result<()> {
920    let displayers = displayers();
921    let columns: Vec<&QueryResultColumnDisplayer> = fields
922        .iter()
923        .map(|field| {
924            displayers.get(field).with_context(|| format!("Invalid 'show' field: {field}"))
925        })
926        .collect::<anyhow::Result<_>>()?;
927
928    let mut table = Vec::with_capacity(rows.len() + 1);
929    if !suppress_headers {
930        table.push(columns.iter().map(|d| Cell::new(d.header, d.header_style)).collect());
931    }
932    for row in rows {
933        let failed = matches!(row.exit_status, Some(s) if s != 0);
934        table.push(
935            columns
936                .iter()
937                .map(|d| {
938                    let cell = (d.displayer)(row);
939                    if failed { Cell { style: RED, ..cell } } else { cell }
940                })
941                .collect(),
942        );
943    }
944    anstream::print!("{}", render_table(&table));
945    Ok(())
946}
947
948// Rewrite a file with lines matching `contraband` removed.  utf-8
949// safe for the file (TODO: I guess make contraband a `BString` too)
950pub fn atomically_remove_lines_from_file(
951    input_filepath: &Path,
952    contraband: &str,
953) -> anyhow::Result<()> {
954    // Resolve symlinks so persist() replaces the real target, not the symlink
955    let resolved = resolve_through_symlinks(input_filepath);
956    let original_perms = std::fs::metadata(&resolved)?.permissions();
957    let input_file = File::open(&resolved)?;
958    let mut input_reader = BufReader::new(input_file);
959
960    let parent = resolved.parent().unwrap_or(Path::new("."));
961    let temp_file = tempfile::NamedTempFile::new_in(parent)?;
962    let mut output_writer = BufWriter::new(&temp_file);
963
964    input_reader.for_byte_line_with_terminator(|line| {
965        if !line.contains_str(contraband) {
966            output_writer.write_all(line)?;
967        }
968        Ok(true)
969    })?;
970
971    output_writer.flush()?;
972    drop(output_writer);
973    temp_file.persist(&resolved)?;
974    std::fs::set_permissions(&resolved, original_perms)?;
975    Ok(())
976}
977
978/// Rewrite a file removing lines that exactly match any of the contraband items.
979/// Unlike `atomically_remove_lines_from_file`, this matches complete lines (after trimming).
980pub fn atomically_remove_matching_lines_from_file(
981    input_filepath: &Path,
982    contraband_items: &[&str],
983) -> anyhow::Result<()> {
984    use std::collections::HashSet;
985
986    // Resolve symlinks so persist() replaces the real target, not the symlink
987    let resolved = resolve_through_symlinks(input_filepath);
988    let original_perms = std::fs::metadata(&resolved)?.permissions();
989    let contraband_set: HashSet<&str> = contraband_items.iter().copied().collect();
990
991    let input_file = File::open(&resolved)?;
992    let mut input_reader = BufReader::new(input_file);
993
994    let parent = resolved.parent().unwrap_or(Path::new("."));
995    let temp_file = tempfile::NamedTempFile::new_in(parent)?;
996    let mut output_writer = BufWriter::new(&temp_file);
997
998    input_reader.for_byte_line_with_terminator(|line| {
999        let line_str = line.to_str_lossy();
1000        let trimmed = line_str.trim();
1001        if !contraband_set.contains(trimmed) {
1002            output_writer.write_all(line)?;
1003        }
1004        Ok(true)
1005    })?;
1006
1007    output_writer.flush()?;
1008    drop(output_writer);
1009    temp_file.persist(&resolved)?;
1010    std::fs::set_permissions(&resolved, original_perms)?;
1011    Ok(())
1012}
1013
1014// Helper functions for command parsing and path resolution
1015pub mod helpers {
1016    use std::ffi::OsString;
1017    use std::path::{Path, PathBuf};
1018
1019    /// Parse an SSH command string into command and arguments, handling quotes and spaces.
1020    /// Similar to how rsync and other tools parse the -e option.
1021    pub fn parse_ssh_command(ssh_cmd: &str) -> (String, Vec<String>) {
1022        // If it's a simple command without spaces, just return it
1023        if !ssh_cmd.contains(char::is_whitespace) {
1024            return (ssh_cmd.to_string(), vec![]);
1025        }
1026
1027        // Otherwise, we need to parse it properly
1028        let mut cmd = String::new();
1029        let mut args = Vec::new();
1030        let mut current = String::new();
1031        let mut in_quotes = false;
1032        let mut quote_char = '\0';
1033        let mut is_first = true;
1034        let mut chars = ssh_cmd.chars().peekable();
1035
1036        while let Some(ch) = chars.next() {
1037            match ch {
1038                '"' | '\'' if !in_quotes => {
1039                    in_quotes = true;
1040                    quote_char = ch;
1041                }
1042                '"' | '\'' if in_quotes && ch == quote_char => {
1043                    in_quotes = false;
1044                    quote_char = '\0';
1045                }
1046                ' ' | '\t' if !in_quotes => {
1047                    if !current.is_empty() {
1048                        if is_first {
1049                            cmd = current.clone();
1050                            is_first = false;
1051                        } else {
1052                            args.push(current.clone());
1053                        }
1054                        current.clear();
1055                    }
1056                }
1057                '\\' if chars.peek().is_some() => {
1058                    // Handle escaped characters
1059                    if let Some(next_ch) = chars.next() {
1060                        current.push(next_ch);
1061                    }
1062                }
1063                _ => {
1064                    current.push(ch);
1065                }
1066            }
1067        }
1068
1069        // Don't forget the last token
1070        if !current.is_empty() {
1071            if is_first {
1072                cmd = current;
1073            } else {
1074                args.push(current);
1075            }
1076        }
1077
1078        (cmd, args)
1079    }
1080
1081    /// Build a list of candidate paths to search for pxh on the remote host.
1082    /// The first candidate that exists and is executable will be used.
1083    fn remote_pxh_candidates(configured_path: &str) -> Vec<String> {
1084        let mut candidates = Vec::new();
1085
1086        if configured_path != "pxh" {
1087            candidates.push(configured_path.to_string());
1088            return candidates;
1089        }
1090
1091        // Try the same relative-to-home path as the local binary
1092        if let Some(rel) = get_relative_path_from_home(None, None)
1093            && rel != "pxh"
1094        {
1095            candidates.push(format!("$HOME/{rel}"));
1096        }
1097
1098        // Common installation locations
1099        for p in [
1100            "$HOME/.cargo/bin/pxh",
1101            "$HOME/bin/pxh",
1102            "$HOME/.local/bin/pxh",
1103            "/usr/local/bin/pxh",
1104            "/usr/bin/pxh",
1105        ] {
1106            if !candidates.contains(&p.to_string()) {
1107                candidates.push(p.to_string());
1108            }
1109        }
1110
1111        candidates
1112    }
1113
1114    /// Build a shell command that finds and executes pxh on the remote host.
1115    /// When a single explicit path is configured, uses it directly.
1116    /// Otherwise, probes a prioritized list of candidate locations.
1117    pub fn build_remote_pxh_command(configured_path: &str, args: &str) -> String {
1118        let candidates = remote_pxh_candidates(configured_path);
1119
1120        if candidates.len() == 1 {
1121            return format!("{} {args}", candidates[0]);
1122        }
1123
1124        // Build a shell snippet that tries each candidate in order
1125        let checks: Vec<String> =
1126            candidates.iter().map(|p| format!("[ -x \"{p}\" ] && exec \"{p}\" {args}")).collect();
1127        format!(
1128            "sh -c '{}; echo \"pxh: not found on remote host\" >&2; exit 127'",
1129            checks.join("; ")
1130        )
1131    }
1132
1133    /// Gets the relative path from home directory if the current executable is within it.
1134    /// Returns None if the executable is not in the home directory.
1135    /// Takes optional overrides for testing.
1136    pub fn get_relative_path_from_home(
1137        exe_override: Option<&Path>,
1138        home_override: Option<&Path>,
1139    ) -> Option<String> {
1140        let exe = match exe_override {
1141            Some(path) => path.to_path_buf(),
1142            None => std::env::current_exe().ok()?,
1143        };
1144
1145        let home = match home_override {
1146            Some(path) => path.to_path_buf(),
1147            None => std::env::home_dir()?,
1148        };
1149
1150        exe.strip_prefix(&home).ok().map(|path| path.to_string_lossy().to_string())
1151    }
1152
1153    /// Return a shell expression that resolves the pxh database path on a remote host.
1154    /// Checks XDG path first, falls back to legacy ~/.pxh.
1155    pub fn default_remote_db_expr() -> String {
1156        r#"$(if [ -d "${XDG_DATA_HOME:-$HOME/.local/share}/pxh" ]; then echo "${XDG_DATA_HOME:-$HOME/.local/share}/pxh/pxh.db"; elif [ -d "$HOME/.pxh" ]; then echo "$HOME/.pxh/pxh.db"; else echo "${XDG_DATA_HOME:-$HOME/.local/share}/pxh/pxh.db"; fi)"#.to_string()
1157    }
1158
1159    // --- bootstrap ----------------------------------------------------------
1160
1161    const INSTALL_SCRIPT_URL: &str =
1162        "https://raw.githubusercontent.com/chipturner/pxhist/main/install.sh";
1163
1164    /// Single-quote `s` for a POSIX shell unless it is plainly safe as-is.
1165    pub fn shell_quote(s: &str) -> String {
1166        let safe = |c: char| c.is_ascii_alphanumeric() || "_-./".contains(c);
1167        if !s.is_empty() && s.chars().all(safe) {
1168            s.to_string()
1169        } else {
1170            format!("'{}'", s.replace('\'', "'\\''"))
1171        }
1172    }
1173
1174    /// Quote `--install-dir` for interpolation into the remote install
1175    /// command. A leading `~` becomes `"$HOME"` so it still expands on the
1176    /// remote; the rest is quoted so spaces and metacharacters are inert.
1177    pub fn quote_remote_install_dir(install_dir: &str) -> String {
1178        match install_dir.strip_prefix("~/") {
1179            _ if install_dir == "~" => "\"$HOME\"".to_string(),
1180            Some(rest) => format!("\"$HOME\"/{}", shell_quote(rest)),
1181            None => shell_quote(install_dir),
1182        }
1183    }
1184
1185    /// The remote install command line: fetch install.sh, then run it with
1186    /// the release as `$1`. The script is fetched into a variable first --
1187    /// `sh -c "$(curl ...)"` alone runs an empty command (exit 0) when the
1188    /// fetch fails, and the install would be reported as done.
1189    pub fn install_command(install_dir: &str, release: &str) -> String {
1190        format!(
1191            "s=$(curl -sSfL {INSTALL_SCRIPT_URL}) || {{ rc=$?; echo \"could not fetch install.sh\" >&2; exit $rc; }}; \
1192             PXH_INSTALL_DIR={} sh -c \"$s\" sh {}",
1193            quote_remote_install_dir(install_dir),
1194            shell_quote(release)
1195        )
1196    }
1197
1198    /// What the post-install probe (`pxh --version` through the same
1199    /// candidate paths `sync --remote` uses) established about the remote.
1200    #[derive(Debug, Clone, PartialEq, Eq)]
1201    pub enum Probe {
1202        /// The installed binary answered with its version.
1203        Version(String),
1204        /// Exit 127: the install dir is not one `sync --remote` probes.
1205        NotOnPath,
1206        /// ssh failed, or the binary printed something that is not a version.
1207        Unknown,
1208    }
1209
1210    /// The probe's answer is its last stdout line (a remote rc file that
1211    /// echoes on login puts noise in front of it), of the form
1212    /// `pxh X.Y.Z (...)`.
1213    pub fn parse_probe_output(status_code: Option<i32>, stdout: &str) -> Probe {
1214        match status_code {
1215            Some(127) => Probe::NotOnPath,
1216            Some(0) => stdout
1217                .lines()
1218                .last()
1219                .and_then(|l| l.trim().strip_prefix("pxh "))
1220                .and_then(|rest| rest.split_whitespace().next())
1221                .map_or(Probe::Unknown, |v| Probe::Version(v.to_string())),
1222            _ => Probe::Unknown,
1223        }
1224    }
1225
1226    /// The installed executable as a remote shell word: quoted like the
1227    /// install dir, so a directory with spaces works and `~` still expands.
1228    pub fn remote_pxh_path(install_dir: &str) -> String {
1229        format!("{}/pxh", quote_remote_install_dir(install_dir))
1230    }
1231
1232    /// The install line of a bootstrap: what got installed and whether the
1233    /// probe of its own path confirmed it.
1234    pub fn bootstrap_report(
1235        host: &str,
1236        release: &str,
1237        probe: &Probe,
1238        local_version: &str,
1239    ) -> String {
1240        let what =
1241            if release == "latest" { "pxh (latest)".to_string() } else { format!("pxh {release}") };
1242        match probe {
1243            Probe::Version(v) if v == local_version => {
1244                format!("installed {what} on {host} (matches this machine)")
1245            }
1246            Probe::Version(v) => format!(
1247                "installed {what} on {host} (version {v}, but this machine is {local_version})"
1248            ),
1249            Probe::NotOnPath | Probe::Unknown => {
1250                format!("installed {what} on {host} (could not confirm the installed version)")
1251            }
1252        }
1253    }
1254
1255    /// Whether plain `pxh sync --remote <host>` would run the binary just
1256    /// installed: `plain` is the probe through sync's candidate paths,
1257    /// `installed` the probe of the install path. `None` when they agree (or
1258    /// nothing can be said); otherwise the warning naming the fix.
1259    pub fn findability_report(
1260        host: &str,
1261        install_dir: &str,
1262        installed: &Probe,
1263        plain: &Probe,
1264    ) -> Option<String> {
1265        let fix = format!("pass --remote-pxh {install_dir}/pxh");
1266        match (installed, plain) {
1267            (_, Probe::NotOnPath) => Some(format!(
1268                "`pxh sync --remote {host}` will not look in {install_dir}; {fix} \
1269                 (or install to ~/.local/bin)"
1270            )),
1271            (Probe::Version(new), Probe::Version(found)) if new != found => Some(format!(
1272                "another pxh ({found}) shadows the one just installed for plain \
1273                 `pxh sync --remote {host}`; remove it or {fix}"
1274            )),
1275            _ => None,
1276        }
1277    }
1278
1279    /// Determine if the executable is being invoked as pxhs (shorthand for pxh show)
1280    pub fn determine_is_pxhs(args: &[OsString]) -> bool {
1281        args.first()
1282            .and_then(|arg| {
1283                PathBuf::from(arg).file_name().map(|name| name.to_string_lossy().contains("pxhs"))
1284            })
1285            .unwrap_or(false)
1286    }
1287}
1288
1289/// Test utilities - only intended for use in tests
1290#[doc(hidden)]
1291pub mod test_utils {
1292    use std::{
1293        env,
1294        ffi::OsStr,
1295        path::{Path, PathBuf},
1296        process::Command,
1297    };
1298
1299    use rand::{RngExt, distr::Alphanumeric};
1300    use tempfile::TempDir;
1301
1302    /// Return the `detail` column of `EXPLAIN QUERY PLAN` for `sql`, one
1303    /// string per plan node. Hot-path tests assert on these to pin the
1304    /// O(window) vs O(table) behaviour of a query independent of timing.
1305    pub fn explain_query_plan(
1306        conn: &rusqlite::Connection,
1307        sql: &str,
1308        params: &[&dyn rusqlite::types::ToSql],
1309    ) -> Vec<String> {
1310        let mut stmt = conn.prepare(&format!("EXPLAIN QUERY PLAN {sql}")).unwrap();
1311        stmt.query_map(params, |row| row.get::<_, String>(3))
1312            .unwrap()
1313            .collect::<Result<Vec<_>, _>>()
1314            .unwrap()
1315    }
1316
1317    /// Path to the built `pxh` binary, for integration tests.
1318    ///
1319    /// Read at run time because the compile-time `env!` form exists only in
1320    /// the integration-test crates, not in this library. nextest always
1321    /// exports the variable; plain `cargo test` does from cargo 1.100, so an
1322    /// older toolchain must run the suite through nextest.
1323    pub fn pxh_path() -> PathBuf {
1324        let path = env::var_os("CARGO_BIN_EXE_pxh")
1325            .map(PathBuf::from)
1326            .expect("CARGO_BIN_EXE_pxh is not set; run integration tests with `just test`");
1327        assert!(path.exists(), "pxh binary not found at {path:?}");
1328        path
1329    }
1330
1331    /// A bare `pxh` process with only the coverage environment propagated.
1332    /// For tests that build their own HOME/DB environment; everything else
1333    /// should use `PxhTestHelper::command()`.
1334    pub fn pxh_command() -> Command {
1335        let mut cmd = Command::new(pxh_path());
1336        for key in ["LLVM_PROFILE_FILE", "CARGO_LLVM_COV"] {
1337            if let Ok(v) = env::var(key) {
1338                cmd.env(key, v);
1339            }
1340        }
1341        cmd
1342    }
1343
1344    fn generate_random_string(length: usize) -> String {
1345        rand::rng().sample_iter(&Alphanumeric).take(length).map(char::from).collect()
1346    }
1347
1348    fn get_standard_path() -> String {
1349        // Use getconf to get the standard PATH
1350        Command::new("getconf")
1351            .arg("PATH")
1352            .output()
1353            .ok()
1354            .and_then(|output| {
1355                if output.status.success() { String::from_utf8(output.stdout).ok() } else { None }
1356            })
1357            .map_or_else(
1358                || {
1359                    // Fallback to a reasonable default if getconf fails
1360                    "/usr/bin:/bin:/usr/sbin:/sbin".to_string()
1361                },
1362                |s| s.trim().to_string(),
1363            )
1364    }
1365
1366    /// Unified test helper for invoking pxh with proper isolation and environment setup
1367    pub struct PxhTestHelper {
1368        _tmpdir: TempDir,
1369        pub hostname: String,
1370        pub username: String,
1371        home_dir: PathBuf,
1372        db_path: PathBuf,
1373    }
1374
1375    impl PxhTestHelper {
1376        pub fn new() -> Self {
1377            let tmpdir = TempDir::new().unwrap();
1378            let home_dir = tmpdir.path().to_path_buf();
1379            let db_path = home_dir.join(".pxh/pxh.db");
1380
1381            // Create .pxh dir and a config with empty ignore_patterns so tests
1382            // aren't affected by default ignore patterns filtering trivial commands.
1383            let pxh_dir = home_dir.join(".pxh");
1384            std::fs::create_dir_all(&pxh_dir).unwrap();
1385            std::fs::write(pxh_dir.join("config.toml"), "[history]\nignore_patterns = []\n")
1386                .unwrap();
1387
1388            PxhTestHelper {
1389                _tmpdir: tmpdir,
1390                hostname: generate_random_string(12),
1391                username: "testuser".to_string(),
1392                home_dir,
1393                db_path,
1394            }
1395        }
1396
1397        pub fn with_custom_db_path(mut self, db_path: impl AsRef<Path>) -> Self {
1398            self.db_path = self.home_dir.join(db_path);
1399            self
1400        }
1401
1402        /// Get the temporary directory path
1403        pub fn home_dir(&self) -> &Path {
1404            &self.home_dir
1405        }
1406
1407        /// Get the database path
1408        pub fn db_path(&self) -> &Path {
1409            &self.db_path
1410        }
1411
1412        /// Get the full PATH including pxh binary directory
1413        pub fn get_full_path(&self) -> String {
1414            format!("{}:{}", pxh_path().parent().unwrap().display(), get_standard_path())
1415        }
1416
1417        /// Create a pxh command with all environment properly set up
1418        pub fn command(&self) -> Command {
1419            let mut cmd = Command::new(pxh_path());
1420
1421            // Clear environment to ensure isolation
1422            cmd.env_clear();
1423
1424            // Set consistent test environment
1425            cmd.env("HOME", &self.home_dir);
1426            cmd.env("PXH_DB_PATH", &self.db_path);
1427            cmd.env("PXH_HOSTNAME", &self.hostname);
1428            cmd.env("USER", &self.username);
1429            cmd.env("PATH", self.get_full_path());
1430
1431            // Propagate coverage environment variables if they exist
1432            for key in ["LLVM_PROFILE_FILE", "CARGO_LLVM_COV"] {
1433                if let Ok(v) = env::var(key) {
1434                    cmd.env(key, v);
1435                }
1436            }
1437
1438            cmd
1439        }
1440
1441        /// Convenience method to create a command with arguments
1442        pub fn command_with_args(&self, args: &[&str]) -> Command {
1443            let mut cmd = self.command();
1444            cmd.args(args);
1445            cmd
1446        }
1447
1448        /// Create a shell command (bash/zsh) with proper environment for interactive testing.
1449        ///
1450        /// The child's PATH is rebuilt from `getconf PATH`, and a bare program
1451        /// name is resolved against *that* PATH, not the caller's -- so pass an
1452        /// absolute path when the shell you verified lives elsewhere (Homebrew
1453        /// bash on macOS).
1454        pub fn shell_command(&self, shell: impl AsRef<OsStr>) -> Command {
1455            let shell = shell.as_ref();
1456            let mut cmd = Command::new(shell);
1457
1458            // Force interactive mode - use login shell to ensure rc files are loaded
1459            cmd.arg("-i");
1460            cmd.env_clear();
1461
1462            // Set consistent environment variables
1463            cmd.env("HOME", &self.home_dir);
1464            cmd.env("PXH_DB_PATH", &self.db_path);
1465            cmd.env("PXH_HOSTNAME", &self.hostname);
1466            cmd.env("PATH", self.get_full_path());
1467
1468            // Set a clean environment for testing
1469            cmd.env("USER", &self.username);
1470            cmd.env("SHELL", shell);
1471
1472            // For bash to properly load rc files in a minimal environment
1473            cmd.env("BASH_ENV", self.home_dir.join(".bashrc"));
1474
1475            // Ubuntu's /etc/zsh/zshrc runs compinit for every interactive
1476            // shell unless this is set; on hosts with an insecure fpath
1477            // directory (GitHub runners) compinit prompts and hangs the pty.
1478            cmd.env("skip_global_compinit", "1");
1479
1480            cmd
1481        }
1482    }
1483
1484    impl Default for PxhTestHelper {
1485        fn default() -> Self {
1486            Self::new()
1487        }
1488    }
1489}
1490
1491#[cfg(test)]
1492mod tests {
1493    use super::*;
1494
1495    /// Create an in-memory database with full schema and migrations applied.
1496    fn test_connection() -> Connection {
1497        let conn = Connection::open_in_memory().unwrap();
1498        initialize_base_schema(&conn).unwrap();
1499        run_schema_migrations(&conn).unwrap();
1500        conn
1501    }
1502
1503    #[test]
1504    fn test_resolve_hostname_from_config() {
1505        let conn = test_connection();
1506
1507        let mut config = config::Config::default();
1508        config.host.hostname = Some("from-config".to_string());
1509        set_setting(&conn, "original_hostname", &BString::from("from-db")).unwrap();
1510
1511        let result = resolve_hostname(&config, &conn);
1512        assert_eq!(result, BString::from("from-config"));
1513    }
1514
1515    #[test]
1516    fn test_resolve_hostname_from_db() {
1517        let conn = test_connection();
1518
1519        let config = config::Config::default();
1520        set_setting(&conn, "original_hostname", &BString::from("from-db")).unwrap();
1521
1522        let result = resolve_hostname(&config, &conn);
1523        assert_eq!(result, BString::from("from-db"));
1524    }
1525
1526    #[test]
1527    fn test_resolve_hostname_live_fallback() {
1528        let conn = test_connection();
1529
1530        let config = config::Config::default();
1531        let result = resolve_hostname(&config, &conn);
1532        assert_eq!(result, get_hostname());
1533    }
1534
1535    #[test]
1536    fn test_effective_host_set_no_aliases() {
1537        let config = config::Config::default();
1538        let hosts = effective_host_set(&config);
1539        assert_eq!(hosts, vec![get_hostname()]);
1540    }
1541
1542    #[test]
1543    fn test_effective_host_set_with_aliases() {
1544        let mut config = config::Config::default();
1545        config.host.aliases = vec!["old-host".to_string(), "other-host".to_string()];
1546        let hosts = effective_host_set(&config);
1547        assert_eq!(hosts.len(), 3);
1548        assert_eq!(hosts[0], get_hostname());
1549        assert_eq!(hosts[1], BString::from("old-host"));
1550        assert_eq!(hosts[2], BString::from("other-host"));
1551    }
1552
1553    #[test]
1554    fn test_effective_host_set_dedup() {
1555        let mut config = config::Config::default();
1556        let current = get_hostname().to_string();
1557        config.host.aliases = vec![current, "other".to_string()];
1558        let hosts = effective_host_set(&config);
1559        assert_eq!(hosts.len(), 2);
1560    }
1561
1562    #[test]
1563    fn test_unmetafy_decodes_pairs_and_leaves_plain_bytes() {
1564        assert_eq!(unmetafy(b"plain ascii"), b"plain ascii");
1565        // U+1F600 as zsh stores it: 9F -> 83 BF, 98 -> 83 B8.
1566        assert_eq!(unmetafy(&[0xF0, 0x83, 0xBF, 0x83, 0xB8, 0x80]), "😀".as_bytes());
1567        // A trailing lone Meta byte has nothing to decode and is kept.
1568        assert_eq!(unmetafy(&[b'x', 0x83]), [b'x', 0x83]);
1569    }
1570
1571    #[test]
1572    fn test_migration_fresh_database() {
1573        let conn = Connection::open_in_memory().unwrap();
1574        conn.execute_batch(include_str!("base_schema.sql")).unwrap();
1575
1576        let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0)).unwrap();
1577        assert_eq!(version, 0);
1578
1579        run_schema_migrations(&conn).unwrap();
1580
1581        let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0)).unwrap();
1582        assert_eq!(version, CURRENT_SCHEMA_VERSION);
1583
1584        // machine_id column should exist
1585        conn.execute(
1586            "INSERT INTO command_history (session_id, full_command, shellname, machine_id) VALUES (1, X'6C73', 'bash', 42)",
1587            [],
1588        )
1589        .unwrap();
1590    }
1591
1592    #[test]
1593    fn test_migration_idempotent() {
1594        let conn = test_connection();
1595        // Run migrations again -- should be a no-op
1596        run_schema_migrations(&conn).unwrap();
1597
1598        let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0)).unwrap();
1599        assert_eq!(version, CURRENT_SCHEMA_VERSION);
1600    }
1601
1602    #[test]
1603    fn test_migration_legacy_database_with_machine_id() {
1604        // Simulate a database that was used with post-machine_id pxh but has no version tracking
1605        let conn = Connection::open_in_memory().unwrap();
1606        conn.execute_batch(include_str!("base_schema.sql")).unwrap();
1607        conn.execute("ALTER TABLE command_history ADD COLUMN machine_id INTEGER", []).unwrap();
1608
1609        let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0)).unwrap();
1610        assert_eq!(version, 0);
1611
1612        // Should handle the duplicate column gracefully and bump version
1613        run_schema_migrations(&conn).unwrap();
1614
1615        let version: i32 = conn.pragma_query_value(None, "user_version", |row| row.get(0)).unwrap();
1616        assert_eq!(version, CURRENT_SCHEMA_VERSION);
1617    }
1618
1619    #[test]
1620    fn test_with_write_retry_succeeds_immediately() {
1621        let mut conn = Connection::open_in_memory().unwrap();
1622        conn.execute_batch("CREATE TABLE t (v INTEGER);").unwrap();
1623        with_write_retry(&mut conn, Duration::from_secs(1), |tx| {
1624            tx.execute("INSERT INTO t VALUES (?)", [42])?;
1625            Ok(())
1626        })
1627        .unwrap();
1628        let v: i64 = conn.query_row("SELECT v FROM t", [], |r| r.get(0)).unwrap();
1629        assert_eq!(v, 42);
1630    }
1631
1632    #[test]
1633    fn test_with_write_retry_propagates_non_busy_error() {
1634        let mut conn = Connection::open_in_memory().unwrap();
1635        let result = with_write_retry(&mut conn, Duration::from_secs(1), |tx| {
1636            tx.execute("SELECT * FROM does_not_exist", [])?;
1637            Ok(())
1638        });
1639        assert!(result.is_err());
1640    }
1641
1642    #[test]
1643    fn test_with_write_retry_retries_then_succeeds() {
1644        let mut conn = Connection::open_in_memory().unwrap();
1645        conn.execute_batch("CREATE TABLE t (v INTEGER);").unwrap();
1646        let mut calls = 0;
1647        with_write_retry(&mut conn, Duration::from_secs(1), |tx| {
1648            calls += 1;
1649            if calls < 3 {
1650                // Synthesize a busy error to drive the retry loop.
1651                return Err(Error::SqliteFailure(
1652                    rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_BUSY),
1653                    None,
1654                ));
1655            }
1656            tx.execute("INSERT INTO t VALUES (?)", [1])?;
1657            Ok(())
1658        })
1659        .unwrap();
1660        assert_eq!(calls, 3);
1661        let n: i64 = conn.query_row("SELECT COUNT(*) FROM t", [], |r| r.get(0)).unwrap();
1662        assert_eq!(n, 1);
1663    }
1664
1665    #[test]
1666    fn test_with_write_retry_gives_up_after_timeout() {
1667        let mut conn = Connection::open_in_memory().unwrap();
1668        let start = Instant::now();
1669        let result: Result<(), _> = with_write_retry(&mut conn, Duration::from_millis(20), |_tx| {
1670            Err(Error::SqliteFailure(rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_BUSY), None))
1671        });
1672        let elapsed = start.elapsed();
1673        assert!(result.is_err());
1674        // Should give up roughly at the cap, not run forever.
1675        assert!(elapsed < Duration::from_millis(200), "took {elapsed:?}");
1676    }
1677    #[test]
1678    fn render_table_pads_columns_and_continues_multiline_cells() {
1679        let plain = |t: &str| Cell { text: t.to_string(), style: Style::new() };
1680        let rows = vec![
1681            vec![plain("Start"), plain("Command")],
1682            vec![plain("t1"), plain("a\nbb")],
1683            vec![plain("日本"), plain("x")],
1684        ];
1685        // One space each side of every cell, every cell but the last padded to
1686        // its column (in terminal columns, so 日本 is as wide as four ASCII
1687        // characters), continuation lines under their own column.
1688        assert_eq!(render_table(&rows), " Start  Command \n t1     a \n        bb \n 日本   x \n");
1689    }
1690
1691    #[test]
1692    fn render_table_styles_content_and_padding_as_one_span() {
1693        let red = AnsiColor::Red.on_default();
1694        let rows = vec![
1695            vec![Cell { text: "ab".into(), style: red }, Cell { text: "c".into(), style: red }],
1696            vec![
1697                Cell { text: "abcd".into(), style: Style::new() },
1698                Cell { text: "c".into(), style: Style::new() },
1699            ],
1700        ];
1701        let out = render_table(&rows);
1702        assert_eq!(out.lines().next().unwrap(), format!(" {red}ab  {red:#}  {red}c{red:#} "));
1703        assert_eq!(anstream::adapter::strip_str(&out).to_string(), " ab    c \n abcd  c \n");
1704    }
1705    #[test]
1706    fn format_local_time_handles_range() {
1707        assert_eq!(format_local_time(0, "%Y").unwrap().len(), 4);
1708        assert_eq!(format_local_time(i64::MAX, TIME_FORMAT), None);
1709    }
1710}