Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.
TypeScript claude-code-mod
Copy a list item's marker only when the selection covers it
razorback162c08d77f9dd0
diff --git a/hooks/lib/copy.ts b/hooks/lib/copy.tsindex 4c08945..8f71c37 100644--- a/hooks/lib/copy.ts+++ b/hooks/lib/copy.ts@@ -146,8 +146,9 @@ export type Item = {href?: string; code?: string}// A block: paragraph, heading, code, quote, cell (`cx` its column), item-// (`prefix`); `first` its first item.-export type TextBlock = { kind: string; prefix: string; cx?: number; first: number }+// (`prefix`, and `mx` the x of its marker's centre in pixels, on its first+// word's line); `first` its first item.+export type TextBlock = { kind: string; prefix: string; cx?: number; mx?: number; first: number }// A probe's words by page (0-based: PNG page N+1), and its blocks.export type Positions = { ppi: number; blocks: TextBlock[]; pages: Item[][] }@@ -171,7 +172,8 @@ function isMarker(marks: readonly Mark[], j: number): boolean {}// The probe's marks (document order) as boxes in pixels at `ppi`. A list's-// marker is dropped (isMarker). A block mark with no word since the last one+// marker is dropped (isMarker), its centre kept as its item's `mx` (a+// checkbox drawn as a shape: 0.8 of the text size left of the item). A block mark with no word since the last one// (an item, then its paragraph) adds to that block. A word of only// invisible characters is dropped. A link's URL and an inline code's text go// to its words.@@ -184,12 +186,17 @@ export function positionsOf(marks: readonly Mark[], ppi: number): Positions {marks.forEach((m, j) => {if (m.k === 'b') {const cur = blocks[blocks.length - 1]+ const before = marks[j - 1]+ const mx =+ m.kind !== 'item' ? undefined+ : before && isMarker(marks, j - 1) ? round((before.x + (before.w ?? 0) / 2) * k)+ : round(Math.max(0, m.x - 0.8 * (m.s ?? 11)) * k)if (cur && !hasItems) {- cur.prefix ||= m.prefix ?? ''+ if (!cur.prefix && m.prefix) Object.assign(cur, { prefix: m.prefix }, mx === undefined ? {} : { mx })if (m.kind === 'cell') Object.assign(cur, { kind: 'cell', cx: m.cx ?? 0 })else if (cur.kind !== 'item' && cur.kind !== 'cell') cur.kind = m.kind ?? cur.kind} else {- blocks.push({ kind: m.kind ?? 'par', prefix: m.prefix ?? '', ...(m.kind === 'cell' ? { cx: m.cx ?? 0 } : {}), first: -1 })+ blocks.push({ kind: m.kind ?? 'par', prefix: m.prefix ?? '', ...(m.kind === 'cell' ? { cx: m.cx ?? 0 } : {}), ...(mx === undefined ? {} : { mx }), first: -1 })hasItems = false}return@@ -354,21 +361,24 @@ export function pickPages(segments: readonly Segment[], drawn: readonly Drawn[],// height / rows PNG pixels (a page drawn smaller has larger cells). A word is under the run when its box's centre is in a selected// cell (its row by the vertical centre, its column by the horizontal one):// half a word selected copies the word when the selection covers its middle.-// A code line is cut by characters, each by its own centre.+// A code line is cut by characters, each by its own centre. An item's+// prefix is copied only when its marker's centre (`mx`) is selected too.export function textOfRun(lines: readonly RunLine[], items: readonly Item[], blocks: readonly TextBlock[], page: { width: number; height: number; columns: number; rows: number },): string {const colW = page.width / page.columnsconst rowH = page.height / page.rowsconst sel = new Map(lines.map(l => [l.row, l]))+ const lineOf = (item: Item) => sel.get(Math.floor((item.y0 + item.y1) / 2 / rowH))+ const within = (line: RunLine, x: number) => {+ const col = Math.floor(x / colW)+ return col >= line.colStart && col <= line.colEnd+ }const picked: { item: Item; text: string }[] = []for (const item of items) {- const line = sel.get(Math.floor((item.y0 + item.y1) / 2 / rowH))+ const line = lineOf(item)if (!line) continue- const inside = (x: number) => {- const col = Math.floor(x / colW)- return col >= line.colStart && col <= line.colEnd- }+ const inside = (x: number) => within(line, x)if (item.adv) {const chars = Array.from(item.text)const kept = chars.filter((_, c) => inside(item.x0 + (c + 0.5) * item.adv!))@@ -377,7 +387,13 @@ export function textOfRun(picked.push({ item, text: chars.slice(first, first + kept.length).join('') })} else if (inside((item.x0 + item.x1) / 2)) picked.push({ item, text: item.text })}- return joinItems(picked, blocks)+ const marked = (b: number) => {+ const mx = blocks[b]?.mx+ const first = mx === undefined ? undefined : picked.find(p => p.item.i === blocks[b]!.first)+ const line = first && lineOf(first.item)+ return mx === undefined || (!!line && within(line, mx))+ }+ return joinItems(picked, blocks, marked)}// Joins picked words: a new line between blocks (a tab between the cells of@@ -386,15 +402,18 @@ export function textOfRun(// the pieces of one word (boxes on one line closer than 0.15 of the text// size, not counting the padding beside inline code: a space is 0.22-0.32// of it; none before a glued character that follows the one before it),-// none after a - or / that ends a line. Not of the line height: that is the+// none after a - or / that ends a line. `marked`: whether a block's marker+// is selected (its prefix is copied only then; all when absent). Not of the line height: that is the// font's (Noto Sans: 1.36 of the size) or the grid's rows. Items with no// text size: a fifth of the line height.-export function joinItems(picked: readonly { item: Item; text: string }[], blocks: readonly TextBlock[]): string {+export function joinItems(+ picked: readonly { item: Item; text: string }[], blocks: readonly TextBlock[], marked: (block: number) => boolean = () => true,+): string {let out = ''let prev: { item: Item; text: string } | null = nullconst prefixOf = (item: Item) => {const b = blocks[item.block]- return b?.prefix && b.first === item.i ? b.prefix : ''+ return b?.prefix && b.first === item.i && marked(item.block) ? b.prefix : ''}for (const p of picked) {if (!prev) out += prefixOf(p.item)diff --git a/tests/copy.test.ts b/tests/copy.test.tsindex 45816b3..85af40f 100644--- a/tests/copy.test.ts+++ b/tests/copy.test.ts@@ -78,7 +78,8 @@ test('probe marks become pixel boxes; a list number is dropped and an item takes{ k: 'c', p: 2, x: 10, y: 60, t: 'ab', w: 12, n: 2, a: 8, d: 2 },]const got = positionsOf(marks, 144)- expect(got.blocks).toEqual([{ kind: 'par', prefix: '', first: 0 }, { kind: 'item', prefix: '1. ', first: 1 }])+ // The item keeps its number's centre (`mx`): (20 + 3) pt at 2 px/pt.+ expect(got.blocks).toEqual([{ kind: 'par', prefix: '', first: 0 }, { kind: 'item', prefix: '1. ', mx: 46, first: 1 }])expect(got.pages[0]).toEqual([{ i: 0, text: 'Hi', block: 0, x0: 0, x1: 20, y0: 0, y1: 24 }])expect(got.pages[1]).toEqual([{ i: 1, text: 'one', block: 1, x0: 60, x1: 90, y0: 60, y1: 84 },@@ -100,7 +101,7 @@ test("a word keeps its link's URL and its inline code's text", () => {const got = positionsOf(marks, 72)expect(got.blocks).toEqual([{ kind: 'par', prefix: '', first: 0 },- { kind: 'item', prefix: '- ', first: 2 },+ { kind: 'item', prefix: '- ', mx: 0, first: 2 },{ kind: 'code', prefix: '', first: 3 },])expect(got.pages[0]!.map(w => [w.text, w.href, w.code])).toEqual([@@ -299,6 +300,28 @@ test('a real probe: a selection from mid-paragraph into the code block', () => {expect(textOfRun([{ row: 9, colStart: 10, colEnd: 97 }], REAL.pages[0]!, REAL.blocks, SMALL_PAGE)).toBe('item')})+test('a real probe: an item from its first word copies no marker; with the marker, the prefix', () => {+ // The "-" is in column 4, "second" starts in column 5.+ expect(textOfRun([{ row: 9, colStart: 5, colEnd: 97 }], REAL.pages[0]!, REAL.blocks, SMALL_PAGE)).toBe('second item')+ expect(textOfRun([{ row: 9, colStart: 4, colEnd: 97 }], REAL.pages[0]!, REAL.blocks, SMALL_PAGE)).toBe('- second item')+ // From the first item's first word on: the second item's marker is selected.+ expect(textOfRun([{ row: 7, colStart: 5, colEnd: 97 }, ALL_COLUMNS(8), ALL_COLUMNS(9)], REAL.pages[0]!, REAL.blocks, SMALL_PAGE)).toBe(+ 'first item\n- second item',+ )+})++test('a numbered item from its first word copies no number', () => {+ const marks: Mark[] = [+ { k: 'w', p: 1, x: 20, y: 0, s: 11, t: '12.', w: 12, h: 12 },+ { k: 'b', p: 1, x: 40, y: 0, s: 11, kind: 'item', prefix: '12. ' },+ { k: 'w', p: 1, x: 40, y: 0, s: 11, t: 'twelve', w: 30, h: 12 },+ ]+ const got = positionsOf(marks, 72)+ const page = { width: 100, height: 20, columns: 10, rows: 1 }+ expect(textOfRun([{ row: 0, colStart: 4, colEnd: 9 }], got.pages[0]!, got.blocks, page)).toBe('twelve')+ expect(textOfRun([{ row: 0, colStart: 2, colEnd: 9 }], got.pages[0]!, got.blocks, page)).toBe('12. twelve')+})+test('a real probe on a page drawn at half its box: a cell is twice as large', () => {const half = { ...SMALL_PAGE, columns: 49, rows: 8 }// "jumps" has its centre at x 339 px: column 21 of 16 px, column 10 of 32 px.