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 / cell-width / cell-width.ts
29 lines · 962 BRaw
1import { FIRST_WIDE_POINT } from './first-wide-point'
2import { WIDE_RANGES } from './wide-ranges'
3import { ZERO_WIDTH_RANGES } from './zero-width-ranges'
4
5/**
6 * How many terminal cells a sanitized line takes: one per code point, two
7 * for a wide one, none for a combining or zero-width one.
8 *
9 * Everything below FIRST_WIDE_POINT, the common case in a diff, never
10 * reaches the tables.
11 *
12 * @param text a line with no tabs or control characters
13 * @returns its width in cells
14 */
15export function cellWidth(text: string) {
16 let width = 0
17
18 for (const character of text) {
19 const point = character.codePointAt(0) ?? 0
20 const holds = ([low, high]: readonly [number, number]) =>
21 point >= low && point <= high
22 const isNarrow = point < FIRST_WIDE_POINT
23 const isZero = !isNarrow && ZERO_WIDTH_RANGES.some(holds)
24 const isWide = !isNarrow && !isZero && WIDE_RANGES.some(holds)
25 width += isZero ? 0 : isWide ? 2 : 1
26 }
27
28 return width
29}