use std::path::Path;
use anyhow::Result;
use super::{Env, TabToOpen, TerminalAdapter, applescript};
use crate::engine::rules::TerminalShape;
use crate::procs::ProcInfo;
use crate::shells;
pub struct Ghostty;
impl TerminalAdapter for Ghostty {
fn id(&self) -> &'static str {
"ghostty"
}
fn name(&self) -> &'static str {
"Ghostty"
}
fn shape(&self) -> TerminalShape {
TerminalShape::Tabs
}
fn find(&self, env: &Env, lineage: &[ProcInfo]) -> Option<ProcInfo> {
if cfg!(target_os = "macos") {
ghostty_of(env, lineage)
} else {
None
}
}
fn reopen(&self, _exe: Option<&Path>, tabs: &[TabToOpen]) -> Result<()> {
for tab in tabs {
applescript::run(NEW_TAB, &new_tab_arguments(tab))?;
}
Ok(())
}
}
fn ghostty_of(env: &Env, lineage: &[ProcInfo]) -> Option<ProcInfo> {
if env.get("TERM_PROGRAM").map(String::as_str) != Some("ghostty") {
return None;
}
let is_terminal = |process: &ProcInfo| process.name == "ghostty";
match lineage.get(1) {
Some(parent) if is_terminal(parent) => Some(parent.clone()),
Some(parent) if parent.name == "login" => lineage
.get(2)
.filter(|process| is_terminal(process))
.cloned(),
_ => None,
}
}
const NEW_TAB: &str = r#"on run argv
set startFolder to item 1 of argv
set typed to item 2 of argv
with timeout of 30 seconds
tell application id "com.mitchellh.ghostty"
set settings to new surface configuration
set initial working directory of settings to startFolder
if typed is not "" then
set initial input of settings to typed & return
end if
if (count of windows) is 0 then
new window with configuration settings
else
new tab in front window with configuration settings
end if
end tell
end timeout
end run"#;
fn new_tab_arguments(tab: &TabToOpen) -> Vec<String> {
let typed = if tab.run.is_empty() {
String::new()
} else {
shells::bash_run_line(&tab.cwd, &tab.run)
};
vec![tab.cwd.clone(), typed]
}
#[cfg(test)]
mod tests {
use super::*;
use crate::procs::{fake::FakeTable, lineage};
const ID: &str = "00000000-0000-4000-8000-000000000001";
fn tab_env() -> Env {
Env::from([("TERM_PROGRAM".to_string(), "ghostty".to_string())])
}
fn tab_table() -> FakeTable {
FakeTable::new(&[
(50, Some(40), "zsh", Some(500)),
(40, Some(30), "zsh", Some(400)),
(30, Some(20), "login", Some(300)),
(20, Some(1), "ghostty", Some(200)),
])
}
#[test]
fn the_ghostty_that_started_a_tab_s_login_is_the_terminal() {
let found = ghostty_of(&tab_env(), &lineage(&tab_table(), 40));
assert_eq!(found.map(|process| process.pid), Some(20));
let tracked = Ghostty
.find(&tab_env(), &lineage(&tab_table(), 40))
.is_some();
assert_eq!(tracked, cfg!(target_os = "macos"));
}
#[test]
fn a_shell_inside_a_tab_is_not_a_tab() {
assert!(ghostty_of(&tab_env(), &lineage(&tab_table(), 50)).is_none());
assert!(ghostty_of(&Env::new(), &lineage(&tab_table(), 40)).is_none());
}
#[test]
fn ghostty_gives_kaeru_no_tab_id() {
let mut env = tab_env();
env.insert("TERM_SESSION_ID".into(), ID.into());
assert_eq!(Ghostty.tab_id(&env), None);
}
fn tab(cwd: &str, run: &[&str]) -> TabToOpen {
TabToOpen {
title: "web-ui".into(),
cwd: cwd.into(),
shell: Some("zsh".into()),
run: run.iter().map(|word| word.to_string()).collect(),
..TabToOpen::default()
}
}
#[test]
fn a_plain_tab_opens_in_its_folder_and_types_nothing() {
let folder = "/Users/demo/code/it's my 프로젝트";
assert_eq!(new_tab_arguments(&tab(folder, &[])), [folder, ""]);
}
#[test]
fn an_agent_tab_types_the_line_that_resumes_its_session() {
let folder = "/Users/demo/code/web ui";
assert_eq!(
new_tab_arguments(&tab(folder, &["claude", "--resume", ID])),
[
folder.to_string(),
format!("cd -- '/Users/demo/code/web ui' && claude --resume {ID}")
]
);
}
}