MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / wrapText

Function wrapText

source code/utils/terminal.ts:21–63  ·  view source on GitHub ↗

* Inserts newlines in a string to wrap it at the specified width. * Uses ANSI-aware slicing to avoid splitting escape sequences. * @param text The text to wrap. * @param wrapWidth The width at which to wrap lines (in visible characters). * @returns The wrapped text.

(
  text: string,
  wrapWidth: number,
)

Source from the content-addressed store, hash-verified

19 * @returns The wrapped text.
20 */
21function wrapText(
22 text: string,
23 wrapWidth: number,
24): { aboveTheFold: string; remainingLines: number } {
25 const lines = text.split('\n')
26 const wrappedLines: string[] = []
27
28 for (const line of lines) {
29 const visibleWidth = stringWidth(line)
30 if (visibleWidth <= wrapWidth) {
31 wrappedLines.push(line.trimEnd())
32 } else {
33 // Break long lines into chunks of wrapWidth visible characters
34 // using ANSI-aware slicing to preserve escape sequences
35 let position = 0
36 while (position < visibleWidth) {
37 const chunk = sliceAnsi(line, position, position + wrapWidth)
38 wrappedLines.push(chunk.trimEnd())
39 position += wrapWidth
40 }
41 }
42 }
43
44 const remainingLines = wrappedLines.length - MAX_LINES_TO_SHOW
45
46 // If there's only 1 line after the fold, show it directly
47 // instead of showing "... +1 line (ctrl+o to expand)"
48 if (remainingLines === 1) {
49 return {
50 aboveTheFold: wrappedLines
51 .slice(0, MAX_LINES_TO_SHOW + 1)
52 .join('\n')
53 .trimEnd(),
54 remainingLines: 0, // All lines are shown, nothing remaining
55 }
56 }
57
58 // Otherwise show the standard MAX_LINES_TO_SHOW
59 return {
60 aboveTheFold: wrappedLines.slice(0, MAX_LINES_TO_SHOW).join('\n').trimEnd(),
61 remainingLines: Math.max(0, remainingLines),
62 }
63}
64
65/**
66 * Renders the content with line-based truncation for terminal display.

Callers 1

renderTruncatedContentFunction · 0.70

Calls 2

sliceAnsiFunction · 0.85
maxMethod · 0.80

Tested by

no test coverage detected