From 21880ef1ea6dd6e8eb0275f623a4e72f6562c8d8 Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Sun, 23 Aug 2026 22:32:05 +0800 Subject: [PATCH] fix(terminal): respect Option-as-Meta during block selection --- .../patch-xterm-macos-column-selection.cjs | 27 +++++---- ...atch-xterm-macos-column-selection.test.cjs | 13 +++- ...xterm-macos-column-selection.live.test.cjs | 60 +++++++++++++++---- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/scripts/patch-xterm-macos-column-selection.cjs b/scripts/patch-xterm-macos-column-selection.cjs index 2d8f52360..fa092b8d5 100644 --- a/scripts/patch-xterm-macos-column-selection.cjs +++ b/scripts/patch-xterm-macos-column-selection.cjs @@ -8,47 +8,50 @@ const path = require("node:path"); const EXPECTED_XTERM_VERSION = "6.1.0-beta.292"; // xterm normally treats macOptionClickForcesSelection and column selection as -// mutually exclusive. Netcatty needs both: Option must keep forcing local -// selection inside mouse-aware programs, and that local selection must retain -// the standard macOS rectangular shape. Patch the pinned bundles at install -// time and fail closed if upstream changes the expected code shape. +// mutually exclusive. Netcatty needs both when Option is not Meta: Option must +// keep forcing local selection inside mouse-aware programs, and that local +// selection must retain the standard macOS rectangular shape. Patch the pinned +// bundles at install time and fail closed if upstream changes the expected code +// shape. const PATCHES = [ { file: "node_modules/@xterm/xterm/lib/xterm.js", original: "!(f.isMac&&this._optionsService.rawOptions.macOptionClickForcesSelection)", + replacement: "(!f.isMac||!this._optionsService.rawOptions.macOptionIsMeta)", preserveLength: true, }, { file: "node_modules/@xterm/xterm/lib/xterm.mjs", original: "!(ie&&this._optionsService.rawOptions.macOptionClickForcesSelection)", + replacement: "(!ie||!this._optionsService.rawOptions.macOptionIsMeta)", preserveLength: true, }, { file: "node_modules/@xterm/xterm/src/browser/services/SelectionService.ts", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionIsMeta);", }, { file: "node_modules/@xterm/xterm/lib/xterm.js.map", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionIsMeta);", }, { file: "node_modules/@xterm/xterm/lib/xterm.mjs.map", original: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionClickForcesSelection);", - replacement: "return event.altKey;", + replacement: "return event.altKey && !(Browser.isMac && this._optionsService.rawOptions.macOptionIsMeta);", }, ]; -function sameLengthTrueExpression(length) { - if (length < 4) throw new Error("replacement target is too short"); +function sameLengthExpression(expression, length) { + if (expression.length > length) throw new Error("replacement expression is too long"); // Keep generated bundle offsets stable so the shipped source maps remain valid. - return `(!0${" ".repeat(length - 4)})`; + return `${expression}${" ".repeat(length - expression.length)}`; } function patchXtermSource(source, patch) { const replacement = patch.preserveLength - ? sameLengthTrueExpression(patch.original.length) + ? sameLengthExpression(patch.replacement, patch.original.length) : patch.replacement; if (!replacement) throw new Error(`missing replacement for ${patch.file}`); @@ -106,5 +109,5 @@ module.exports = { PATCHES, patchInstalledXterm, patchXtermSource, - sameLengthTrueExpression, + sameLengthExpression, }; diff --git a/scripts/patch-xterm-macos-column-selection.test.cjs b/scripts/patch-xterm-macos-column-selection.test.cjs index 32089adf6..038907e33 100644 --- a/scripts/patch-xterm-macos-column-selection.test.cjs +++ b/scripts/patch-xterm-macos-column-selection.test.cjs @@ -6,7 +6,7 @@ const test = require("node:test"); const { PATCHES, patchXtermSource, - sameLengthTrueExpression, + sameLengthExpression, } = require("./patch-xterm-macos-column-selection.cjs"); test("patches both distributed bundles without shifting source-map offsets", () => { @@ -17,7 +17,7 @@ test("patches both distributed bundles without shifting source-map offsets", () assert.equal(result.source.length, input.length); assert.equal(result.source.includes(patch.original), false); assert.equal( - result.source.includes(sameLengthTrueExpression(patch.original.length)), + result.source.includes(sameLengthExpression(patch.replacement, patch.original.length)), true, ); } @@ -34,7 +34,7 @@ test("patches readable xterm sources and source-map content", () => { test("is idempotent and fails closed on an unknown package shape", () => { for (const patch of PATCHES) { const replacement = patch.preserveLength - ? sameLengthTrueExpression(patch.original.length) + ? sameLengthExpression(patch.replacement, patch.original.length) : patch.replacement; assert.deepEqual(patchXtermSource(`before:${replacement}:after`, patch), { source: `before:${replacement}:after`, @@ -46,3 +46,10 @@ test("is idempotent and fails closed on an unknown package shape", () => { ); } }); + +test("fails closed when a generated-bundle replacement would shift offsets", () => { + assert.throws( + () => sameLengthExpression("replacement is longer", 4), + /replacement expression is too long/, + ); +}); diff --git a/scripts/xterm-macos-column-selection.live.test.cjs b/scripts/xterm-macos-column-selection.live.test.cjs index bd7b080d5..04375db87 100644 --- a/scripts/xterm-macos-column-selection.live.test.cjs +++ b/scripts/xterm-macos-column-selection.live.test.cjs @@ -12,23 +12,32 @@ if (!process.versions.electron) { } else { const assert = require("node:assert/strict"); const fs = require("node:fs"); - const os = require("node:os"); const path = require("node:path"); const electron = require("electron"); + const tempDirBridge = require("../electron/bridges/tempDirBridge.cjs"); const appRoot = path.resolve(__dirname, ".."); - const userData = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-xterm-macos-selection-")); + const userData = fs.mkdtempSync( + `${tempDirBridge.getTempFilePath("xterm-macos-selection")}-`, + ); electron.app.setPath("userData", userData); electron.app.on("window-all-closed", () => {}); + let window = null; const cleanup = (exitCode) => { - fs.rmSync(userData, { recursive: true, force: true }); - electron.app.exit(exitCode); + if (window && !window.isDestroyed()) window.destroy(); + try { + fs.rmSync(userData, { recursive: true, force: true }); + } catch (error) { + console.warn("Unable to remove xterm selection test data:", error); + } finally { + electron.app.exit(exitCode); + } }; const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds)); void electron.app.whenReady().then(async () => { - const window = new electron.BrowserWindow({ + window = new electron.BrowserWindow({ show: process.env.NETCATTY_XTERM_SELECTION_SHOW_WINDOW === "1", width: 860, height: 400, @@ -56,6 +65,7 @@ if (!process.versions.electron) { fontSize: 18, allowProposedApi: true, macOptionClickForcesSelection: true, + macOptionIsMeta: false, altClickMovesCursor: true, }); let data = ""; @@ -68,10 +78,12 @@ if (!process.versions.electron) { "!@#$%^&*()_+-=[]{}|;", ].join("\\r\\n"); window.harness = { - reset: mouseTracking => new Promise(resolve => { + reset: (mouseTracking, optionAsMeta) => new Promise(resolve => { data = ""; term.reset(); term.clearSelection(); + term.options.macOptionIsMeta = optionAsMeta; + term.options.altClickMovesCursor = !optionAsMeta; term.write((mouseTracking ? "\\x1b[?1002h\\x1b[?1006h" : "\\x1b[?1002l\\x1b[?1006l") + content, resolve); }), geometry: () => { @@ -87,6 +99,7 @@ if (!process.versions.electron) { selection: term.getSelection(), data, mouseTrackingMode: term.modes.mouseTrackingMode, + optionAsMeta: term.options.macOptionIsMeta, }), }; })()`); @@ -107,8 +120,10 @@ if (!process.versions.electron) { await delay(100); return window.webContents.executeJavaScript("window.harness.state()"); }; - const runScenario = async (mouseTracking, alt) => { - await window.webContents.executeJavaScript(`window.harness.reset(${mouseTracking})`); + const runScenario = async (mouseTracking, alt, optionAsMeta = false) => { + await window.webContents.executeJavaScript( + `window.harness.reset(${mouseTracking}, ${optionAsMeta})`, + ); return drag({ alt }); }; @@ -116,11 +131,15 @@ if (!process.versions.electron) { const option = await runScenario(false, true); const remote = await runScenario(true, false); const remoteOption = await runScenario(true, true); + const optionAsMeta = await runScenario(false, true, true); + const remoteOptionAsMeta = await runScenario(true, true, true); + const optionAfterMeta = await runScenario(false, true, false); const expectedColumn = "BCDEF\nbcdef\n12345"; + const expectedNormal = "BCDEFGHIJKLMNOPQRST\nabcdefghijklmnopqrst\n012345"; assert.equal( normal.selection, - "BCDEFGHIJKLMNOPQRST\nabcdefghijklmnopqrst\n012345", + expectedNormal, `ordinary drag must remain a normal selection: ${JSON.stringify(normal)}`, ); assert.equal(option.selection, expectedColumn, `Option drag must select columns: ${JSON.stringify(option)}`); @@ -132,11 +151,30 @@ if (!process.versions.electron) { `Option drag must force rectangular selection in mouse mode: ${JSON.stringify(remoteOption)}`, ); assert.equal(remoteOption.data, "", `forced local selection must not emit mouse reports: ${JSON.stringify(remoteOption)}`); + assert.equal( + optionAsMeta.selection, + expectedNormal, + `Option-as-Meta must disable rectangular selection: ${JSON.stringify(optionAsMeta)}`, + ); + assert.equal( + remoteOptionAsMeta.selection, + expectedNormal, + `Option-as-Meta must disable rectangular selection in mouse mode: ${JSON.stringify(remoteOptionAsMeta)}`, + ); + assert.equal( + remoteOptionAsMeta.data, + "", + `Option must still force local selection in mouse mode: ${JSON.stringify(remoteOptionAsMeta)}`, + ); + assert.equal( + optionAfterMeta.selection, + expectedColumn, + `turning Option-as-Meta off must restore rectangular selection: ${JSON.stringify(optionAfterMeta)}`, + ); process.stdout.write( - `XTERM_MACOS_COLUMN_SELECTION_OK ${JSON.stringify({ normal, option, remote, remoteOption })}\n`, + `XTERM_MACOS_COLUMN_SELECTION_OK ${JSON.stringify({ normal, option, remote, remoteOption, optionAsMeta, remoteOptionAsMeta, optionAfterMeta })}\n`, ); - window.destroy(); cleanup(0); }).catch((error) => { console.error(error);