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

Method render

packages/pi-tui/src/components/text.ts:45–105  ·  view source on GitHub ↗
(width: number)

Source from the content-addressed store, hash-verified

43 }
44
45 render(width: number): string[] {
46 // Check cache
47 if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
48 return this.cachedLines;
49 }
50
51 // Don't render anything if there's no actual text
52 if (!this.text || this.text.trim() === "") {
53 const result: string[] = [];
54 this.cachedText = this.text;
55 this.cachedWidth = width;
56 this.cachedLines = result;
57 return result;
58 }
59
60 // Replace tabs with 3 spaces
61 const normalizedText = this.text.replace(/\t/g, " ");
62
63 // Calculate content width (subtract left/right margins)
64 const contentWidth = Math.max(1, width - this.paddingX * 2);
65
66 // Wrap text (this preserves ANSI codes but does NOT pad)
67 const wrappedLines = wrapTextWithAnsi(normalizedText, contentWidth);
68
69 // Add margins and background to each line
70 const leftMargin = " ".repeat(this.paddingX);
71 const rightMargin = " ".repeat(this.paddingX);
72 const contentLines: string[] = [];
73
74 for (const line of wrappedLines) {
75 // Add margins
76 const lineWithMargins = leftMargin + line + rightMargin;
77
78 // Apply background if specified (this also pads to full width)
79 if (this.customBgFn) {
80 contentLines.push(applyBackgroundToLine(lineWithMargins, width, this.customBgFn));
81 } else {
82 // No background - just pad to width with spaces
83 const visibleLen = visibleWidth(lineWithMargins);
84 const paddingNeeded = Math.max(0, width - visibleLen);
85 contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
86 }
87 }
88
89 // Add top/bottom padding (empty lines)
90 const emptyLine = " ".repeat(Math.max(0, width));
91 const emptyLines: string[] = [];
92 for (let i = 0; i < this.paddingY; i++) {
93 const line = this.customBgFn ? applyBackgroundToLine(emptyLine, width, this.customBgFn) : emptyLine;
94 emptyLines.push(line);
95 }
96
97 const result = [...emptyLines, ...contentLines, ...emptyLines];
98
99 // Update cache
100 this.cachedText = this.text;
101 this.cachedWidth = width;
102 this.cachedLines = result;

Callers 3

renderLifecycleLineFunction · 0.95
renderMethod · 0.95
renderMethod · 0.95

Calls 4

wrapTextWithAnsiFunction · 0.90
applyBackgroundToLineFunction · 0.90
visibleWidthFunction · 0.90
pushMethod · 0.45

Tested by

no test coverage detected