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 / wrapped-lines / wrapped-lines.ts
73 lines · 1.8 KBRaw
1import { cellWidth } from '../cell-width'
2
3/**
4 * A line cut into rows as Ink's `wrap` cuts the built-in's empty state
5 * before it centers each row.
6 *
7 * It cuts at spaces, keeping the space a row breaks on at its end; a word
8 * wider than a row is cut across rows.
9 *
10 * @param text one sanitized line
11 * @param columns the width, at least one cell; no row holds more
12 * @returns the rows, at least one
13 */
14export function wrappedLines(text: string, columns: number): string[] {
15 const width = Math.max(1, columns)
16 const rows: string[] = ['']
17 const words = text.split(' ')
18
19 for (const [index, word] of words.entries()) {
20 const wordWidth = cellWidth(word)
21 let rowWidth = cellWidth(rows[rows.length - 1] ?? '')
22
23 if (index !== 0) {
24 if (rowWidth >= width) {
25 rows.push('')
26 rowWidth = 0
27 }
28
29 rows[rows.length - 1] += ' '
30 rowWidth += 1
31 }
32
33 if (wordWidth > width) {
34 const breaksHere =
35 1 + Math.floor((wordWidth - (width - rowWidth) - 1) / width)
36
37 if (Math.floor((wordWidth - 1) / width) < breaksHere) {
38 rows.push('')
39 }
40
41 const characters = [...word]
42 let visible = cellWidth(rows[rows.length - 1] ?? '')
43
44 for (const [at, character] of characters.entries()) {
45 const characterWidth = cellWidth(character)
46
47 if (visible + characterWidth <= width) {
48 rows[rows.length - 1] += character
49 } else {
50 rows.push(character)
51 visible = 0
52 }
53
54 visible += characterWidth
55
56 if (visible === width && at < characters.length - 1) {
57 rows.push('')
58 visible = 0
59 }
60 }
61
62 continue
63 }
64
65 if (rowWidth + wordWidth > width && rowWidth > 0 && wordWidth > 0) {
66 rows.push('')
67 }
68
69 rows[rows.length - 1] += word
70 }
71
72 return rows
73}