2026-11-17 01:08:35 -08:00
mod chrome ;
mod client ;
2026-11-17 00:12:24 -08:00
mod colors ;
2026-11-17 01:08:35 -08:00
mod cursor ;
2026-11-17 00:12:24 -08:00
mod fonts ;
mod input ;
2026-11-17 01:08:35 -08:00
mod label ;
mod launcher ;
mod pane_name ;
mod protocol ;
2026-11-17 00:12:24 -08:00
mod render ;
2026-11-17 01:08:35 -08:00
mod server ;
2026-11-17 00:12:24 -08:00
mod session ;
mod terminal ;
2026-11-17 01:08:35 -08:00
use anyhow ::{ Result , bail };
2026-11-17 00:12:24 -08:00
use gpui ::{ prelude ::* , * };
2026-11-17 01:08:35 -08:00
use std ::path ::PathBuf ;
2026-11-17 00:12:24 -08:00
2026-11-17 01:08:35 -08:00
fn main () -> Result < () > {
2026-11-17 00:12:24 -08:00
env_logger ::init ();
2026-11-17 01:08:35 -08:00
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\n Closing a window detaches; tabs and shells keep running. \n SSH requires key/agent authentication and psyterm in the remote PATH. \n PSYTERM_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 < launcher ::Zygote > = 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 < String > ) -> 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 ();
2026-11-17 00:12:24 -08:00
Application ::new (). run ( move | cx : & mut App | {
cx . on_window_closed ( | cx | {
if cx . windows (). is_empty () {
cx . quit ();
}
})
. detach ();
2026-11-17 01:08:35 -08:00
let bounds = Bounds ::centered ( None , size ( px ( 1000. ), px ( 680. )), cx );
2026-11-17 00:12:24 -08:00
cx . open_window (
WindowOptions {
window_bounds : Some ( WindowBounds ::Windowed ( bounds )),
titlebar : Some ( TitlebarOptions {
2026-11-17 01:08:35 -08:00
title : Some ( "psyTerm" . into ()),
2026-11-17 00:12:24 -08:00
.. Default ::default ()
}),
2026-11-17 01:08:35 -08:00
window_decorations : Some ( WindowDecorations ::Client ),
app_id : Some ( "dev.buzzert.psyterm" . into ()),
2026-11-17 00:12:24 -08:00
.. Default ::default ()
},
2026-11-17 01:08:35 -08:00
move | window , cx | {
cx . new ( | cx | terminal ::Workspace ::new ( client , updates , connection , window , cx ))
},
2026-11-17 00:12:24 -08:00
)
2026-11-17 01:08:35 -08:00
. expect ( "could not open psyTerm window" );
2026-11-17 00:12:24 -08:00
cx . activate ( true );
});
Ok (())
}