Skip to content

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

TypeScript

mods/diff: the empty state sits where the built-in centers it, the pre-session line at the foot

poteat148e0f0930a1
diff --git a/mods/diff/hooks/register.ts b/mods/diff/hooks/register.ts
index 8033d8bf..26f06c38 100644
--- a/mods/diff/hooks/register.ts
+++ b/mods/diff/hooks/register.ts
@@ -491,6 +491,7 @@ export function register(on: On) {
ui: { Box, Text, Button, Select, Code },
actions: actionsOf(host),
columns: e.props.bodyColumns,
+ rows: e.props.scroll.bodyRows,
},
model,
e.props.placement,
diff --git a/mods/diff/hooks/views/current-pane/current-pane.tsx b/mods/diff/hooks/views/current-pane/current-pane.tsx
index 44cb1e91..bc4cb544 100644
--- a/mods/diff/hooks/views/current-pane/current-pane.tsx
+++ b/mods/diff/hooks/views/current-pane/current-pane.tsx
@@ -12,6 +12,7 @@ import Layout from '../layout'
import Sections from '../sections'
import { fileRowOf } from './file-row-of'
import { listBodyOf } from './list-body-of'
+import { messagePaneOf } from './message-pane-of'
/**
* The pane over the repository's diff now (ReplDiffSidebarBody, one file's
@@ -19,9 +20,11 @@ import { listBodyOf } from './list-body-of'
*
* The tests-and-generated toggle sits above the rows; the elision count, the
* withheld-untracked note, the pre-session line and rows below them; the
- * selected file last.
+ * selected file last. With no rows to list, the loading line, the empty
+ * state or the too-many and only-hidden lines take the body instead
+ * (messagePaneOf), the header keeping only the close.
*
- * @param kit the elements, the handlers, the width
+ * @param kit the elements, the handlers, the width, the rows
* @param model the pane's state
* @returns the pane's tree
*/
@@ -37,7 +40,13 @@ export function currentPane(
}
if (!data && !model.hasSettled) {
- return <Box>{Sections.dimNote(kit, 'Loading diff…')}</Box>
+ return messagePaneOf(kit, {
+ top: [Sections.headerView(kit, null)],
+ message: ['Loading diff…'],
+ controls: null,
+ earlier: null,
+ rest: [],
+ })
}
const noise = model.isNoiseShown ? 'shown' : 'hidden'
@@ -84,6 +93,16 @@ export function currentPane(
const earlierRows = model.isPreSessionShown ? partition.preSession : []
const isUntrackedNoted = data?.isUntrackedWithheld === true && !empty
const listed = listBodyOf(kit, { data, partition, totals, empty })
+ const notes = listed.filter(line => typeof line === 'string')
+ const hasRows = notes.length < listed.length
+ const message = empty ? [empty.headline, ...notes] : hasRows ? [] : notes
+ const header = Sections.headerView(kit, empty ? null : totals)
+ const notShownNote = noteOf(
+ totals.notShown > 0 ? `${totals.notShown} not shown` : null,
+ )
+ const untrackedNote = noteOf(
+ isUntrackedNoted ? Names.untrackedWithheldTextOf(model.words.lister) : null,
+ )
const detail = selected
? [
Sections.divider(kit),
@@ -104,10 +123,27 @@ export function currentPane(
]
: []
+ if (message.length > 0) {
+ return messagePaneOf(kit, {
+ top: Sections.present([
+ header,
+ noteOf(baseLabel),
+ Sections.todoBar(kit, model.todos),
+ noiseToggle,
+ notShownNote,
+ untrackedNote,
+ ]),
+ message,
+ controls: Sections.controlsView(kit, model),
+ earlier: earlierToggle,
+ rest: [...earlierRows.map(rowOf), ...detail],
+ })
+ }
+
return (
<Box flexDirection="column">
{Sections.present([
- Sections.headerView(kit, empty?.headline ?? null, totals),
+ header,
noteOf(baseLabel),
Sections.todoBar(kit, model.todos),
Sections.controlsView(kit, model),
@@ -115,12 +151,8 @@ export function currentPane(
...listed.map(line =>
typeof line === 'string' ? noteOf(line) : rowOf(line),
),
- noteOf(totals.notShown > 0 ? `${totals.notShown} not shown` : null),
- noteOf(
- isUntrackedNoted
- ? Names.untrackedWithheldTextOf(model.words.lister)
- : null,
- ),
+ notShownNote,
+ untrackedNote,
earlierToggle,
...earlierRows.map(rowOf),
...detail,
diff --git a/mods/diff/hooks/views/current-pane/index.ts b/mods/diff/hooks/views/current-pane/index.ts
index f802aa62..30ac292e 100644
--- a/mods/diff/hooks/views/current-pane/index.ts
+++ b/mods/diff/hooks/views/current-pane/index.ts
@@ -1,5 +1,6 @@
export * from './current-pane.jsx'
export * from './file-row-of'
export * from './list-body-of'
+export * from './message-pane-of'
export * as default from '.'
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/index.ts b/mods/diff/hooks/views/current-pane/message-pane-of/index.ts
new file mode 100644
index 00000000..14dfcef0
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/index.ts
@@ -0,0 +1,3 @@
+export * from './message-pane-of.jsx'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/message-pane-of.tsx b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane-of.tsx
new file mode 100644
index 00000000..35a6c759
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane-of.tsx
@@ -0,0 +1,84 @@
+/* @jsxRuntime classic */
+/* @jsx h */
+/* @jsxFrag Fragment */
+import type { RenderElement } from 'claude-code'
+
+import type { Kit } from '../../kit'
+import Layout from '../../layout'
+
+/**
+ * The pane while a message stands in for the list (ReplDiffSidebar's
+ * centered body: the first fetch, the empty state, the too-many and
+ * only-hidden states): the header block, the built-in's blank row under it,
+ * the message with the pickers under it, the pre-session line.
+ *
+ * With nothing listed below and the rows to hold it, the message's lines
+ * sit where the built-in centers them (half the spare rows above, rounded
+ * down; each wrapped line centered on its own), the pickers right under
+ * them and the pre-session line at the foot; otherwise it all stacks from
+ * the top.
+ *
+ * @param kit the elements, the width, the body's rows
+ * @param pane the header block, the message's lines, the pickers, the
+ * pre-session line, and what is listed under it
+ * @returns the pane's tree
+ */
+export function messagePaneOf(
+ kit: Kit,
+ pane: {
+ top: readonly RenderElement[]
+ message: readonly string[]
+ controls: RenderElement | null
+ earlier: RenderElement | null
+ rest: readonly RenderElement[]
+ },
+): RenderElement {
+ const { Box, Text } = kit.ui
+ const { top, controls, earlier, rest } = pane
+ const lines = pane.message.flatMap(text =>
+ Layout.wrappedLines(Layout.sanitizeName(text), kit.columns),
+ )
+ const drawn = lines.map(line => <Text dimColor>{line}</Text>)
+ const controlRows = controls === null ? [] : [controls]
+ const earlierRows = earlier === null ? [] : [earlier]
+ const middleRows = kit.rows - top.length - 1 - (earlier === null ? 0 : 3)
+ const above = Math.floor((middleRows - lines.length) / 2)
+ const isCentered =
+ rest.length === 0 &&
+ middleRows - above - lines.length >= controlRows.length
+
+ if (!isCentered) {
+ return (
+ <Box flexDirection="column">
+ {[
+ ...top,
+ <Box height={1} />,
+ ...drawn,
+ ...controlRows,
+ ...earlierRows,
+ ...rest,
+ ]}
+ </Box>
+ )
+ }
+
+ const spacer = above > 0 ? [<Box height={above} />] : []
+ const foot = earlierRows.map(row => (
+ <Box flexDirection="column" marginTop={1} paddingBottom={1}>
+ {row}
+ </Box>
+ ))
+
+ return (
+ <Box flexDirection="column" height={kit.rows}>
+ {[
+ ...top,
+ <Box height={1} />,
+ <Box flexDirection="column" height={middleRows} alignItems="center">
+ {[...spacer, ...drawn, ...controlRows]}
+ </Box>,
+ ...foot,
+ ]}
+ </Box>
+ )
+}
diff --git a/mods/diff/hooks/views/kit/kit.ts b/mods/diff/hooks/views/kit/kit.ts
index 5950d26d..2bfa0eb9 100644
--- a/mods/diff/hooks/views/kit/kit.ts
+++ b/mods/diff/hooks/views/kit/kit.ts
@@ -3,10 +3,12 @@ import type { Ui } from './ui'
/**
* What every part of one drawing is handed: the surface's elements, the
- * controls' handlers, and the body's width (`props.bodyColumns`).
+ * controls' handlers, the body's width (`props.bodyColumns`) and its rows
+ * (`props.scroll.bodyRows`).
*/
export type Kit = {
ui: Ui
actions: PaneActions
columns: number
+ rows: number
}
diff --git a/mods/diff/hooks/views/layout/index.ts b/mods/diff/hooks/views/layout/index.ts
index 7ac964be..b9c5deb5 100644
--- a/mods/diff/hooks/views/layout/index.ts
+++ b/mods/diff/hooks/views/layout/index.ts
@@ -5,5 +5,6 @@ export * from './sanitize-line'
export * from './sanitize-name'
export * from './tab-width'
export * from './truncate-start'
+export * from './wrapped-lines'
export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/wrapped-lines/index.ts b/mods/diff/hooks/views/layout/wrapped-lines/index.ts
new file mode 100644
index 00000000..2c41c4d5
--- /dev/null
+++ b/mods/diff/hooks/views/layout/wrapped-lines/index.ts
@@ -0,0 +1,3 @@
+export * from './wrapped-lines.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts b/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts
new file mode 100644
index 00000000..ae4625c2
--- /dev/null
+++ b/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts
@@ -0,0 +1,71 @@
+import { cellWidth } from '../cell-width'
+
+/**
+ * A line cut into rows of at most `columns` cells as Ink's `wrap` cuts the
+ * built-in's empty state before it centers each row: at spaces, the space a
+ * row breaks on kept at its end, a word wider than a row cut across rows.
+ *
+ * @param text one sanitized line
+ * @param columns the width, at least one cell
+ * @returns the rows, at least one
+ */
+export function wrappedLines(text: string, columns: number): string[] {
+ const width = Math.max(1, columns)
+ const rows: string[] = ['']
+ const words = text.split(' ')
+
+ for (const [index, word] of words.entries()) {
+ const wordWidth = cellWidth(word)
+ let rowWidth = cellWidth(rows[rows.length - 1] ?? '')
+
+ if (index !== 0) {
+ if (rowWidth >= width) {
+ rows.push('')
+ rowWidth = 0
+ }
+
+ rows[rows.length - 1] += ' '
+ rowWidth += 1
+ }
+
+ if (wordWidth > width) {
+ const breaksHere = 1 + Math.floor((wordWidth - (width - rowWidth) - 1) / width)
+ const breaksNext = Math.floor((wordWidth - 1) / width)
+
+ if (breaksNext < breaksHere) {
+ rows.push('')
+ }
+
+ const characters = [...word]
+ let visible = cellWidth(rows[rows.length - 1] ?? '')
+
+ for (const [at, character] of characters.entries()) {
+ const characterWidth = cellWidth(character)
+
+ if (visible + characterWidth <= width) {
+ rows[rows.length - 1] += character
+ } else {
+ rows.push(character)
+ visible = 0
+ }
+
+ visible += characterWidth
+
+ if (visible === width && at < characters.length - 1) {
+ rows.push('')
+ visible = 0
+ }
+ }
+
+ continue
+ }
+
+ if (rowWidth + wordWidth > width && rowWidth > 0 && wordWidth > 0) {
+ rows.push('')
+ }
+
+ rows[rows.length - 1] += word
+ }
+
+ return rows
+}
diff --git a/mods/diff/hooks/views/pane-view.tsx b/mods/diff/hooks/views/pane-view.tsx
index 1694b208..ce030328 100644
--- a/mods/diff/hooks/views/pane-view.tsx
+++ b/mods/diff/hooks/views/pane-view.tsx
@@ -40,7 +40,11 @@ export function paneView(
const right = Limits.PANE_RIGHT_PAD_COLUMNS
const top = Limits.PANE_TOP_PAD_ROWS
- const inset: Kit = { ...kit, columns: Math.max(1, kit.columns - right) }
+ const inset: Kit = {
+ ...kit,
+ columns: Math.max(1, kit.columns - right),
+ rows: Math.max(0, kit.rows - top),
+ }
return (
<Box flexDirection="column" paddingTop={top} paddingRight={right}>
diff --git a/mods/diff/hooks/views/sections/header-view.tsx b/mods/diff/hooks/views/sections/header-view.tsx
index 86fa6716..1cf7626e 100644
--- a/mods/diff/hooks/views/sections/header-view.tsx
+++ b/mods/diff/hooks/views/sections/header-view.tsx
@@ -11,39 +11,32 @@ import { diffStat } from './diff-stat'
/**
* The pane's first line (ReplDiffSidebar's header row): `N files changed
- * +A -R`, or the empty headline in its place when there is nothing to
- * count, and the close `✕` at the right edge either way.
- *
- * The headline may name a branch the repository chose, so it is drawn as
- * a name: no control or format character survives.
+ * +A -R`, or nothing over an empty state, and the close `✕` at the right
+ * edge either way.
*
* @param kit the drawing's kit; its elements draw the line
- * @param headline the empty headline, or null to draw the counts
- * @param totals the header's counts
+ * @param totals the header's counts, or null when there is nothing to count
* @returns the line
*/
export function headerView(
kit: Kit,
- headline: string | null,
- totals: PaneState.HeaderTotals,
+ totals: PaneState.HeaderTotals | null,
): RenderElement {
const { Box, Text } = kit.ui
- const empty = (
- <Text dimColor wrap="truncate-end">
- {Layout.sanitizeName(headline ?? '')}
- </Text>
- )
- const counts = (
- <Text wrap="truncate-end">
- <Text bold>{Layout.plural(totals.filesCount, 'file')}</Text>
- {' changed '}
- {diffStat(kit, totals.linesAdded, totals.linesRemoved)}
- </Text>
- )
+ const counts =
+ totals === null
+ ? []
+ : [
+ <Text wrap="truncate-end">
+ <Text bold>{Layout.plural(totals.filesCount, 'file')}</Text>
+ {' changed '}
+ {diffStat(kit, totals.linesAdded, totals.linesRemoved)}
+ </Text>,
+ ]
return (
<Box flexDirection="row">
- {[headline === null ? counts : empty, <Box flexGrow={1} />, closeButton(kit)]}
+ {[...counts, <Box flexGrow={1} />, closeButton(kit)]}
</Box>
)
}
diff --git a/mods/diff/hooks/views/turn-pane/turn-pane.tsx b/mods/diff/hooks/views/turn-pane/turn-pane.tsx
index cdee0eb6..24a97c05 100644
--- a/mods/diff/hooks/views/turn-pane/turn-pane.tsx
+++ b/mods/diff/hooks/views/turn-pane/turn-pane.tsx
@@ -70,7 +70,7 @@ export function turnPane(
return (
<Box flexDirection="column">
{Sections.present([
- Sections.headerView(kit, null, PaneState.turnTotalsOf(turn)),
+ Sections.headerView(kit, PaneState.turnTotalsOf(turn)),
Sections.dimNote(
kit,
`Turn ${turn.index} "${Layout.sanitizeName(turn.preview)}"`,