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.
This commit is contained in:
Simon Klee
2026-11-05 00:34:08 +01:00
Unverified
parent de9012540b
commit edb0cc9773
7 changed files with 6 additions and 57 deletions
-7
View File
@@ -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"
-1
View File
@@ -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"] }
@@ -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.";
-1
View File
@@ -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
+1 -44
View File
@@ -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::<String>()
})
.collect::<Vec<_>>()
.join("\n")
}
+1
View File
@@ -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;
@@ -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] = [