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