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 / rows-of / rows-of.ts
48 lines · 1.4 KBRaw
1import { countOf } from '../../../count-of'
2import Layout from '../../layout'
3import type Types from '../types'
4import { MAX_HIGHLIGHTS } from './max-highlights'
5import { segmentsOf } from './segments-of'
6
7/**
8 * Numbered lines laid into terminal rows: a long line wraps onto
9 * continuation rows with an empty gutter.
10 *
11 * Word highlights stop once MAX_HIGHLIGHTS are spent, keeping the tree
12 * under its node cap.
13 *
14 * @param lines the numbered lines
15 * @param cells the content's room per row after gutter and marker, one at
16 * least
17 * @returns the rows in drawing order
18 */
19export function rowsOf(
20 lines: readonly Types.DiffLine[],
21 cells: number,
22): Types.Row[] {
23 const rows: Types.Row[] = []
24 let highlights = 0
25
26 for (const line of lines) {
27 const marked = segmentsOf(line)
28 const wanted = countOf(marked, segment => segment.isChanged)
29 const isAffordable = highlights + wanted <= MAX_HIGHLIGHTS
30 highlights += isAffordable ? wanted : 0
31 const segments = isAffordable
32 ? marked
33 : [{ text: line.text, isChanged: false }]
34 const marker =
35 line.kind === 'added' ? '+' : line.kind === 'removed' ? '-' : ' '
36
37 Layout.wrapSegments(segments, cells).forEach((wrapped, at) => {
38 rows.push({
39 kind: line.kind,
40 gutter: at === 0 ? String(line.lineNumber) : '',
41 marker: at === 0 ? marker : ' ',
42 segments: wrapped,
43 })
44 })
45 }
46
47 return rows
48}