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: each decision a hook makes names its test, and a value used once is written where it is used

poteat46ea2a528de7
diff --git a/mods/diff/hooks/git/probes/untracked-files/untracked-files.ts b/mods/diff/hooks/git/probes/untracked-files/untracked-files.ts
index 27698ecd..aabd75c2 100644
--- a/mods/diff/hooks/git/probes/untracked-files/untracked-files.ts
+++ b/mods/diff/hooks/git/probes/untracked-files/untracked-files.ts
@@ -30,11 +30,11 @@ export async function untrackedFiles(
'--full-name',
])
- if (!Parse.isWholeAnswer(listing)) {
- return null
- }
-
- const paths = listing.stdout.split('\0').filter(path => path !== '')
-
- return untrackedRowsOf(context, paths, place)
+ return Parse.isWholeAnswer(listing)
+ ? untrackedRowsOf(
+ context,
+ listing.stdout.split('\0').filter(path => path !== ''),
+ place,
+ )
+ : null
}
diff --git a/mods/diff/hooks/git/tiers/with-untracked/with-untracked.ts b/mods/diff/hooks/git/tiers/with-untracked/with-untracked.ts
index 365b62d9..1c004215 100644
--- a/mods/diff/hooks/git/tiers/with-untracked/with-untracked.ts
+++ b/mods/diff/hooks/git/tiers/with-untracked/with-untracked.ts
@@ -22,15 +22,13 @@ export async function withUntracked(
result: Types.NumstatResult,
scope: Types.UntrackedScope,
): Promise<Types.MergedResult> {
- if (isPastDetails(result)) {
- return mergedResultOf(result, { isUntrackedWithheld: false })
- }
-
- return mergedWithUntracked(
- result,
- await Probes.untrackedFiles(context, {
- slots: Limits.MAX_FILES - result.files.length,
- scope,
- }),
- )
+ return isPastDetails(result)
+ ? mergedResultOf(result, { isUntrackedWithheld: false })
+ : mergedWithUntracked(
+ result,
+ await Probes.untrackedFiles(context, {
+ slots: Limits.MAX_FILES - result.files.length,
+ scope,
+ }),
+ )
}
diff --git a/mods/diff/hooks/pane-toggle/pane-toggle-of.ts b/mods/diff/hooks/pane-toggle/pane-toggle-of.ts
index b72b0ad1..431f6900 100644
--- a/mods/diff/hooks/pane-toggle/pane-toggle-of.ts
+++ b/mods/diff/hooks/pane-toggle/pane-toggle-of.ts
@@ -12,11 +12,12 @@ import type { PaneBelief } from './pane-belief'
* @param pane the plugin's belief, the probe's answer and the width
* @returns `close`, `open` or `too-narrow`
*/
-export const paneToggleOf = (
+export function paneToggleOf(
pane: PaneBelief,
-): 'open' | 'close' | 'too-narrow' =>
- pane.isBelievedOpen && pane.wasDrawnWhenProbed
- ? 'close'
- : pane.columns !== null && pane.columns < Limits.OPEN_MIN_COLUMNS
- ? 'too-narrow'
- : 'open'
+): 'open' | 'close' | 'too-narrow' {
+ const isClosing = pane.isBelievedOpen && pane.wasDrawnWhenProbed
+ const isTooNarrow =
+ pane.columns !== null && pane.columns < Limits.OPEN_MIN_COLUMNS
+
+ return isClosing ? 'close' : isTooNarrow ? 'too-narrow' : 'open'
+}
diff --git a/mods/diff/hooks/register.ts b/mods/diff/hooks/register.ts
index 0a7d967f..0e3979b0 100644
--- a/mods/diff/hooks/register.ts
+++ b/mods/diff/hooks/register.ts
@@ -595,10 +595,10 @@ export function register(on: On) {
}
const context = e.context ?? []
- const used = context.reduce((sum, entry) => sum + entry.length, 0)
const text = Ask.fittedAskTextOf(
asked.text,
- Limits.PROMPT_CONTEXT_MAX_CHARS - used,
+ Limits.PROMPT_CONTEXT_MAX_CHARS -
+ context.reduce((sum, entry) => sum + entry.length, 0),
)
if (text === undefined) {
diff --git a/mods/diff/hooks/views/current-pane/current-pane.tsx b/mods/diff/hooks/views/current-pane/current-pane.tsx
index b0646b23..a575200e 100644
--- a/mods/diff/hooks/views/current-pane/current-pane.tsx
+++ b/mods/diff/hooks/views/current-pane/current-pane.tsx
@@ -89,10 +89,10 @@ export function currentPane(
})
: null
const earlierFiles = model.isPreSessionShown ? partition.preSession : []
- const earlierRows =
- earlierFiles.length === 0
- ? []
- : [<Box height={1} />, ...earlierFiles.map(rowOf)]
+ const isEarlierListed = earlierFiles.length > 0
+ const earlierRows = isEarlierListed
+ ? [<Box height={1} />, ...earlierFiles.map(rowOf)]
+ : []
const isUntrackedNoted = data?.isUntrackedWithheld === true && !empty
const listed = listBodyOf(kit, { data, partition, totals, empty })
const notes = listed.filter(line => typeof line === 'string')
@@ -125,8 +125,10 @@ export function currentPane(
]
: []
- if (message.length > 0) {
- return messagePaneOf(kit, {
+ const isMessageShown = message.length > 0
+
+ return isMessageShown ? (
+ messagePaneOf(kit, {
top: Sections.present([
header,
noteOf(baseLabel),
@@ -140,9 +142,7 @@ export function currentPane(
earlier: earlierToggle,
rest: [...earlierRows, ...detail],
})
- }
-
- return (
+ ) : (
<Box flexDirection="column">
{Sections.present([
header,
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/foot-rows.ts b/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/foot-rows.ts
new file mode 100644
index 00000000..60dcdcc5
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/foot-rows.ts
@@ -0,0 +1,5 @@
+/**
+ * The rows the pre-session line takes at the pane's foot: a blank row, the
+ * line, and a blank row under it.
+ */
+export const FOOT_ROWS = 3
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/index.ts b/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/index.ts
new file mode 100644
index 00000000..2718b9b5
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/foot-rows/index.ts
@@ -0,0 +1,3 @@
+export * from './foot-rows.js'
+
+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
index 14dfcef0..8a4704e0 100644
--- a/mods/diff/hooks/views/current-pane/message-pane-of/index.ts
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/index.ts
@@ -1,3 +1,5 @@
+export * from './foot-rows'
+export * from './message-pane'
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
index 36805baa..a82a8533 100644
--- 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
@@ -5,6 +5,8 @@ import type { RenderElement } from 'claude-code'
import type { Kit } from '../../kit'
import Layout from '../../layout'
+import { FOOT_ROWS } from './foot-rows'
+import type { MessagePane } from './message-pane'
/**
* The pane while a message stands in for the list (ReplDiffSidebar's
@@ -19,16 +21,7 @@ import Layout from '../../layout'
* 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 {
+export function messagePaneOf(kit: Kit, pane: MessagePane): RenderElement {
const { Box, Text } = kit.ui
const { top, controls, earlier, rest } = pane
const lines = pane.message.flatMap(text =>
@@ -37,11 +30,13 @@ export function messagePaneOf(
const drawn = lines.map(line => <Text dimColor>{line}</Text>)
const controlRows = controls === null ? [] : [controls]
const earlierRows = earlier === null ? [] : [earlier]
- const aboveEarlier =
+ const isControlsAboveEarlier =
controlRows.length > 0 || earlierRows.length === 0
- ? controlRows
- : [<Box height={1} />]
- const middleRows = kit.rows - top.length - 1 - (earlier === null ? 0 : 3)
+ const aboveEarlier = isControlsAboveEarlier
+ ? controlRows
+ : [<Box height={1} />]
+ const footRows = earlier === null ? 0 : FOOT_ROWS
+ const middleRows = kit.rows - top.length - 1 - footRows
const above = Math.floor((middleRows - lines.length) / 2)
const isCentered =
rest.length === 0 && middleRows - above - lines.length >= controlRows.length
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/index.ts b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/index.ts
new file mode 100644
index 00000000..6e64d47d
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/index.ts
@@ -0,0 +1,3 @@
+export type * from './message-pane.js'
+
+export * as default from '.'
diff --git a/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/message-pane.ts b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/message-pane.ts
new file mode 100644
index 00000000..9d42adcd
--- /dev/null
+++ b/mods/diff/hooks/views/current-pane/message-pane-of/message-pane/message-pane.ts
@@ -0,0 +1,13 @@
+import type { RenderElement } from 'claude-code'
+
+/**
+ * What stands around a message in the pane: the header block, the message,
+ * the pickers, the pre-session line, and what is listed under it.
+ */
+export type MessagePane = {
+ top: readonly RenderElement[]
+ message: readonly string[]
+ controls: RenderElement | null
+ earlier: RenderElement | null
+ rest: readonly RenderElement[]
+}
diff --git a/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts b/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts
index ba5b7f16..0e921424 100644
--- a/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts
+++ b/mods/diff/hooks/views/layout/wrapped-lines/wrapped-lines.ts
@@ -33,9 +33,8 @@ export function wrappedLines(text: string, columns: number): string[] {
if (wordWidth > width) {
const breaksHere =
1 + Math.floor((wordWidth - (width - rowWidth) - 1) / width)
- const breaksNext = Math.floor((wordWidth - 1) / width)
- if (breaksNext < breaksHere) {
+ if (Math.floor((wordWidth - 1) / width) < breaksHere) {
rows.push('')
}
diff --git a/mods/diff/hooks/views/sections/controls-view.tsx b/mods/diff/hooks/views/sections/controls-view.tsx
index fcb6e9ea..82ce467e 100644
--- a/mods/diff/hooks/views/sections/controls-view.tsx
+++ b/mods/diff/hooks/views/sections/controls-view.tsx
@@ -46,23 +46,23 @@ export function controlsView(
) : null
const isCurrent = source.kind === 'current'
const fetchedSource = model.data?.source
- const base =
- fetchedSource?.kind === 'working-tree'
- ? fetchedSource.base
- : model.words.base
+ const isWorkingTree = fetchedSource?.kind === 'working-tree'
+ const base = isWorkingTree ? fetchedSource.base : model.words.base
const basePicker = isCurrent ? (
<Select
key="base"
label="base"
- options={model.baseModes.map(mode => ({
- value: mode,
- label:
- mode === 'uncommitted'
- ? `uncommitted (vs ${base})`
- : mode === 'session'
- ? 'this session'
- : mode,
- }))}
+ options={model.baseModes.map(mode => {
+ const isUncommitted = mode === 'uncommitted'
+ const isSession = mode === 'session'
+ const label = isUncommitted
+ ? `uncommitted (vs ${base})`
+ : isSession
+ ? 'this session'
+ : mode
+
+ return { value: mode, label }
+ })}
value={model.requestedMode}
onSelect={value => kit.actions.chooseBase(value)}
/>
diff --git a/mods/diff/hooks/views/sections/header-view.tsx b/mods/diff/hooks/views/sections/header-view.tsx
index de515595..ee10e5bf 100644
--- a/mods/diff/hooks/views/sections/header-view.tsx
+++ b/mods/diff/hooks/views/sections/header-view.tsx
@@ -24,20 +24,22 @@ export function headerView(
totals: PaneState.HeaderTotals | null,
): RenderElement {
const { Box, Text } = kit.ui
- 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">
- {[...counts, <Box flexGrow={1} />, closeButton(kit)]}
+ {[
+ ...(totals
+ ? [
+ <Text wrap="truncate-end">
+ <Text bold>{Layout.plural(totals.filesCount, 'file')}</Text>
+ {' changed '}
+ {diffStat(kit, totals.linesAdded, totals.linesRemoved)}
+ </Text>,
+ ]
+ : []),
+ <Box flexGrow={1} />,
+ closeButton(kit),
+ ]}
</Box>
)
}