fix(tui): preserve code block indentation in selection

Render code indentation spaces as selectable cells so copied fenced code keeps its leading whitespace.
This commit is contained in:
Brooklyn Nicholson
2026-04-25 15:17:36 -05:00
parent bba16943f6
commit 1735ced93b

View File

@@ -114,6 +114,25 @@ const renderTable = (k: number, rows: string[][], t: Theme) => {
)
}
const codeText = (text: string, color: string | undefined, key: number | string, t: Theme) => (
<Text color={color ?? (text.trim() ? undefined : t.color.dim)} key={key}>
{text}
</Text>
)
const plainCodeLine = (text: string, t: Theme) => {
const indent = text.match(/^\s+/)?.[0] ?? ''
return indent ? (
<>
{codeText(indent, undefined, 'indent', t)}
{text.slice(indent.length)}
</>
) : (
text
)
}
function MdInline({ t, text }: { t: Theme; text: string }) {
const parts: ReactNode[] = []
@@ -316,19 +335,7 @@ function MdImpl({ compact, t, text }: MdProps) {
{block.map((l, j) => {
if (highlighted) {
return (
<Text key={j}>
{highlightLine(l, lang, t).map(([color, text], kk) =>
color ? (
<Text color={color} key={kk}>
{text}
</Text>
) : (
<Text key={kk}>{text}</Text>
)
)}
</Text>
)
return <Text key={j}>{highlightLine(l, lang, t).map(([color, text], kk) => codeText(text, color, kk, t))}</Text>
}
const add = isDiff && l.startsWith('+')
@@ -342,7 +349,7 @@ function MdImpl({ compact, t, text }: MdProps) {
dimColor={isDiff && !add && !del && !hunk && l.startsWith(' ')}
key={j}
>
{l}
{plainCodeLine(l, t)}
</Text>
)
})}