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 / slice-cells / slice-cells.ts
37 lines · 990 BRaw
1import { chargedWidth } from '../charged-width'
2import type { CellsSplit } from './cells-split'
3
4/**
5 * The longest head of a line that fits a cell budget, cut between code
6 * points, and what is left after it.
7 *
8 * The budget is spent by chargedWidth, at least one cell a code point, so
9 * a head never holds more code points than cells: a line of ten thousand
10 * combining marks still wraps.
11 *
12 * @param text a sanitized line
13 * @param cells the room, at least one cell
14 * @returns the head that fits, the budget it spent, and the remaining tail
15 */
16export function sliceCells(text: string, cells: number): CellsSplit {
17 let spent = 0
18 let taken = 0
19 const characters = [...text]
20
21 for (const character of characters) {
22 const next = spent + chargedWidth(character)
23
24 if (next > cells && taken > 0) {
25 break
26 }
27
28 spent = next
29 taken += 1
30 }
31
32 return {
33 head: characters.slice(0, taken).join(''),
34 spent,
35 tail: characters.slice(taken).join(''),
36 }
37}