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
| 1 | import { 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 | */ |
| 14 | export 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 | |
| 22 | let rowWidth = cellWidth(rows[rows.length - 1] ?? '') |
| 23 | |
| 24 | if (index !== 0) { |
| 25 | if (rowWidth >= width) { |
| 26 | rows.push('') |
| 27 | rowWidth = 0 |
| 28 | } |
| 29 | |
| 30 | rows[rows.length - 1] += ' ' |
| 31 | rowWidth += 1 |
| 32 | } |
| 33 | |
| 34 | if (wordWidth > width) { |
| 35 | const breaksHere = |
| 36 | 1 + Math.floor((wordWidth - (width - rowWidth) - 1) / width) |
| 37 | |
| 38 | if (Math.floor((wordWidth - 1) / width) < breaksHere) { |
| 39 | rows.push('') |
| 40 | } |
| 41 | |
| 42 | const characters = [...word] |
| 43 | |
| 44 | let visible = cellWidth(rows[rows.length - 1] ?? '') |
| 45 | |
| 46 | for (const [at, character] of characters.entries()) { |
| 47 | const characterWidth = cellWidth(character) |
| 48 | |
| 49 | if (visible + characterWidth <= width) { |
| 50 | rows[rows.length - 1] += character |
| 51 | } else { |
| 52 | rows.push(character) |
| 53 | visible = 0 |
| 54 | } |
| 55 | |
| 56 | visible += characterWidth |
| 57 | |
| 58 | if (visible === width && at < characters.length - 1) { |
| 59 | rows.push('') |
| 60 | visible = 0 |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | continue |
| 65 | } |
| 66 | |
| 67 | if (rowWidth + wordWidth > width && rowWidth > 0 && wordWidth > 0) { |
| 68 | rows.push('') |
| 69 | } |
| 70 | |
| 71 | rows[rows.length - 1] += word |
| 72 | } |
| 73 | |
| 74 | return rows |
| 75 | } |