From edb0cc9773be1a3cda7da628f84a5c1d0570b490 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Thu, 5 Nov 2026 00:34:08 +0100 Subject: [PATCH] refactor(cli-rs): share the TS QR encoder with the pair dialog The TUI /pair dialog drew its code with the qrcode crate, which picks other masks than packages/util/src/qr.ts. Move the C.9 port to opencode-ui so both use it, and drop the qrcode dependency. --- packages/cli-rs/Cargo.lock | 7 --- packages/cli-rs/Cargo.toml | 1 - .../cli-rs/crates/cli/src/commands/pair.rs | 3 +- packages/cli-rs/crates/tui/Cargo.toml | 1 - .../cli-rs/crates/tui/src/dialogs/pair.rs | 45 +------------------ packages/cli-rs/crates/ui/src/lib.rs | 1 + .../{cli/src/commands/pair => ui/src}/qr.rs | 5 ++- 7 files changed, 6 insertions(+), 57 deletions(-) rename packages/cli-rs/crates/{cli/src/commands/pair => ui/src}/qr.rs (98%) diff --git a/packages/cli-rs/Cargo.lock b/packages/cli-rs/Cargo.lock index 23f75f01d30..d30196deeb9 100644 --- a/packages/cli-rs/Cargo.lock +++ b/packages/cli-rs/Cargo.lock @@ -1311,7 +1311,6 @@ dependencies = [ "opencode-store", "opencode-ui", "opentui", - "qrcode", "regex", "rusqlite", "serde", @@ -1439,12 +1438,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "qrcode" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec" - [[package]] name = "quinn" version = "0.11.12" diff --git a/packages/cli-rs/Cargo.toml b/packages/cli-rs/Cargo.toml index c6822d6b379..cc5839c1cbf 100644 --- a/packages/cli-rs/Cargo.toml +++ b/packages/cli-rs/Cargo.toml @@ -44,7 +44,6 @@ libc = "0.2" log = { version = "0.4", features = ["std"] } notify = "8" percent-encoding = "2" -qrcode = { version = "0.14", default-features = false } rand = "0.10" regex = "1" reqwest = { version = "0.13", default-features = false, features = ["rustls", "json", "stream", "http2"] } diff --git a/packages/cli-rs/crates/cli/src/commands/pair.rs b/packages/cli-rs/crates/cli/src/commands/pair.rs index d66825e5ed7..694d44f1da8 100644 --- a/packages/cli-rs/crates/cli/src/commands/pair.rs +++ b/packages/cli-rs/crates/cli/src/commands/pair.rs @@ -10,6 +10,7 @@ use opencode_api::client::{Client, RequestOptions}; use opencode_local::service_config::Info; use opencode_service::delegate; use opencode_service::ts_command::TsCommand; +use opencode_ui::qr; use super::Context; use super::client::block_on; @@ -18,8 +19,6 @@ use crate::args::PairArgs; use crate::output::{Exit, print}; use crate::prompt; -mod qr; - const NOT_ENABLED: &str = "Remote access was not enabled. To pair locally, run `opencode pair`."; pub const REMOTE_WARNING: &str = "This will expose your OpenCode server to the internet.\nTraffic is end-to-end encrypted, and access requires your server password.\nTo pair locally instead, run `opencode pair` without the --remote flag."; diff --git a/packages/cli-rs/crates/tui/Cargo.toml b/packages/cli-rs/crates/tui/Cargo.toml index e7bcebf7f90..45ee8013aee 100644 --- a/packages/cli-rs/crates/tui/Cargo.toml +++ b/packages/cli-rs/crates/tui/Cargo.toml @@ -24,7 +24,6 @@ chrono.workspace = true futures-util.workspace = true indexmap.workspace = true log.workspace = true -qrcode.workspace = true regex.workspace = true rusqlite.workspace = true serde.workspace = true diff --git a/packages/cli-rs/crates/tui/src/dialogs/pair.rs b/packages/cli-rs/crates/tui/src/dialogs/pair.rs index 9028fe82c41..3392d870aa4 100644 --- a/packages/cli-rs/crates/tui/src/dialogs/pair.rs +++ b/packages/cli-rs/crates/tui/src/dialogs/pair.rs @@ -12,8 +12,6 @@ use opencode_ui::dialog::{DialogCx, DialogOptions, DialogSize, HeaderProps, head use opencode_ui::keymap::{Bind, GLOBAL_MODE, KeymapLayer, Slash, TuiCommand}; use opentui::keymap::Disposer; use opentui::prelude::*; -use qrcode::bits::Bits; -use qrcode::{EcLevel, QrCode, Version}; use crate::app::Tui; @@ -217,7 +215,7 @@ fn content(cx: &DialogCx, value: &Pairing) -> View { .align_items(Align::Center); } if let Some(url) = value.links.first() { - code.add(&Text::new(app, render_qr(url)).fg(theme.text.base)); + code.add(&Text::new(app, opencode_ui::qr::render_unicode_compact(url, 1)).fg(theme.text.base)); } let row = View::new(app).gap(2).child(&side).child(&code); @@ -235,44 +233,3 @@ fn content(cx: &DialogCx, value: &Pairing) -> View { scroll.add(&row); View::new(app).child(&scroll) } - -/// `renderUnicodeCompact(text, { border: 1 })`: byte mode at level L in the smallest version, two modules per -/// cell with half blocks. Light modules draw as blocks and dark ones as the background. -pub fn render_qr(text: &str) -> String { - let Some(code) = (1..=40).find_map(|version| { - let mut bits = Bits::new(Version::Normal(version)); - bits.push_byte_data(text.as_bytes()).ok()?; - bits.push_terminator(EcLevel::L).ok()?; - QrCode::with_bits(bits, EcLevel::L).ok() - }) else { - return String::new(); - }; - let width = code.width(); - let colors = code.to_colors(); - // With the quiet zone of one module; `true` is dark. - let size = width + 2; - let dark = |x: usize, y: usize| { - (1..=width).contains(&x) - && (1..=width).contains(&y) - && colors[(y - 1) * width + (x - 1)] == qrcode::Color::Dark - }; - (0..size.div_ceil(2)) - .map(|line| { - (0..size) - .map(|x| { - let top = dark(x, line * 2); - // The symbol has an odd number of rows, so the last line's missing bottom half shows the - // terminal background. - let bottom = line * 2 + 1 >= size || dark(x, line * 2 + 1); - match (top, bottom) { - (true, true) => ' ', - (true, false) => '\u{2584}', - (false, true) => '\u{2580}', - (false, false) => '\u{2588}', - } - }) - .collect::() - }) - .collect::>() - .join("\n") -} diff --git a/packages/cli-rs/crates/ui/src/lib.rs b/packages/cli-rs/crates/ui/src/lib.rs index 8bb690f7d92..4fe6609a5af 100644 --- a/packages/cli-rs/crates/ui/src/lib.rs +++ b/packages/cli-rs/crates/ui/src/lib.rs @@ -13,6 +13,7 @@ pub mod motion; pub mod palette; pub mod path; pub mod prompt; +pub mod qr; pub mod select; pub mod theme; pub mod toast; diff --git a/packages/cli-rs/crates/cli/src/commands/pair/qr.rs b/packages/cli-rs/crates/ui/src/qr.rs similarity index 98% rename from packages/cli-rs/crates/cli/src/commands/pair/qr.rs rename to packages/cli-rs/crates/ui/src/qr.rs index d64086b5dd8..17cbaf8a4e3 100644 --- a/packages/cli-rs/crates/cli/src/commands/pair/qr.rs +++ b/packages/cli-rs/crates/ui/src/qr.rs @@ -1,5 +1,6 @@ -//! QR codes for `pair`: a port of `packages/util/src/qr.ts` (Nayuki's reference encoder), byte mode at level L in -//! the smallest version. The `qrcode` crate picks other masks, so its codes differ from the TS client's. +//! QR codes for `pair` and the TUI's `/pair` dialog: a port of `packages/util/src/qr.ts` (Nayuki's reference +//! encoder), byte mode at level L in the smallest version. Not the `qrcode` crate, which picks other masks, so its +//! codes differ from the TS client's. // Error correction codewords per block and block count at level L, for versions 1 to 40. const ECC_PER_BLOCK: [usize; 40] = [