MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / wrapTextWithAnsi

Function wrapTextWithAnsi

packages/pi-tui/src/utils.ts:720–743  ·  view source on GitHub ↗
(text: string, width: number)

Source from the content-addressed store, hash-verified

718 * @returns Array of wrapped lines (NOT padded to width)
719 */
720export function wrapTextWithAnsi(text: string, width: number): string[] {
721 if (!text) {
722 return [""];
723 }
724
725 // Handle newlines by processing each line separately
726 // Track ANSI state across lines so styles carry over after literal newlines
727 const inputLines = text.split("\n");
728 const result: string[] = [];
729 const tracker = new AnsiCodeTracker();
730
731 for (const inputLine of inputLines) {
732 // Prepend active ANSI codes from previous lines (except for first line)
733 const prefix = result.length > 0 ? tracker.getActiveCodes() : "";
734 const wrappedLines = wrapSingleLine(prefix + inputLine, width);
735 for (const wrappedLine of wrappedLines) {
736 result.push(wrappedLine);
737 }
738 // Update tracker with codes from this line for next iteration
739 updateTrackerFromText(inputLine, tracker);
740 }
741
742 return result.length > 0 ? result : [""];
743}
744
745function wrapSingleLine(line: string, width: number): string[] {
746 if (!line) {

Callers 15

wrap-ansi.test.tsFile · 0.90
renderMethod · 0.90
renderMethod · 0.90
renderTokenMethod · 0.90
renderListMethod · 0.90
wrapCellTextMethod · 0.90
renderTableMethod · 0.90
renderMainListMethod · 0.90
renderMethod · 0.90
wrapDescriptionFunction · 0.90
wrapFunction · 0.90

Calls 4

getActiveCodesMethod · 0.95
wrapSingleLineFunction · 0.85
updateTrackerFromTextFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected