From 34a2bcce49dc86f3087554fb96a1346ad9ca66cd Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 17 Jun 2025 11:27:30 +0200 Subject: [PATCH] Removed --spawn-terminal for now --- tty-fwd/src/main.rs | 14 +---- tty-fwd/src/sessions.rs | 5 +- tty-fwd/src/term.rs | 131 ---------------------------------------- 3 files changed, 5 insertions(+), 145 deletions(-) delete mode 100644 tty-fwd/src/term.rs diff --git a/tty-fwd/src/main.rs b/tty-fwd/src/main.rs index a57e16e5..148dea41 100644 --- a/tty-fwd/src/main.rs +++ b/tty-fwd/src/main.rs @@ -19,7 +19,7 @@ fn main() -> Result<(), anyhow::Error> { .ok_or_else(|| anyhow!("Unable to determine home directory"))? .join(".vibetunnel/control"); let mut session_name = None::; - let mut session_id = None::; + let mut session_id = std::env::var("TTY_SESSION_ID").ok(); let mut send_key = None::; let mut send_text = None::; let mut signal = None::; @@ -31,7 +31,6 @@ fn main() -> Result<(), anyhow::Error> { let mut serve_address = None::; let mut static_path = None::; let mut password = None::; - let mut spawn_terminal = None::; let mut cmdline = Vec::::new(); while let Some(param) = parser.param()? { @@ -95,9 +94,6 @@ fn main() -> Result<(), anyhow::Error> { p if p.is_long("password") => { password = Some(parser.value()?); } - p if p.is_long("spawn-terminal") => { - spawn_terminal = Some(parser.value()?); - } p if p.is_pos() => { cmdline.push(parser.value()?); } @@ -204,13 +200,7 @@ fn main() -> Result<(), anyhow::Error> { return crate::api_server::start_server(&addr, control_path, static_path, password); } - // Spawn terminal - if let Some(terminal) = spawn_terminal { - let exit_code = term::spawn_terminal(&terminal, &control_path, session_name)?; - std::process::exit(exit_code); - } - // Spawn command - let exit_code = sessions::spawn_command(control_path, session_name, cmdline)?; + let exit_code = sessions::spawn_command(control_path, session_name, session_id, cmdline)?; std::process::exit(exit_code); } diff --git a/tty-fwd/src/sessions.rs b/tty-fwd/src/sessions.rs index 6d1a54ea..cd266372 100644 --- a/tty-fwd/src/sessions.rs +++ b/tty-fwd/src/sessions.rs @@ -373,14 +373,15 @@ pub fn cleanup_sessions( pub fn spawn_command( control_path: std::path::PathBuf, session_name: Option, + session_id: Option, cmdline: Vec, ) -> Result { if cmdline.is_empty() { return Err(anyhow!("No command provided")); } - let session_id = Uuid::new_v4(); - let session_path = control_path.join(session_id.to_string()); + let session_id = session_id.unwrap_or_else(|| Uuid::new_v4().to_string()); + let session_path = control_path.join(session_id); fs::create_dir_all(&session_path)?; let session_info_path = session_path.join("session.json"); let stream_out_path = session_path.join("stream-out"); diff --git a/tty-fwd/src/term.rs b/tty-fwd/src/term.rs deleted file mode 100644 index 7070e280..00000000 --- a/tty-fwd/src/term.rs +++ /dev/null @@ -1,131 +0,0 @@ -use anyhow::anyhow; -use std::process::Command; -use std::str::FromStr; - -pub enum Terminal { - TerminalApp, - Ghostty, -} - -impl Terminal { - pub fn path(&self) -> &str { - match self { - Terminal::TerminalApp => "/System/Applications/Utilities/Terminal.app", - Terminal::Ghostty => "/Applications/Ghostty.app", - } - } - - pub fn name(&self) -> &str { - match self { - Terminal::TerminalApp => "Terminal.app", - Terminal::Ghostty => "Ghostty", - } - } -} - -impl FromStr for Terminal { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s.to_lowercase().as_str() { - "terminal" | "terminal.app" => Ok(Terminal::TerminalApp), - "ghostty" | "ghostty.app" => Ok(Terminal::Ghostty), - _ => Err(anyhow::anyhow!("Unsupported terminal application: {}", s)), - } - } -} - -pub fn spawn_terminal( - terminal: &str, - control_path: &std::path::PathBuf, - session_name: Option, -) -> Result { - let terminal = Terminal::from_str(terminal)?; - let terminal_path = terminal.path(); - - // Build the command line for the terminal application - let mut tty_fwd_cmd = Vec::new(); - tty_fwd_cmd.push( - std::env::current_exe() - .unwrap() - .to_string_lossy() - .to_string(), - ); - tty_fwd_cmd.push("--control-path".to_string()); - tty_fwd_cmd.push(control_path.to_string_lossy().to_string()); - - if let Some(name) = session_name { - tty_fwd_cmd.push("--session-name".to_string()); - tty_fwd_cmd.push(name); - } - - tty_fwd_cmd.push("--".to_string()); - tty_fwd_cmd.push("${SHELL:-/bin/bash}".to_string()); - - // Check if the terminal application exists - if !std::path::Path::new(&terminal_path).exists() { - return Err(anyhow!( - "Terminal application not found: {}", - terminal.name() - )); - } - - // Use osascript to open the terminal and run the command - let script = match terminal { - Terminal::TerminalApp => { - format!( - r#"tell application "Terminal" - activate - do script "{} && exit" -end tell"#, - shell_escape(&tty_fwd_cmd.join(" ")) - ) - } - Terminal::Ghostty => { - format!( - r#"tell application "Ghostty" - activate - tell application "System Events" - keystroke "t" using command down - delay 0.2 - keystroke "{} && exit" - keystroke return - end tell -end tell"#, - shell_escape(&format!("{}", tty_fwd_cmd.join(" "))) - ) - } - }; - - // First, open the terminal application - let open_result = Command::new("open").arg(&terminal_path).output()?; - - if !open_result.status.success() { - return Err(anyhow!( - "Failed to open terminal application: {}", - String::from_utf8_lossy(&open_result.stderr) - )); - } - - // Give the terminal a moment to open - std::thread::sleep(std::time::Duration::from_millis(500)); - - // Run the osascript command - let osascript_result = Command::new("osascript").arg("-e").arg(&script).output()?; - - if !osascript_result.status.success() { - return Err(anyhow!( - "Failed to execute osascript: {}", - String::from_utf8_lossy(&osascript_result.stderr) - )); - } - - // Return 0 for success since we launched the terminal - Ok(0) -} - -fn shell_escape(s: &str) -> String { - s.replace("\\", "\\\\") - .replace("\"", "\\\"") - .replace("'", "\\'") -}