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 / layout / wrap-segments / wrap-segments.ts
43 lines · 1.2 KBRaw
1import type { Segment } from '../segment'
2import { sliceCells } from '../slice-cells'
3
4/**
5 * A line's segments laid into rows of at most a cell budget, a segment
6 * split where a row fills, so each row draws as exactly one terminal row.
7 *
8 * A row holds no more code points than cells (sliceCells' spending), and
9 * a continuation can sit under an empty gutter.
10 *
11 * @param segments the line's segments in order
12 * @param cells the room per row, at least one cell
13 * @returns the rows; one empty row for an empty line
14 */
15export function wrapSegments(
16 segments: readonly Segment[],
17 cells: number,
18): Segment[][] {
19 const rows: Segment[][] = []
20 let row: Segment[] = []
21 let used = 0
22
23 for (const segment of segments) {
24 let rest = segment.text
25
26 while (rest !== '') {
27 const split = sliceCells(rest, Math.max(1, cells - used))
28 row.push({ text: split.head, isChanged: segment.isChanged })
29 used += split.spent
30 rest = split.tail
31
32 if (rest !== '' || used >= cells) {
33 rows.push(row)
34 row = []
35 used = 0
36 }
37 }
38 }
39
40 const isRowPending = row.length > 0 || rows.length === 0
41
42 return isRowPending ? [...rows, row] : rows
43}