diff --git a/CHANGELOG.md b/CHANGELOG.md index f693887..6c33dbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,23 @@ The version on `main` is always the next release. The first change to what ships `Cargo.toml`, `Cargo.lock`) after a release bumps it one step and opens its section here, and later changes add to that section until it ships; `scripts/version-check.py` holds them to it. +## [0.1.6] + +### Added + +- On macOS, `screenshot` with `mode=window` captures one window, even one on another desktop. It + matches the window's title first, then its app's name, and with no title it takes the frontmost + window. A miss lists the windows it saw. + +### Fixed + +- On Windows, a pasted line break no longer sends the message. The console types a paste out one + key at a time, so luxe gathers a burst of keys back into one paste. +- On a scaled Windows display, `screenshot` captures the whole screen instead of its top-left + corner. +- On a Mac with fontconfig installed, the OpenAI and Claude marks fall back to the robot and cloud + when no font has them, instead of drawing a box with a question mark. + ## [0.1.5] ### Added diff --git a/Cargo.lock b/Cargo.lock index e0d6bac..9c76965 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1690,7 +1690,7 @@ checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" [[package]] name = "luxe-cli" -version = "0.1.5" +version = "0.1.6" dependencies = [ "anyhow", "async-trait", @@ -1723,7 +1723,7 @@ dependencies = [ [[package]] name = "luxe-core" -version = "0.1.5" +version = "0.1.6" dependencies = [ "async-trait", "base16ct", @@ -1754,7 +1754,7 @@ dependencies = [ [[package]] name = "luxe-dap" -version = "0.1.5" +version = "0.1.6" dependencies = [ "async-trait", "luxe-core", @@ -1768,7 +1768,7 @@ dependencies = [ [[package]] name = "luxe-lsp" -version = "0.1.5" +version = "0.1.6" dependencies = [ "async-trait", "base16ct", @@ -1792,7 +1792,7 @@ dependencies = [ [[package]] name = "luxe-mcp" -version = "0.1.5" +version = "0.1.6" dependencies = [ "async-trait", "futures-util", @@ -1812,7 +1812,7 @@ dependencies = [ [[package]] name = "luxe-providers" -version = "0.1.5" +version = "0.1.6" dependencies = [ "async-trait", "base64", @@ -1831,7 +1831,7 @@ dependencies = [ [[package]] name = "luxe-remote" -version = "0.1.5" +version = "0.1.6" dependencies = [ "axum", "base16ct", @@ -1854,7 +1854,7 @@ dependencies = [ [[package]] name = "luxe-tools" -version = "0.1.5" +version = "0.1.6" dependencies = [ "anyhow", "async-trait", @@ -1880,7 +1880,7 @@ dependencies = [ [[package]] name = "luxe-tui" -version = "0.1.5" +version = "0.1.6" dependencies = [ "anyhow", "arboard", diff --git a/Cargo.toml b/Cargo.toml index ebaee60..e9833ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = ["crates/*"] # Every crate here releases together from one workspace, so the versions move in lockstep and each # crate inherits this one. The internal `path` dependencies carry the same number explicitly — cargo # uses the path locally and the version on crates.io, and refuses to publish a path dep without one. -version = "0.1.5" +version = "0.1.6" edition = "2021" license = "MIT OR Apache-2.0" repository = "https://github.com/evanwhittaker/luxe" diff --git a/crates/luxe-cli/Cargo.toml b/crates/luxe-cli/Cargo.toml index 87da841..fd4e0c6 100644 --- a/crates/luxe-cli/Cargo.toml +++ b/crates/luxe-cli/Cargo.toml @@ -18,14 +18,14 @@ name = "luxe" path = "src/main.rs" [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } -luxe-providers = { path = "../luxe-providers", version = "0.1.5" } -luxe-tools = { path = "../luxe-tools", version = "0.1.5" } -luxe-lsp = { path = "../luxe-lsp", version = "0.1.5" } -luxe-dap = { path = "../luxe-dap", version = "0.1.5" } -luxe-mcp = { path = "../luxe-mcp", version = "0.1.5" } -luxe-tui = { path = "../luxe-tui", version = "0.1.5" } -luxe-remote = { path = "../luxe-remote", version = "0.1.5", optional = true } +luxe-core = { path = "../luxe-core", version = "0.1.6" } +luxe-providers = { path = "../luxe-providers", version = "0.1.6" } +luxe-tools = { path = "../luxe-tools", version = "0.1.6" } +luxe-lsp = { path = "../luxe-lsp", version = "0.1.6" } +luxe-dap = { path = "../luxe-dap", version = "0.1.6" } +luxe-mcp = { path = "../luxe-mcp", version = "0.1.6" } +luxe-tui = { path = "../luxe-tui", version = "0.1.6" } +luxe-remote = { path = "../luxe-remote", version = "0.1.6", optional = true } # The pairing QR in the terminal. Without its defaults it is the encoder and text renderer alone, # with no dependencies. qrcode = { version = "0.14", default-features = false, optional = true } diff --git a/crates/luxe-cli/src/guide.md b/crates/luxe-cli/src/guide.md index f87c237..d5add94 100644 --- a/crates/luxe-cli/src/guide.md +++ b/crates/luxe-cli/src/guide.md @@ -4676,8 +4676,9 @@ needed, such as a passwordless `sudo` rule for one program: The `screenshot` tool's `url` mode renders a web page and captures only that. Its `fullscreen`, `region` and `window` modes capture whatever is on your screen, and the image goes to your model -provider, so each **asks** first, and runs with nobody watching refuse them. To turn screen capture -off entirely: +provider, so each **asks** first, and runs with nobody watching refuse them. On macOS, `window` can +also capture a window on another desktop, and when no window matches it tells the model the titles +of the windows it saw. To turn screen capture off entirely: ```json title="~/.config/luxe/permissions.json" { "permission": { "screenshot": { "fullscreen": "deny", "region": "deny", "window": "deny" } } } diff --git a/crates/luxe-dap/Cargo.toml b/crates/luxe-dap/Cargo.toml index 1addc87..eff339b 100644 --- a/crates/luxe-dap/Cargo.toml +++ b/crates/luxe-dap/Cargo.toml @@ -11,7 +11,7 @@ homepage.workspace = true # Provides a native Debug Adapter Protocol client + the `debug` tool. [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-lsp/Cargo.toml b/crates/luxe-lsp/Cargo.toml index a3f9cd8..f4c7966 100644 --- a/crates/luxe-lsp/Cargo.toml +++ b/crates/luxe-lsp/Cargo.toml @@ -11,7 +11,7 @@ homepage.workspace = true # Provides a generic LSP client (rust-analyzer first). [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-mcp/Cargo.toml b/crates/luxe-mcp/Cargo.toml index 12f8afc..1f84fd1 100644 --- a/crates/luxe-mcp/Cargo.toml +++ b/crates/luxe-mcp/Cargo.toml @@ -13,7 +13,7 @@ homepage.workspace = true # no-HTTP rule forbids in core itself. The low-level stdio transport stays in luxe-core. [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-providers/Cargo.toml b/crates/luxe-providers/Cargo.toml index 4ce25de..af5cf69 100644 --- a/crates/luxe-providers/Cargo.toml +++ b/crates/luxe-providers/Cargo.toml @@ -11,7 +11,7 @@ homepage.workspace = true # Provides LLM provider adapters (OpenAI-compat, Anthropic, Gemini). [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-remote/Cargo.toml b/crates/luxe-remote/Cargo.toml index c3c4c6f..8a7cd72 100644 --- a/crates/luxe-remote/Cargo.toml +++ b/crates/luxe-remote/Cargo.toml @@ -18,7 +18,7 @@ exclude = ["app-tests/"] # reqwest and the workspace's rustls. [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-tools/Cargo.toml b/crates/luxe-tools/Cargo.toml index 4f2d77b..4403180 100644 --- a/crates/luxe-tools/Cargo.toml +++ b/crates/luxe-tools/Cargo.toml @@ -11,7 +11,7 @@ homepage.workspace = true # Provides tool implementations (read, write, edit, bash, grep, glob, etc.). [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } thiserror = { workspace = true } tracing = { workspace = true } serde = { workspace = true } diff --git a/crates/luxe-tools/src/screenshot.rs b/crates/luxe-tools/src/screenshot.rs index 87bbcb3..620dd2f 100644 --- a/crates/luxe-tools/src/screenshot.rs +++ b/crates/luxe-tools/src/screenshot.rs @@ -75,7 +75,7 @@ impl Tool for ScreenshotTool { }, "window": { "type": "string", - "description": "window mode: case-sensitive title substring; omit for the active window (Hyprland/X11 only)" + "description": "window mode: case-sensitive title substring; omit for the active window. Hyprland, X11 and macOS only; on macOS this also captures a window on another desktop, matching the title or the app's name" }, "url": { "type": "string", diff --git a/crates/luxe-tools/src/screenshot/capture.rs b/crates/luxe-tools/src/screenshot/capture.rs index 7d85930..c106202 100644 --- a/crates/luxe-tools/src/screenshot/capture.rs +++ b/crates/luxe-tools/src/screenshot/capture.rs @@ -2,8 +2,10 @@ //! //! Shells out to the platform's native tool rather than linking a capture crate, keeping the //! dependency surface tiny and the gate clean: grim (+ hyprctl) on Wayland, maim/scrot/ImageMagick -//! on X11, `screencapture` on macOS, and PowerShell + System.Drawing on Windows. Each writes a PNG -//! that the tool then reads and returns inline. +//! on X11, `screencapture` (plus `osascript` to find windows) on macOS, and PowerShell + +//! System.Drawing on Windows. Each writes a PNG that the tool then reads and returns inline. + +mod macos; use anyhow::{anyhow, bail, Context, Result}; use std::path::Path; @@ -282,9 +284,7 @@ async fn capture_macos(target: &CaptureTarget, out: &Path) -> Result<()> { let rect = format!("{},{},{},{}", r.x, r.y, r.w, r.h); run("screencapture", &["-x".into(), "-R".into(), rect, out]).await } - CaptureTarget::Window { .. } => bail!( - "per-window capture on macOS needs a window id; use mode=fullscreen or mode=region" - ), + CaptureTarget::Window { title } => macos::capture_window(title.as_deref(), &out).await, } } @@ -306,6 +306,16 @@ async fn capture_windows(target: &CaptureTarget, out: &Path) -> Result<()> { .await } +/// Opts the PowerShell process out of DPI virtualization, as a script prefix. +/// +/// Without it Windows reports the scaled screen size (1371x914 on a 2400x1600 panel at 175%) and +/// `CopyFromScreen` copies only that many physical pixels, cropping the capture to the top-left. +/// It has to run before anything touches `System.Windows.Forms`, so it leads the script. Regions +/// are then in physical pixels too, which is what the image the model sees is measured in. +const WINDOWS_DPI_AWARE: &str = "Add-Type -TypeDefinition 'using System.Runtime.InteropServices; \ + public static class LuxeDpi { [DllImport(\"user32.dll\")] public static extern bool \ + SetProcessDPIAware(); }'; [void][LuxeDpi]::SetProcessDPIAware(); "; + /// Builds a PowerShell one-liner that captures the virtual screen (or `x,y,w,h`) to `out`. fn windows_capture_script(rect: &str, out: &str) -> String { let bounds = if rect == "virtual" { @@ -321,7 +331,7 @@ fn windows_capture_script(rect: &str, out: &str) -> String { ) }; format!( - "Add-Type -AssemblyName System.Windows.Forms,System.Drawing; {bounds}; \ + "{WINDOWS_DPI_AWARE}Add-Type -AssemblyName System.Windows.Forms,System.Drawing; {bounds}; \ $bmp=New-Object System.Drawing.Bitmap($r.Width,$r.Height); \ $g=[System.Drawing.Graphics]::FromImage($bmp); \ $g.CopyFromScreen($r.Left,$r.Top,0,0,$bmp.Size); \ @@ -439,6 +449,17 @@ mod tests { assert!(!which("definitely-not-a-real-binary-xyzzy")); } + /// Reading the screen size before opting into DPI awareness is what cropped Windows captures. + #[test] + fn windows_script_turns_on_dpi_awareness_before_reading_the_screen() { + for rect in ["virtual", "0,0,800,600"] { + let script = windows_capture_script(rect, "out.png"); + let aware = script.find("SetProcessDPIAware();").expect("calls SetProcessDPIAware"); + let forms = script.find("System.Windows.Forms").expect("loads Windows.Forms"); + assert!(aware < forms, "{rect}: DPI awareness must come first: {script}"); + } + } + #[test] fn backend_label_is_stable() { assert_eq!(backend_label(DisplayBackend::X11), "X11"); diff --git a/crates/luxe-tools/src/screenshot/capture/macos.rs b/crates/luxe-tools/src/screenshot/capture/macos.rs new file mode 100644 index 0000000..80ab0d2 --- /dev/null +++ b/crates/luxe-tools/src/screenshot/capture/macos.rs @@ -0,0 +1,214 @@ +//! macOS window capture — finds a window on any desktop and hands its id to `screencapture -l`. +//! +//! `screencapture` takes a window by id and can photograph one on a desktop (Space) that isn't the +//! one on screen, which a fullscreen or region capture never can. Ids come from CoreGraphics' +//! window list, read through `osascript`'s JavaScript so nothing beyond the OS is needed. + +use anyhow::{anyhow, bail, Context, Result}; +use serde::Deserialize; +use tokio::process::Command; + +/// Lists every window on every desktop as JSON, front to back. +/// +/// `kCGWindowListOptionAll` rather than on-screen only: that is what makes a window on another +/// desktop visible to us at all. +const LIST_WINDOWS_SCRIPT: &str = r#" +ObjC.import('CoreGraphics'); +const raw = $.CGWindowListCopyWindowInfo($.kCGWindowListOptionAll, 0); +const list = ObjC.deepUnwrap(ObjC.castRefToObject(raw)); +JSON.stringify(list.map(w => ({ + id: w.kCGWindowNumber, owner: w.kCGWindowOwnerName || '', name: w.kCGWindowName || '', + layer: w.kCGWindowLayer, w: w.kCGWindowBounds.Width, h: w.kCGWindowBounds.Height, + on: !!w.kCGWindowIsOnscreen }))); +"#; + +/// How long the window list may take; it answers in about a tenth of a second. +const LIST_DEADLINE: std::time::Duration = std::time::Duration::from_secs(10); + +/// Windows narrower or shorter than this are the helper strips and offscreen buffers apps keep +/// around (a terminal alone leaves ten), not something a person means by "that window". +const MIN_SIDE: f64 = 100.0; + +/// How many window names an unmatched search reports back. +const SHOWN_ON_MISS: usize = 12; + +/// One entry of the window list. +#[derive(Debug, Clone, Deserialize, PartialEq)] +struct WindowInfo { + id: u64, + owner: String, + name: String, + layer: i64, + w: f64, + h: f64, + on: bool, +} + +impl WindowInfo { + /// A normal, sizeable application window: layer 0 is where apps draw, and the menu bar, the + /// Dock and overlays sit above it. + fn is_usable(&self) -> bool { + self.layer == 0 && self.w >= MIN_SIDE && self.h >= MIN_SIDE + } + + fn label(&self) -> String { + match self.name.is_empty() { + true => self.owner.clone(), + false => format!("{}: {}", self.owner, self.name), + } + } +} + +/// Captures a window to `out`, wherever its desktop is. `title` matches a window's title, or +/// failing that its app's name; `None` takes the frontmost window on screen. +pub(super) async fn capture_window(title: Option<&str>, out: &str) -> Result<()> { + let id = pick_window(&list_windows().await?, title)?; + let args = ["-x", "-o", "-l", &id.to_string(), out].map(String::from); + super::run("screencapture", &args).await +} + +async fn list_windows() -> Result> { + let child = Command::new("osascript") + .args(["-l", "JavaScript", "-e", LIST_WINDOWS_SCRIPT]) + .kill_on_drop(true) + .stdin(std::process::Stdio::null()) + .output(); + let output = tokio::time::timeout(LIST_DEADLINE, child) + .await + .map_err(|_| { + anyhow!("`osascript` did not list windows within {}s", LIST_DEADLINE.as_secs()) + })? + .context("failed to run `osascript` to list windows")?; + if !output.status.success() { + bail!("listing windows failed: {}", String::from_utf8_lossy(&output.stderr).trim()); + } + serde_json::from_slice(&output.stdout).context("could not parse the window list") +} + +/// Chooses the window to capture. A title match beats an app-name match; among matches an +/// on-screen window beats a hidden one and a bigger beats a smaller. +fn pick_window(windows: &[WindowInfo], title: Option<&str>) -> Result { + let usable = || windows.iter().filter(|window| window.is_usable()); + let Some(needle) = title else { + let frontmost = usable().find(|window| window.on); + return frontmost.map(|window| window.id).ok_or_else(|| anyhow!("no window is on screen")); + }; + let titled = best(usable().filter(|window| window.name.contains(needle))); + let by_app = || best(usable().filter(|window| window.owner.contains(needle))); + titled.or_else(by_app).ok_or_else(|| no_match(windows, needle)) +} + +fn best<'a>(matches: impl Iterator) -> Option { + matches.max_by_key(|window| (window.on, (window.w * window.h) as u64)).map(|window| window.id) +} + +fn no_match(windows: &[WindowInfo], needle: &str) -> anyhow::Error { + // Titled windows are what a search can name; with titles hidden, the apps are all there is + let usable = || windows.iter().filter(|w| w.is_usable()); + let mut shown: Vec<&WindowInfo> = usable().filter(|w| !w.name.is_empty()).collect(); + if shown.is_empty() { + shown = usable().collect(); + } + let labels: Vec = + shown.into_iter().take(SHOWN_ON_MISS).map(WindowInfo::label).collect(); + anyhow!( + "no window whose title or app contains '{needle}' (case-sensitive). Windows seen: {}. \ + Titles of other apps' windows need Screen Recording permission for the terminal.", + labels.join(", ") + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn window(id: u64, owner: &str, name: &str, size: (f64, f64), on: bool) -> WindowInfo { + let (w, h) = size; + WindowInfo { id, owner: owner.into(), name: name.into(), layer: 0, w, h, on } + } + + /// The Ghostty entries from a real listing: one real window among its helper strips. + fn ghostty_with_helpers(real_is_on_screen: bool) -> Vec { + vec![ + window(102536, "Ghostty", "", (500.0, 500.0), false), + window(102535, "Ghostty", "luxe", (2539.0, 1391.0), real_is_on_screen), + window(102534, "Ghostty", "", (3840.0, 30.0), false), + window(102530, "Ghostty", "", (1728.0, 33.0), false), + ] + } + + /// Copied from `osascript -l JavaScript` output on macOS, so a renamed key fails here. + #[test] + fn the_window_list_parses_as_the_script_prints_it() { + let line = r#"[{"id":102535,"owner":"Ghostty","name":"luxe","layer":0,"w":2539,"h":1391,"on":true}]"#; + let parsed: Vec = serde_json::from_str(line).expect("parses"); + assert_eq!(parsed, vec![window(102535, "Ghostty", "luxe", (2539.0, 1391.0), true)]); + } + + /// Catches a title match falling through to the app name, which would also hit the helpers. + #[test] + fn a_title_picks_that_window() { + assert_eq!(pick_window(&ghostty_with_helpers(true), Some("lux")).expect("found"), 102535); + } + + /// A window on another desktop is not on screen; it must still be found. + #[test] + fn a_window_on_another_desktop_is_found() { + assert_eq!(pick_window(&ghostty_with_helpers(false), Some("luxe")).expect("found"), 102535); + } + + /// Two windows share a title: the one in front of the reader wins over a bigger one hidden away. + #[test] + fn an_on_screen_window_beats_a_bigger_hidden_one() { + let windows = vec![ + window(1, "Ghostty", "luxe", (3000.0, 2000.0), false), + window(2, "Ghostty", "luxe", (1000.0, 700.0), true), + ]; + assert_eq!(pick_window(&windows, Some("luxe")).expect("found"), 2); + } + + /// Without a title permission every name is empty; the app name still finds the big window, + /// not the 500 by 500 offscreen buffer or the thin strips. + #[test] + fn an_app_name_picks_its_biggest_window_when_titles_are_hidden() { + let mut windows = ghostty_with_helpers(false); + windows[1].name.clear(); + assert_eq!(pick_window(&windows, Some("Ghostty")).expect("found"), 102535); + } + + /// A title beats an app match even when the app's other window is on screen and bigger. + #[test] + fn a_title_match_beats_an_app_match() { + let windows = vec![ + window(1, "Safari", "docs", (1200.0, 800.0), true), + window(2, "Ghostty", "docs", (900.0, 600.0), false), + window(3, "docs", "", (2000.0, 1500.0), true), + ]; + assert_eq!(pick_window(&windows, Some("docs")).expect("found"), 1); + } + + #[test] + fn matching_is_case_sensitive_and_a_miss_lists_what_exists() { + let message = pick_window(&ghostty_with_helpers(true), Some("LUXE")) + .expect_err("no match") + .to_string(); + assert!(message.contains("'LUXE'"), "{message}"); + assert!(message.contains("Ghostty: luxe"), "{message}"); + } + + /// No title means the frontmost window on screen: the first usable one, in front-to-back order. + #[test] + fn no_title_takes_the_frontmost_window_on_screen() { + let mut menu_bar = window(9, "Window Server", "Menubar", (3840.0, 30.0), true); + menu_bar.layer = 24; + let windows = vec![ + menu_bar, + window(8, "Ghostty", "", (3840.0, 30.0), true), + window(7, "Safari", "docs", (1200.0, 800.0), false), + window(6, "Ghostty", "luxe", (2539.0, 1391.0), true), + window(5, "Notes", "todo", (800.0, 600.0), true), + ]; + assert_eq!(pick_window(&windows, None).expect("found"), 6); + assert!(pick_window(&windows[..3], None).is_err(), "nothing usable is on screen"); + } +} diff --git a/crates/luxe-tui/Cargo.toml b/crates/luxe-tui/Cargo.toml index bd1807f..7f1701f 100644 --- a/crates/luxe-tui/Cargo.toml +++ b/crates/luxe-tui/Cargo.toml @@ -8,7 +8,7 @@ repository.workspace = true homepage.workspace = true [dependencies] -luxe-core = { path = "../luxe-core", version = "0.1.5" } +luxe-core = { path = "../luxe-core", version = "0.1.6" } # `unstable-rendered-line-info` enables Paragraph::line_count, used for precise wrapping-aware # mouse hit-testing over word-wrapped surfaces (guide/dock/conversation clicks). It adds no crate; # the API is marked unstable upstream but is stable within our pinned minor (re-check on upgrade). diff --git a/crates/luxe-tui/src/glyphs/detect.rs b/crates/luxe-tui/src/glyphs/detect.rs index 7a64780..49288be 100644 --- a/crates/luxe-tui/src/glyphs/detect.rs +++ b/crates/luxe-tui/src/glyphs/detect.rs @@ -73,7 +73,18 @@ fn fontconfig_has_all(wanted: impl IntoIterator) -> bool { .args([charset.as_str(), "family"]) .stderr(std::process::Stdio::null()) .output() - .is_ok_and(|probe| probe.status.success() && !probe.stdout.is_empty()) + .is_ok_and(|probe| probe.status.success() && names_a_real_font(&probe.stdout)) +} + +/// Whether `fc-list`'s family listing names a font a reader could have chosen. +/// +/// macOS lists `.LastResort`, whose job is to draw a placeholder box for every codepoint, so it +/// answers yes to any charset question and the probe would never say no. Fonts the system hides +/// from its pickers carry a leading dot; none of them is a face a terminal is set to. +fn names_a_real_font(listing: &[u8]) -> bool { + String::from_utf8_lossy(listing) + .lines() + .any(|family| !family.trim().is_empty() && !family.starts_with('.')) } /// Whether the Material icons can actually be drawn on this machine. @@ -140,3 +151,25 @@ fn is_kitty() -> bool { let multiplexed = term.starts_with("tmux") || term.starts_with("screen"); multiplexed && std::env::var_os("KITTY_WINDOW_ID").is_some() } + +#[cfg(test)] +mod probe_tests { + use super::names_a_real_font; + + /// Copied from `fc-list ":charset=ec82 ec81 ec1e" family` on a Mac with Nerd Fonts older than 3.5. + #[test] + fn the_macos_placeholder_font_is_not_a_font_with_the_glyph() { + assert!(!names_a_real_font(b".LastResort\n")); + } + + #[test] + fn a_hidden_font_beside_a_real_one_still_counts_the_real_one() { + assert!(names_a_real_font(b".LastResort\nHack Nerd Font\n")); + } + + #[test] + fn an_empty_listing_names_no_font() { + assert!(!names_a_real_font(b"")); + assert!(!names_a_real_font(b"\n")); + } +} diff --git a/crates/luxe-tui/src/main_loop.rs b/crates/luxe-tui/src/main_loop.rs index e6da09f..8f8aead 100644 --- a/crates/luxe-tui/src/main_loop.rs +++ b/crates/luxe-tui/src/main_loop.rs @@ -10,6 +10,7 @@ mod launch; mod mouse; mod pacing; mod paste; +mod paste_burst; use crate::app::{App, Msg}; use crate::workspace_layout::Pane; diff --git a/crates/luxe-tui/src/main_loop/pacing.rs b/crates/luxe-tui/src/main_loop/pacing.rs index 5defbd8..609952f 100644 --- a/crates/luxe-tui/src/main_loop/pacing.rs +++ b/crates/luxe-tui/src/main_loop/pacing.rs @@ -1,6 +1,7 @@ //! Frame pacing — how many queued events join a frame, and whether an iteration paints. -use super::keys::{apply_terminal_event, collect_exit}; +use super::keys::collect_exit; +use super::paste_burst::apply_folding_pastes; use super::TuiExit; use crate::app::{App, Msg}; use crossterm::event::{Event, EventStream}; @@ -37,7 +38,7 @@ pub(super) async fn next_event(events: &mut Option) -> Option) -> Option { +pub(super) async fn queued_event(events: Option<&mut EventStream>) -> Option { let events = events?; std::future::poll_fn(|context| { std::task::Poll::Ready(match events.poll_next_unpin(context) { @@ -80,13 +81,13 @@ pub(super) async fn apply_terminal_batch( first: Event, exit: &mut TuiExit, ) -> (bool, usize) { - let mut idle_motion = apply_terminal_event(app, first, exit); + let mut idle_motion = apply_folding_pastes(app, events, first, exit).await; let mut coalesced = 0; while may_coalesce(app, coalesced) { let Some(event) = queued_event(events.as_mut()).await else { break; }; - idle_motion &= apply_terminal_event(app, event, exit); + idle_motion &= apply_folding_pastes(app, events, event, exit).await; coalesced += 1; } (idle_motion, coalesced) diff --git a/crates/luxe-tui/src/main_loop/paste_burst.rs b/crates/luxe-tui/src/main_loop/paste_burst.rs new file mode 100644 index 0000000..3aa00d4 --- /dev/null +++ b/crates/luxe-tui/src/main_loop/paste_burst.rs @@ -0,0 +1,227 @@ +//! Paste burst — rebuilding a paste from the key presses a console delivers in its place. +//! +//! The Windows console has no bracketed paste: a paste arrives as one key press per character, and +//! every line break as an Enter, which the composer would submit. A run of text keys that lands +//! all at once is a paste, so it is folded back into one [`Event::Paste`] and meets the same code +//! a bracketed paste does, chip and all. + +use super::pacing::{next_event, queued_event}; +use crossterm::event::{Event, EventStream, KeyCode, KeyEventKind, KeyModifiers}; +use std::time::{Duration, Instant}; + +/// Text keys that must arrive together before they read as a paste instead of fast typing. +/// +/// A person cannot get this many keys into one batch, but a loop stalled on a slow frame can queue +/// a few, so the bar sits above that. +const MIN_BURST_KEYS: usize = 5; + +/// How long a paste may go quiet before it is taken as finished. +/// +/// The console hands a large paste over in chunks, and a tail that arrived alone would run its +/// last Enter as a submit. +const BURST_GAP: Duration = Duration::from_millis(30); + +/// The longest one burst holds the loop, so a huge paste cannot starve the engine's events. +const BURST_CEILING: Duration = Duration::from_secs(2); + +/// Whether this platform delivers a paste as keystrokes. +pub(super) const fn pastes_as_keystrokes() -> bool { + cfg!(windows) +} + +/// What one terminal event adds to a run of pasted text. +#[derive(Debug, PartialEq, Eq)] +enum Piece { + Text(char), + /// A key release: part of the run, but no text + Quiet, +} + +/// The text a key press stands for, or `None` when the event could not be part of a paste. +fn piece(event: &Event) -> Option { + let Event::Key(key) = event else { return None }; + if !key.modifiers.difference(KeyModifiers::SHIFT).is_empty() { + return None; + } + let character = match key.code { + KeyCode::Char(character) => character, + KeyCode::Enter => '\n', + KeyCode::Tab => '\t', + _ => return None, + }; + Some(if key.kind == KeyEventKind::Release { Piece::Quiet } else { Piece::Text(character) }) +} + +/// Consecutive text key events, kept both raw and as the text they spell. +#[derive(Default)] +struct Run { + events: Vec, + text: String, + keys: usize, +} + +impl Run { + fn push(&mut self, event: Event, piece: Piece) { + if let Piece::Text(character) = piece { + self.text.push(character); + self.keys += 1; + } + self.events.push(event); + } + + fn looks_pasted(&self) -> bool { + self.keys >= MIN_BURST_KEYS + } +} + +/// Applies `first`, folding it and the text keys behind it into one paste when they are one. +/// +/// Answers like `apply_terminal_event`: whether everything applied was idle pointer motion. +pub(super) async fn apply_folding_pastes( + app: &mut crate::app::App, + events: &mut Option, + first: Event, + exit: &mut super::TuiExit, +) -> bool { + use super::keys::apply_terminal_event; + let Some(opening) = piece(&first).filter(|_| pastes_as_keystrokes()) else { + return apply_terminal_event(app, first, exit); + }; + let (run, stopper) = gather(events, first, opening).await; + let mut idle = apply_run(app, run, exit); + if let Some(event) = stopper { + idle &= apply_terminal_event(app, event, exit); + } + idle +} + +/// Applies a run as one paste when it looks like one, and as the keys it was otherwise. +fn apply_run(app: &mut crate::app::App, run: Run, exit: &mut super::TuiExit) -> bool { + use super::keys::apply_terminal_event; + if run.looks_pasted() { + return apply_terminal_event(app, Event::Paste(run.text), exit); + } + let mut idle = true; + for event in run.events { + idle &= apply_terminal_event(app, event, exit); + } + idle +} + +/// Reads text keys until something else arrives or the run ends, returning that something. +async fn gather( + events: &mut Option, + first: Event, + opening: Piece, +) -> (Run, Option) { + let mut run = Run::default(); + run.push(first, opening); + let started = Instant::now(); + loop { + let waiting = run.looks_pasted() && started.elapsed() < BURST_CEILING; + let Some(event) = next_in_run(events, waiting).await else { return (run, None) }; + match piece(&event) { + Some(piece) => run.push(event, piece), + None => return (run, Some(event)), + } + } +} + +/// The next event: one already queued, else one that lands within the gap when `waiting`. +async fn next_in_run(events: &mut Option, waiting: bool) -> Option { + if let Some(event) = queued_event(events.as_mut()).await { + return Some(event); + } + match waiting { + true => next_within_gap(events).await, + false => None, + } +} + +/// The next event if one lands within [`BURST_GAP`]. +async fn next_within_gap(events: &mut Option) -> Option { + match tokio::time::timeout(BURST_GAP, next_event(events)).await { + Ok(Some(Ok(event))) => Some(event), + Ok(Some(Err(error))) => { + tracing::warn!(%error, "terminal read failed while gathering a paste"); + None + } + Ok(None) | Err(_) => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crossterm::event::KeyEvent; + + fn key(code: KeyCode, modifiers: KeyModifiers) -> Event { + Event::Key(KeyEvent::new(code, modifiers)) + } + + fn run_of(events: Vec) -> Run { + let mut run = Run::default(); + for event in events { + let piece = piece(&event).expect("every event in these runs is a text key"); + run.push(event, piece); + } + run + } + + fn typed(text: &str) -> Vec { + text.chars() + .map(|character| match character { + '\n' => key(KeyCode::Enter, KeyModifiers::NONE), + other => key(KeyCode::Char(other), KeyModifiers::NONE), + }) + .collect() + } + + #[test] + fn enter_becomes_a_newline_and_tab_a_tab() { + let run = run_of(typed("a\nb\tc")); + assert_eq!(run.text, "a\nb\tc"); + } + + #[test] + fn a_run_below_the_bar_is_typing() { + assert!(!run_of(typed("ab\n")).looks_pasted()); + } + + #[test] + fn a_run_at_the_bar_is_a_paste() { + assert!(run_of(typed("ab\ncd")).looks_pasted()); + } + + #[test] + fn releases_spell_nothing_and_do_not_count() { + let release = Event::Key(KeyEvent::new_with_kind( + KeyCode::Char('a'), + KeyModifiers::NONE, + KeyEventKind::Release, + )); + let mut events = typed("abcd"); + events.extend((0..10).map(|_| release.clone())); + let run = run_of(events); + assert_eq!(run.text, "abcd"); + assert!(!run.looks_pasted()); + } + + #[test] + fn a_shifted_capital_is_text() { + assert_eq!(piece(&key(KeyCode::Char('A'), KeyModifiers::SHIFT)), Some(Piece::Text('A'))); + } + + #[test] + fn a_chord_is_never_part_of_a_paste() { + assert_eq!(piece(&key(KeyCode::Char('c'), KeyModifiers::CONTROL)), None); + assert_eq!(piece(&key(KeyCode::Enter, KeyModifiers::ALT)), None); + } + + #[test] + fn navigation_and_pointer_events_end_a_run() { + assert_eq!(piece(&key(KeyCode::Up, KeyModifiers::NONE)), None); + assert_eq!(piece(&key(KeyCode::Esc, KeyModifiers::NONE)), None); + assert_eq!(piece(&Event::FocusGained), None); + } +} diff --git a/docs/guide/security.md b/docs/guide/security.md index de0ba1e..c407b3c 100644 --- a/docs/guide/security.md +++ b/docs/guide/security.md @@ -256,8 +256,9 @@ needed, such as a passwordless `sudo` rule for one program: The `screenshot` tool's `url` mode renders a web page and captures only that. Its `fullscreen`, `region` and `window` modes capture whatever is on your screen, and the image goes to your model -provider, so each **asks** first, and runs with nobody watching refuse them. To turn screen capture -off entirely: +provider, so each **asks** first, and runs with nobody watching refuse them. On macOS, `window` can +also capture a window on another desktop, and when no window matches it tells the model the titles +of the windows it saw. To turn screen capture off entirely: ```json title="~/.config/luxe/permissions.json" { "permission": { "screenshot": { "fullscreen": "deny", "region": "deny", "window": "deny" } } }