diff --git a/Cargo.lock b/Cargo.lock index bd889af..0d6bf58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -683,7 +683,7 @@ dependencies = [ [[package]] name = "flyline" -version = "2.0.1" +version = "2.0.2" dependencies = [ "ansi-to-tui", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 0b70381..b232fb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ multiple_crate_versions = { level = "allow", priority = 1 } [package] name = "flyline" -version = "2.0.1" +version = "2.0.2" edition.workspace = true rust-version.workspace = true publish = false diff --git a/src/app/mod.rs b/src/app/mod.rs index b20ae10..4ebc1ab 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1959,7 +1959,7 @@ impl<'a> App<'a> { self.has_run_delayed_startup = Some(std::time::Instant::now()); log::debug!("Running delayed startup initialization"); time_it!("delayed startup", { - let _ = crate::term_info::get_term_info(&GLOBAL_EVENT_READER); + let _ = crate::term_info::get_term_info(); self.sync_viewport_top_from_cpr(); if self.terminal.viewport_top().is_none() { diff --git a/src/changelog.rs b/src/changelog.rs index f88491a..d7a7608 100644 --- a/src/changelog.rs +++ b/src/changelog.rs @@ -130,6 +130,10 @@ pub(crate) const CHANGELOG: &str = r#"# Changelog ## v2.0.1 (2026-11-23) - **Zero-size terminals**: A terminal that reports a 0x0 size (such as a freshly spawned pty) no longer makes flyline exit the shell; it assumes 80x24. - **History path on macOS**: The help and guide now give the JSONL history path on macOS, `~/Library/Application Support/flyline/history.jsonl`. + +## v2.0.2 (2026-11-23) +- **No terminal attached**: `flyline version` and other commands that detect the terminal now work when bash has no controlling terminal, such as when a script or a coding agent runs `bash -ic`. +- **Atuin import**: `flyline history import` with no path now finds Atuin's database in `~/.atuin/data` (recent Atuin) or `~/.local/share/atuin`, on macOS too. "#; pub(crate) fn pretty_changelog() -> String { diff --git a/src/cli.rs b/src/cli.rs index b14372c..8b32145 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2100,7 +2100,7 @@ fn show_version(copy: bool) { let term_program = term_info::term_program().unwrap_or_else(|| "unknown".to_string()); let term_program_version = term_info::term_program_version().unwrap_or_else(|| "unknown".to_string()); - let info = term_info::get_term_info(&crate::app::GLOBAL_EVENT_READER); + let info = term_info::get_term_info(); let detected_emulator = info.emulator.name().to_string(); let detected_multiplexer = info .multiplexer diff --git a/src/history/importing.rs b/src/history/importing.rs index 9cb0ef9..366b0a5 100644 --- a/src/history/importing.rs +++ b/src/history/importing.rs @@ -189,20 +189,47 @@ pub fn import_history_file(path: &Path, target_jsonl_path: &Path) -> anyhow::Res Ok(imported_count) } -pub fn import_atuin_history(target_jsonl_path: &Path) -> anyhow::Result { - let db_path = dirs::data_local_dir() - .unwrap_or_else(|| PathBuf::from("~/.local/share")) - .join("atuin") - .join("history.db"); +/// Where Atuin may keep its history database, most specific first. +/// +/// Recent Atuin releases use `$ATUIN_HOME/data` (default `~/.atuin/data`); older ones use +/// `$XDG_DATA_HOME/atuin` (default `~/.local/share/atuin`) on every Unix, macOS included. +fn atuin_db_candidates() -> Vec { + let home = dirs::home_dir(); + let mut dirs_to_try = Vec::new(); + if let Some(atuin_home) = std::env::var_os("ATUIN_HOME") { + dirs_to_try.push(PathBuf::from(atuin_home).join("data")); + } + if let Some(home) = &home { + dirs_to_try.push(home.join(".atuin").join("data")); + } + if let Some(xdg) = std::env::var_os("XDG_DATA_HOME") { + dirs_to_try.push(PathBuf::from(xdg).join("atuin")); + } + if let Some(home) = &home { + dirs_to_try.push(home.join(".local").join("share").join("atuin")); + } + if let Some(data) = dirs::data_local_dir() { + dirs_to_try.push(data.join("atuin")); + } + let mut candidates: Vec = dirs_to_try + .into_iter() + .map(|d| d.join("history.db")) + .collect(); + candidates.dedup(); + candidates +} - if !db_path.exists() { +pub fn import_atuin_history(target_jsonl_path: &Path) -> anyhow::Result { + let candidates = atuin_db_candidates(); + let Some(db_path) = candidates.iter().find(|p| p.exists()) else { + let tried: Vec = candidates.iter().map(|p| p.display().to_string()).collect(); return Err(anyhow::anyhow!( - "Atuin database file not found at {}", - db_path.display() + "Atuin database not found (tried {}). Pass its path: flyline history import ", + tried.join(", ") )); - } + }; - import_atuin_sqlite_file(&db_path, target_jsonl_path) + import_atuin_sqlite_file(db_path, target_jsonl_path) } #[cfg(test)] diff --git a/src/term_info.rs b/src/term_info.rs index 1571fb6..86032f5 100644 --- a/src/term_info.rs +++ b/src/term_info.rs @@ -485,16 +485,24 @@ static STATIC_TERM_INFO: Mutex> = Mutex::new(None); /// Access the static [`TermInfo`]. /// /// If already initialized, returns the static struct. -/// If not yet initialized, queries the terminal via `reader` (blocking), -/// initializes `TermInfo::new(reader)`, stores it in the static instance, and returns it. -pub fn get_term_info(reader: &termina::EventReader) -> TermInfo { +/// If not yet initialized, queries the terminal through the global event reader (blocking), +/// stores the result in the static instance, and returns it. +/// +/// Without a controlling terminal (for example `bash -ic 'flyline version'` run by a script or +/// an agent), there is nothing to query and opening the event reader would fail, so this +/// returns [`TermInfo::from_env`] without caching it. +pub fn get_term_info() -> TermInfo { if let Ok(guard) = STATIC_TERM_INFO.lock() && let Some(ref info) = *guard { return info.clone(); } - let info = TermInfo::new(reader); + if !has_controlling_terminal() { + return TermInfo::from_env(); + } + + let info = TermInfo::new(&crate::app::GLOBAL_EVENT_READER); if let Ok(mut guard) = STATIC_TERM_INFO.lock() { if guard.is_none() { *guard = Some(info.clone()); @@ -506,19 +514,28 @@ pub fn get_term_info(reader: &termina::EventReader) -> TermInfo { info } +/// Whether this process has a controlling terminal that the event reader can open. +fn has_controlling_terminal() -> bool { + std::fs::OpenOptions::new() + .read(true) + .write(true) + .open("/dev/tty") + .is_ok() +} + /// Returns the cached [`DeviceAttributes`] if device attribute querying has been performed. pub fn device_attributes() -> Option { - get_term_info(&crate::app::GLOBAL_EVENT_READER).device_attributes + get_term_info().device_attributes } /// Returns the detected active terminal emulator for the current process. pub fn current() -> TerminalEmulator { - get_term_info(&crate::app::GLOBAL_EVENT_READER).emulator + get_term_info().emulator } /// Returns the detected active terminal multiplexer for the current process, if any. pub fn multiplexer() -> Option { - get_term_info(&crate::app::GLOBAL_EVENT_READER).multiplexer + get_term_info().multiplexer } /// Helper function to perform DA query on an EventReader.