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: a stable identity, a scheme, and the selection document

RoversX5a679e8e647a
diff --git a/thinkterm-mobile/src/core.rs b/thinkterm-mobile/src/core.rs
index abb143a3..50317d13 100644
--- a/thinkterm-mobile/src/core.rs
+++ b/thinkterm-mobile/src/core.rs
@@ -39,6 +39,7 @@ pub struct ConnectParams {
pub ssh: SshParams,
pub font_paths: Vec<String>,
pub size_pt: f64,
+ pub device_id: String,
}
pub enum Cmd {
@@ -131,10 +132,22 @@ pub enum Cmd {
px: f64,
},
StepFont(f64),
+ ScreenText {
+ reply: Sender<String>,
+ },
+ SetSelection {
+ anchor: (usize, usize),
+ head: (usize, usize),
+ },
+ ClearSelection,
+ SelectedText {
+ reply: Sender<Option<String>>,
+ },
SetSetting {
key: String,
value: String,
},
+ SetPalette(Option<String>),
}
/// The surface currently lent, if any.
@@ -191,11 +204,14 @@ struct State {
fonts: Option<Rc<FontSet>>,
glyph_platform: Option<thinkterm_web::raster::Platform>,
size_pt: f64,
+ device_id: String,
conn: Conn,
status: String,
composing: bool,
/// The shell's preferences, applied to each App as it is made.
settings: Vec<(String, String)>,
+ /// The chosen scheme's colours as JSON, applied to every App made.
+ palette: Option<String>,
}
pub fn run(cmd_tx: Sender<Cmd>, rx: Receiver<Cmd>, notify: Box<dyn Notify>) {
@@ -226,10 +242,12 @@ pub fn run(cmd_tx: Sender<Cmd>, rx: Receiver<Cmd>, notify: Box<dyn Notify>) {
fonts: None,
glyph_platform: None,
size_pt: 11.0,
+ device_id: String::new(),
conn: Conn::Idle,
status: String::new(),
composing: false,
settings: Vec::new(),
+ palette: None,
};
state.notify.on_log("core thread up".into());
state.set_status("idle");
@@ -478,6 +496,28 @@ impl State {
});
}
}
+ Cmd::ScreenText { reply } => {
+ let json = self
+ .app
+ .as_ref()
+ .and_then(|app| app.screen_text())
+ .map(|t| json(&t))
+ .unwrap_or_else(|| "null".into());
+ let _ = reply.send(json);
+ }
+ Cmd::SetSelection { anchor, head } => {
+ if let Some(app) = self.app.clone() {
+ app.set_selection(anchor, head);
+ }
+ }
+ Cmd::ClearSelection => {
+ if let Some(app) = self.app.clone() {
+ app.clear_selection();
+ }
+ }
+ Cmd::SelectedText { reply } => {
+ let _ = reply.send(self.app.as_ref().and_then(|app| app.selected_text()));
+ }
Cmd::StepFont(by) => {
if let Some(app) = self.app.clone() {
app.step_font(by);
@@ -492,9 +532,29 @@ impl State {
}
}
}
+ Cmd::SetPalette(scheme) => {
+ self.palette = scheme.clone();
+ if let Some(app) = self.app.clone() {
+ Self::apply_palette(&app, &self.notify, scheme.as_deref());
+ }
+ }
}
}
+ fn apply_palette(app: &Rc<App<MobilePlatform, SshLink>>, notify: &Arc<dyn Notify>, scheme: Option<&str>) {
+ let palette = match scheme {
+ None => None,
+ Some(json) => match thinkterm_web::settings::SchemeColors::parse(json).and_then(|c| c.to_palette()) {
+ Ok(palette) => Some(palette),
+ Err(err) => {
+ notify.on_log(format!("scheme: {err}"));
+ return;
+ }
+ },
+ };
+ app.set_terminal_palette(palette);
+ }
+
fn view(&self, name: &str) -> String {
let Some(app) = &self.app else {
return "null".into();
@@ -532,6 +592,7 @@ impl State {
fn connect(&mut self, params: ConnectParams, painter: Box<dyn GlyphPainter>) -> Result<()> {
self.disconnect();
+ self.device_id = params.device_id.clone();
let mut faces = Vec::new();
for path in &params.font_paths {
let bytes = std::fs::read(path).with_context(|| format!("reading the font {path}"))?;
@@ -654,7 +715,8 @@ impl State {
return;
};
if self.gpu.is_none() {
- self.fail("attach a surface before connecting".into());
+ // Not a failure: the surface's arrival attaches.
+ self.notify.on_log("connected before the surface; attaching when it comes".into());
return;
}
let scale = self.target.as_ref().map(|t| t.scale).unwrap_or(1.0);
@@ -686,12 +748,15 @@ impl State {
self.size_pt,
size.map(|s| (s.cols, s.rows))
));
+ // The identity the server compares leases against, derived from
+ // the install's id so it survives a relaunch.
+ let (epoch, id) = stable_identity(&self.device_id);
let me = thinkterm_proto::ClientId {
hostname: "ios".into(),
username: "mobile".into(),
- pid: std::process::id(),
- epoch: self.platform.wall_ms() as u64,
- id: self.platform.random_u32() as usize,
+ pid: 0,
+ epoch,
+ id,
ssh_auth_sock: None,
};
let tx = self.cmd_tx.clone();
@@ -814,6 +879,9 @@ impl State {
self.notify.on_log(format!("setting {key}: {err}"));
}
}
+ if let Some(scheme) = self.palette.clone() {
+ Self::apply_palette(&app, &self.notify, Some(&scheme));
+ }
app.fetch_tree();
app.hide_status();
app.refresh_layout();
@@ -885,6 +953,10 @@ impl State {
if let Some(app) = self.app.clone() {
app.resize();
app.request_frame();
+ } else if matches!(self.conn, Conn::Attaching) && self.link.is_some() {
+ // The transport was up before the surface: a fast loopback
+ // dial beats the first layout. The attach waited for this.
+ self.start_attach();
} else {
self.request_frame();
}
@@ -1041,3 +1113,19 @@ fn json<T: serde::Serialize>(value: &T) -> String {
.unwrap_or_else(|err| format!("{{\"error\":{:?}}}", err.to_string()))
}
+
+/// Two numbers from the install's id (FNV-1a over it, twice), for the
+/// client identity's epoch and id fields.
+fn stable_identity(device_id: &str) -> (u64, usize) {
+ fn fnv(bytes: &[u8], seed: u64) -> u64 {
+ let mut h = seed ^ 0xcbf2_9ce4_8422_2325;
+ for b in bytes {
+ h ^= *b as u64;
+ h = h.wrapping_mul(0x0100_0000_01b3);
+ }
+ h
+ }
+ let epoch = fnv(device_id.as_bytes(), 0) & 0xffff_ffff;
+ let id = (fnv(device_id.as_bytes(), 0x9e37_79b9_7f4a_7c15) & 0x7fff_ffff) as usize;
+ (epoch, id)
+}
diff --git a/thinkterm-mobile/src/lib.rs b/thinkterm-mobile/src/lib.rs
index 35d1c8f8..369d0030 100644
--- a/thinkterm-mobile/src/lib.rs
+++ b/thinkterm-mobile/src/lib.rs
@@ -51,6 +51,9 @@ pub trait Notify: Send + Sync {
/// The host's key fingerprint, seen before authentication: the shell
/// remembers it and passes it back as `known_host` next time.
fn on_host_key(&self, fingerprint: String);
+ /// Something the App publishes for the display layer, keyed: "bg" is
+ /// the terminal's background as a hex colour, for the chrome around it.
+ fn on_published(&self, key: String, value: String);
}
/// Rust's `log` output goes to stdout, which the simulator's console shows;
@@ -172,7 +175,9 @@ impl Core {
/// `auth_kind` is "key-path" (the secret is a file path), "key" (the
/// secret is the private key's text, `passphrase` its passphrase) or
/// "password". `known_host` is the fingerprint remembered from an
- /// earlier connection, if any.
+ /// earlier connection, if any. `device_id` names this install to the
+ /// server, the same every launch: a tab this phone held is still its
+ /// own after a relaunch, rather than "another device's".
#[allow(clippy::too_many_arguments)]
pub fn connect(
&self,
@@ -184,6 +189,7 @@ impl Core {
passphrase: Option<String>,
known_host: Option<String>,
remote_command: String,
+ device_id: String,
font_paths: Vec<String>,
size_pt: f64,
painter: Box<dyn painter::GlyphPainter>,
@@ -208,6 +214,7 @@ impl Core {
},
font_paths,
size_pt,
+ device_id,
},
painter,
});
@@ -299,11 +306,49 @@ impl Core {
let _ = self.tx.send(core::Cmd::SetSetting { key, value });
}
+ /// The terminal's colour scheme: one entry of `schemes.json` as JSON
+ /// (`{"foreground":"#...","background":"#...","ansi":[...],...}`), or
+ /// none to follow the host's own scheme. Kept like a setting.
+ pub fn set_palette(&self, scheme: Option<String>) {
+ let _ = self.tx.send(core::Cmd::SetPalette(scheme));
+ }
+
/// Scale the focused pane's font by a tenth per step; 0 resets.
pub fn step_font(&self, by: f64) {
let _ = self.tx.send(core::Cmd::StepFont(by));
}
+ /// The focused pane's visible rows as a document for the platform's
+ /// text system, as JSON (`App::screen_text`); "null" without a pane.
+ pub fn screen_text(&self) -> String {
+ let (reply, ack) = mpsc::channel();
+ if self.tx.send(core::Cmd::ScreenText { reply }).is_err() {
+ return "null".into();
+ }
+ ack.recv().unwrap_or_else(|_| "null".into())
+ }
+
+ /// Select between two visible cells (row, col) of the focused pane.
+ pub fn set_selection(&self, anchor_row: u32, anchor_col: u32, head_row: u32, head_col: u32) {
+ let _ = self.tx.send(core::Cmd::SetSelection {
+ anchor: (anchor_row as usize, anchor_col as usize),
+ head: (head_row as usize, head_col as usize),
+ });
+ }
+
+ pub fn clear_selection(&self) {
+ let _ = self.tx.send(core::Cmd::ClearSelection);
+ }
+
+ /// The selected text of the focused pane, for the clipboard.
+ pub fn selected_text(&self) -> Option<String> {
+ let (reply, ack) = mpsc::channel();
+ if self.tx.send(core::Cmd::SelectedText { reply }).is_err() {
+ return None;
+ }
+ ack.recv().unwrap_or(None)
+ }
+
pub fn disconnect(&self) {
let _ = self.tx.send(core::Cmd::Disconnect);
}
diff --git a/thinkterm-mobile/src/platform.rs b/thinkterm-mobile/src/platform.rs
index ff163a71..e0c6b2d2 100644
--- a/thinkterm-mobile/src/platform.rs
+++ b/thinkterm-mobile/src/platform.rs
@@ -222,5 +222,7 @@ impl Platform for MobilePlatform {
.on_ime_anchor(anchor.left, anchor.top, anchor.width, anchor.height);
}
- fn publish(&self, _key: &str, _value: &str) {}
+ fn publish(&self, key: &str, value: &str) {
+ self.notify.on_published(key.to_string(), value.to_string());
+ }
}