Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(view): code chips measure width in columns [N26]

Corey Olson3c5d24273ef3
diff --git a/src/cockpit.js b/src/cockpit.js
index 49713d57..43577d7f 100644
--- a/src/cockpit.js
+++ b/src/cockpit.js
@@ -191,8 +191,11 @@ function theme(pick) {
}
theme(keep('pb.theme'));
const esc = (text) => String(text ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[c]);
-// Inline code: a short chip stays whole on its line; one longer than a phone line's worth may wrap.
-const codeChip = (text) => '<code class="inline' + (String(text).length > 24 ? ' long' : '') + '">' + esc(text) + '</code>';
+// Inline code: a short chip stays whole on its line; one wider than a phone line's worth may wrap. Width
+// is in columns, as a terminal counts them: CJK, full-width and emoji characters take two.
+const wideChar = (c) => { const p = c.codePointAt(0); return (p >= 0x1100 && p <= 0x115f) || (p >= 0x2e80 && p <= 0xa4cf) || (p >= 0xac00 && p <= 0xd7a3) || (p >= 0xf900 && p <= 0xfaff) || (p >= 0xfe30 && p <= 0xfe4f) || (p >= 0xff00 && p <= 0xff60) || (p >= 0xffe0 && p <= 0xffe6) || (p >= 0x1f300 && p <= 0x1faff) || (p >= 0x20000 && p <= 0x3fffd); };
+const columns = (text) => [...String(text)].reduce((n, c) => n + (wideChar(c) ? 2 : 1), 0);
+const codeChip = (text) => '<code class="inline' + (columns(text) > 24 ? ' long' : '') + '">' + esc(text) + '</code>';
const ago = (iso) => { const m = Math.round((Date.now() - Date.parse(iso)) / 60000); return m < 1 ? 'now' : m < 60 ? m + 'm' : m < 2880 ? Math.round(m / 60) + 'h' : Math.round(m / 1440) + 'd'; };
// An age as the page shows it: the moment it counts from stays on it, so tickAges can move it on.
const age = (iso) => '<time data-ago="' + esc(iso) + '">' + ago(iso) + '</time>';
diff --git a/test/cockpit.test.js b/test/cockpit.test.js
index 15d98cec..206a6ef8 100644
--- a/test/cockpit.test.js
+++ b/test/cockpit.test.js
@@ -3708,6 +3708,9 @@ test('a short code chip at a line end stays whole, and long code still wraps ins
const demo = project(box, 'chip-ends');
const long = 'docs/a/very/long/path/that/keeps/going/well/past/any/phone/screen/width.md';
box.run(demo.repo, 'shout', 'all', `Pass the --flag option, then read ${long} before you submit.`);
+ // Twenty-four wide characters are short by count but take two columns each: about 320px of one chip.
+ const wide = '中文'.repeat(12);
+ box.run(demo.repo, 'shout', 'all', 'Wide code `' + wide + '` wraps too.');
const view = await startView(box);
const profile = mkdtempSync(join(tmpdir(), 'pullboard-chip-end-chrome-'));
let chrome;
@@ -3715,8 +3718,8 @@ test('a short code chip at a line end stays whole, and long code still wraps ins
chrome = await openSnapshotChrome(executable, view.link.href, profile);
await chrome.waitFor('!!document.querySelector(\'[data-tab="shouts"]\')');
await chrome.evaluate('document.querySelector(\'[data-tab="shouts"]\').click()');
- await chrome.waitFor(`[...document.querySelectorAll('#feed code.inline')].some((code) => code.textContent === ${JSON.stringify(long)})`);
- for (const width of [375, 1280]) {
+ await chrome.waitFor(`[...document.querySelectorAll('#feed code.inline')].some((code) => code.textContent === ${JSON.stringify(wide)})`);
+ for (const width of [320, 375, 1280]) {
await chrome.send('Emulation.setDeviceMetricsOverride', { width, height: 900, deviceScaleFactor: 1, mobile: false });
await chrome.waitFor(`innerWidth === ${width}`);
const seen = JSON.parse(await chrome.evaluate(`JSON.stringify((() => {
@@ -3727,18 +3730,21 @@ test('a short code chip at a line end stays whole, and long code still wraps ins
const pathRects = [...path.getClientRects()];
const longCode = { fragments: pathRects.length, right: Math.max(...pathRects.map((r) => r.right)), screen: document.documentElement.clientWidth,
overflow: document.documentElement.scrollWidth > document.documentElement.clientWidth };
+ const wideRects = [...chips.find((code) => code.textContent === ${JSON.stringify(wide)}).getClientRects()];
+ const wideCode = { fragments: wideRects.length, right: Math.max(...wideRects.map((r) => r.right)), screen: document.documentElement.clientWidth };
// Then end the chip's line three pixels inside the chip, whatever this machine's fonts measure.
- const start = text.getBoundingClientRect().left, end = flag.getBoundingClientRect().right;
+ const start = text.getBoundingClientRect().left, end = flag.getBoundingClientRect().right, line = text.getBoundingClientRect().width;
text.style.width = (end - start - 3) + 'px';
- const rects = [...flag.getClientRects()], line = end - start - 3;
+ const rects = [...flag.getClientRects()];
const atEnd = { fragments: rects.length, display: getComputedStyle(flag).display, width: Math.max(...rects.map((r) => r.width)), line };
text.style.width = '';
- return { atEnd, longCode };
+ return { atEnd, longCode, wideCode };
})())`));
assert.equal(seen.atEnd.fragments, 1, `${width}: a short chip at a line's end moves to the next line whole, never broken after its "--": ${JSON.stringify(seen)}`);
assert.equal(seen.atEnd.display, 'inline', `${width}: and it is still inline code, not a bar`);
assert.ok(seen.atEnd.width < seen.atEnd.line / 2, `${width}: and compact: ${JSON.stringify(seen.atEnd)}`);
if (width === 375) assert.ok(seen.longCode.fragments > 1, `${width}: code longer than its line wraps: ${JSON.stringify(seen.longCode)}`);
+ assert.ok(seen.wideCode.right <= seen.wideCode.screen + 0.5, `${width}: so does code of wide characters, by the columns it takes: ${JSON.stringify(seen.wideCode)}`);
assert.ok(seen.longCode.right <= seen.longCode.screen + 0.5 && !seen.longCode.overflow, `${width}: and never runs past the screen: ${JSON.stringify(seen.longCode)}`);
}
} finally {