mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-04-14 12:46:05 +00:00
Removed --spawn-terminal for now
This commit is contained in:
parent
df13c82c64
commit
34a2bcce49
3 changed files with 5 additions and 145 deletions
|
|
@ -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::<String>;
|
||||
let mut session_id = None::<String>;
|
||||
let mut session_id = std::env::var("TTY_SESSION_ID").ok();
|
||||
let mut send_key = None::<String>;
|
||||
let mut send_text = None::<String>;
|
||||
let mut signal = None::<i32>;
|
||||
|
|
@ -31,7 +31,6 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
let mut serve_address = None::<String>;
|
||||
let mut static_path = None::<String>;
|
||||
let mut password = None::<String>;
|
||||
let mut spawn_terminal = None::<String>;
|
||||
let mut cmdline = Vec::<OsString>::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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -373,14 +373,15 @@ pub fn cleanup_sessions(
|
|||
pub fn spawn_command(
|
||||
control_path: std::path::PathBuf,
|
||||
session_name: Option<String>,
|
||||
session_id: Option<String>,
|
||||
cmdline: Vec<OsString>,
|
||||
) -> Result<i32, anyhow::Error> {
|
||||
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");
|
||||
|
|
|
|||
|
|
@ -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<Self, Self::Err> {
|
||||
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<String>,
|
||||
) -> Result<i32, anyhow::Error> {
|
||||
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("'", "\\'")
|
||||
}
|
||||
Loading…
Reference in a new issue