spacehub

surshar / claude-code

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.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / views / detail / max-rows-of / max-rows-of.ts
59 lines · 1.8 KBRaw
1import Layout from '../../layout'
2import type { BodyCells } from '../hunk-body/body-cells'
3import { MARKER_CELLS } from '../marker-cells'
4import { MAX_BODY_CHARS } from '../max-body-chars'
5import { MAX_BODY_NODES } from '../max-body-nodes'
6import { MAX_RUN_CHARS } from '../max-run-chars'
7import type Types from '../types'
8import { FRAME_NODES } from './frame-nodes'
9import { HIGHLIGHT_NODES } from './highlight-nodes'
10import { RUN_NODES } from './run-nodes'
11
12/**
13 * How many of these rows the body may draw under MAX_BODY_CHARS and
14 * MAX_BODY_NODES, counted as the host counts what hunkBody draws.
15 *
16 * A row costs its gutter, marker, segments and padding in UTF-16 units (an
17 * astral letter costs two); a change of kind opens a run (a Text and its
18 * string), a highlight is three nodes, the gutter and long runs split.
19 *
20 * @param rows the laid-out rows, in order
21 * @param cells the gutter's and the content's widths
22 * @returns the count of leading rows whose drawn tree fits both budgets
23 */
24export function maxRowsOf(rows: readonly Types.Row[], cells: BodyCells) {
25 let chars = 0
26 let nodes = FRAME_NODES
27 let kept = 0
28 let kind: Types.LineKind | undefined
29
30 for (const row of rows) {
31 let units = 0
32 let charged = 0
33 let highlights = 0
34
35 for (const segment of row.segments) {
36 units += segment.text.length
37 charged += Layout.chargedWidth(segment.text)
38 highlights += segment.isChanged ? 1 : 0
39 }
40
41 chars +=
42 cells.gutterCells +
43 2 +
44 MARKER_CELLS +
45 units +
46 Math.max(0, cells.contentCells - charged)
47 nodes += (row.kind === kind ? 0 : RUN_NODES) + highlights * HIGHLIGHT_NODES
48 kind = row.kind
49 const splits = Math.ceil((2 * chars) / MAX_RUN_CHARS)
50
51 if (chars > MAX_BODY_CHARS || nodes + splits > MAX_BODY_NODES) {
52 return kept
53 }
54
55 kept += 1
56 }
57
58 return kept
59}