Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

mobile: ask the login shell where the host's ThinkTerm is

RoversX3d5356f37d22
diff --git a/thinkterm-mobile/src/core.rs b/thinkterm-mobile/src/core.rs
index 4fce97c3..c7102960 100644
--- a/thinkterm-mobile/src/core.rs
+++ b/thinkterm-mobile/src/core.rs
@@ -861,8 +861,14 @@ impl State {
Ok(attached) => attached,
Err(err) => {
self.fail(format!("attach: {err}"));
- self.conn = Conn::Disconnected(err.clone());
- self.set_status(&format!("attach failed: {err}"));
+ // A link that closed under the handshake already said why;
+ // that reason beats "closed before the server answered".
+ let reason = match &self.conn {
+ Conn::Disconnected(reason) => reason.clone(),
+ _ => err.clone(),
+ };
+ self.conn = Conn::Disconnected(reason.clone());
+ self.set_status(&format!("failed: {reason}"));
if let Some(link) = self.link.take() {
link.shutdown();
}
diff --git a/thinkterm-mobile/src/ssh.rs b/thinkterm-mobile/src/ssh.rs
index c4020ca8..ad9b9390 100644
--- a/thinkterm-mobile/src/ssh.rs
+++ b/thinkterm-mobile/src/ssh.rs
@@ -86,10 +86,67 @@ impl Handler for HostKeyCheck {
}
}
-const DEFAULT_COMMAND: &str = "if [ -x \"$HOME/.local/bin/thinkterm\" ]; \
- then exec \"$HOME/.local/bin/thinkterm\" cli --prefer-mux proxy; \
- elif command -v thinkterm >/dev/null 2>&1; \
- then exec thinkterm cli --prefer-mux proxy; else exec wezterm cli --prefer-mux proxy; fi";
+/// Asked of the user's login shell: where the host keeps its ThinkTerm.
+/// An exec session runs with the daemon's bare PATH (on macOS without
+/// /usr/local/bin, where the desktop's CLI lives); the login shell's
+/// profile sets the user's own. The usual places come after, for a
+/// profile that sets nothing. Prints the path, or exits 127.
+const PROBE: &str = "for b in thinkterm wezterm; do \
+ p=$(command -v \"$b\" 2>/dev/null) && { printf %s\\\\n \"$p\"; exit 0; }; done; \
+ for p in \"$HOME/.local/bin/thinkterm\" /usr/local/bin/thinkterm /opt/homebrew/bin/thinkterm \
+ /Applications/ThinkTerm.app/Contents/MacOS/thinkterm; do \
+ [ -x \"$p\" ] && { printf %s\\\\n \"$p\"; exit 0; }; done; exit 127";
+
+/// A word for the remote shell, whatever is in it.
+fn shell_quote(word: &str) -> String {
+ format!("'{}'", word.replace('\'', "'\\''"))
+}
+
+/// The CLI's path on the host, from the probe. Only the probe runs in
+/// the login shell: a profile may print, and on the channel that
+/// carries the protocol that would be corruption. The last line that
+/// looks like a path is the answer, whatever came before it.
+async fn find_cli(session: &client::Handle<HostKeyCheck>) -> Result<String> {
+ let mut probe = bounded("opening the probe channel", session.channel_open_session()).await?;
+ let command = format!("exec \"${{SHELL:-/bin/sh}}\" -lc '{PROBE}'");
+ bounded("probing for thinkterm", probe.exec(true, command)).await?;
+ let (mut out, mut err, mut status) = (Vec::new(), String::new(), None);
+ loop {
+ match tokio::time::timeout(STEP, probe.wait()).await {
+ Ok(Some(ChannelMsg::Data { data })) => out.extend_from_slice(&data),
+ Ok(Some(ChannelMsg::ExtendedData { data, .. })) => {
+ err.push_str(&String::from_utf8_lossy(&data));
+ }
+ Ok(Some(ChannelMsg::ExitStatus { exit_status })) => status = Some(exit_status),
+ Ok(Some(ChannelMsg::Close)) | Ok(None) => break,
+ Ok(Some(_)) => {}
+ Err(_) => anyhow::bail!("probing for thinkterm timed out"),
+ }
+ }
+ let text = String::from_utf8_lossy(&out);
+ let found = text
+ .lines()
+ .rev()
+ .map(str::trim)
+ .find(|l| l.starts_with('/') && (l.ends_with("/thinkterm") || l.ends_with("/wezterm")));
+ match found {
+ Some(path) => Ok(path.to_string()),
+ None => {
+ let err = err.trim();
+ let detail = if err.is_empty() {
+ match status {
+ Some(127) | None => String::new(),
+ Some(code) => format!(" (the shell exited with {code})"),
+ }
+ } else {
+ format!(": {}", err.lines().last().unwrap_or(""))
+ };
+ anyhow::bail!(
+ "no thinkterm on the host{detail}. Install it there, or set the host's remote command"
+ )
+ }
+ }
+}
/// Start the connection on its own thread. Returns the sender for bytes
/// to write; dropping it (or sending `Out::Close`) ends the session.
@@ -204,10 +261,12 @@ async fn run(
let mut channel = bounded("opening a session channel", session.channel_open_session()).await?;
let command = if params.remote_command.trim().is_empty() {
- DEFAULT_COMMAND
+ let cli = find_cli(&session).await?;
+ format!("exec {} cli --prefer-mux proxy", shell_quote(&cli))
} else {
- params.remote_command.as_str()
+ params.remote_command.clone()
};
+ let command = command.as_str();
bounded("exec", channel.exec(true, command)).await?;
deliver(Net::Connected);
@@ -230,6 +289,9 @@ async fn run(
let _ = writer.close().await;
Ok::<(), anyhow::Error>(())
});
+ // What the command last said and how it ended: the reason a session
+ // that dies at once gives, instead of a bare "ended".
+ let (mut last_err, mut exit) = (String::new(), None);
loop {
tokio::select! {
sent = &mut sender => {
@@ -245,13 +307,27 @@ async fn run(
msg = reader.wait() => match msg {
Some(ChannelMsg::Data { data }) => deliver(Net::Data(data.to_vec())),
Some(ChannelMsg::ExtendedData { data, .. }) => {
- deliver(Net::Stderr(String::from_utf8_lossy(&data).to_string()))
+ let text = String::from_utf8_lossy(&data).to_string();
+ if let Some(line) = text.lines().rev().find(|l| !l.trim().is_empty()) {
+ last_err = line.trim().to_string();
+ }
+ deliver(Net::Stderr(text))
+ }
+ Some(ChannelMsg::ExitStatus { exit_status }) => {
+ exit = Some(exit_status);
+ deliver(Net::Exit(exit_status))
}
- Some(ChannelMsg::ExitStatus { exit_status }) => deliver(Net::Exit(exit_status)),
Some(ChannelMsg::Eof) => {}
Some(ChannelMsg::Close) | None => {
sender.abort();
- return Ok("the remote command ended".into());
+ let mut reason = String::from("the remote command ended");
+ if let Some(code) = exit.filter(|c| *c != 0) {
+ reason.push_str(&format!(" with status {code}"));
+ }
+ if !last_err.is_empty() {
+ reason.push_str(&format!(": {last_err}"));
+ }
+ return Ok(reason);
}
Some(_) => {}
},