mod chrome; mod client; mod colors; mod cursor; mod fonts; mod input; mod label; mod launcher; mod pane_name; mod protocol; mod render; mod server; mod session; mod terminal; use anyhow::{Result, bail}; use gpui::{prelude::*, *}; use std::path::PathBuf; fn main() -> Result<()> { env_logger::init(); let mut role = "launch"; let mut socket = None; let mut remote = None; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "--server" => role = "server", "--zygote" => role = "zygote", "--gui" => role = "gui", "--bridge" => role = "bridge", "--shutdown" => role = "shutdown", "--list" => role = "list", "--socket" => { socket = Some(PathBuf::from( args.next() .ok_or_else(|| anyhow::anyhow!("--socket needs a path"))?, )) } "--remote" => { remote = Some( args.next() .ok_or_else(|| anyhow::anyhow!("--remote needs an SSH destination"))?, ) } "--help" | "-h" => { println!( "psyTerm — server-owned terminals with a GPUI front end\n\n psyterm Open a window via the local server's zygote\n psyterm --remote user@host Connect a local GUI to a server over SSH\n psyterm --list List server-owned tabs and panes\n psyterm --shutdown Stop the local server and its shell sessions\n psyterm --socket PATH Use a separate local server\n psyterm --gui Direct GUI launch (diagnostics; bypass zygote)\n psyterm --server Run the server in the foreground\n psyterm --bridge Binary stdin/stdout transport used by SSH\n\nClosing a window detaches; tabs and shells keep running.\nSSH requires key/agent authentication and psyterm in the remote PATH.\nPSYTERM_REMOTE_BIN overrides the remote executable path.\n" ); return Ok(()); } _ => bail!("unknown argument: {arg}; see --help"), } } let socket = socket.map(Ok).unwrap_or_else(launcher::default_socket)?; match role { "server" => { let zygote_socket = socket.clone(); let mut zygote: Option = None; server::run(&socket, move |remote| { if zygote.is_none() { zygote = Some(launcher::Zygote::start(&zygote_socket)?); } zygote.as_mut().unwrap().open_window(remote) }) } "zygote" => launcher::run_zygote(&socket), "gui" => { launcher::ensure_server(&socket)?; run_gui(socket, remote) } "bridge" => client::bridge(&socket), "shutdown" => { launcher::control(&socket, protocol::ClientMessage::Shutdown)?; Ok(()) } "list" => list_sessions(&socket), _ => { launcher::ensure_server(&socket)?; launcher::control( &socket, protocol::ClientMessage::OpenWindow { target: remote }, )?; Ok(()) } } } fn list_sessions(socket: &std::path::Path) -> Result<()> { let mut stream = std::os::unix::net::UnixStream::connect(socket)?; stream.set_read_timeout(Some(std::time::Duration::from_secs(5)))?; protocol::write_message( &mut stream, &protocol::ClientMessage::Hello { version: protocol::VERSION, }, )?; let hello: protocol::ServerMessage = protocol::read_message(&mut stream)?; println!("{hello:?}"); protocol::write_message(&mut stream, &protocol::ClientMessage::List)?; loop { match protocol::read_message(&mut stream)? { protocol::ServerMessage::Snapshot(snapshot) => { for tab in snapshot.tabs { println!("tab {} {} panes {:?}", tab.id, tab.title, tab.panes); } break; } protocol::ServerMessage::Error(error) => bail!("{error}"), _ => {} } } Ok(()) } /// Called directly in a fresh fork of the single-threaded zygote. All GPUI, /// graphics, font discovery and client I/O initialization happens after fork. pub(crate) fn run_gui(socket: PathBuf, remote: Option) -> Result<()> { let (client, updates) = client::Client::connect(&socket, remote.as_deref())?; let connection: SharedString = remote .map(|host| format!("SSH · {host}")) .unwrap_or_else(|| "Local".into()) .into(); Application::new().run(move |cx: &mut App| { cx.on_window_closed(|cx| { if cx.windows().is_empty() { cx.quit(); } }) .detach(); let bounds = Bounds::centered(None, size(px(1000.), px(680.)), cx); cx.open_window( WindowOptions { window_bounds: Some(WindowBounds::Windowed(bounds)), titlebar: Some(TitlebarOptions { title: Some("psyTerm".into()), ..Default::default() }), window_decorations: Some(WindowDecorations::Client), app_id: Some("dev.buzzert.psyterm".into()), ..Default::default() }, move |window, cx| { cx.new(|cx| terminal::Workspace::new(client, updates, connection, window, cx)) }, ) .expect("could not open psyTerm window"); cx.activate(true); }); Ok(()) }