(text: string, cols: number, n: number)
| 58 | } |
| 59 | |
| 60 | export function getLastNVisualLines(text: string, cols: number, n: number): { lines: string[]; hasMore: boolean } { |
| 61 | if (n <= 0 || cols <= 0) return { lines: [], hasMore: false } |
| 62 | const lines: string[] = [] |
| 63 | if (!text) return { lines, hasMore: false } |
| 64 | |
| 65 | const tokens = text.split(/(\s+)/) |
| 66 | let current = '' |
| 67 | let currentWidth = 0 |
| 68 | |
| 69 | const pushLine = () => { |
| 70 | lines.push(current) |
| 71 | current = '' |
| 72 | currentWidth = 0 |
| 73 | } |
| 74 | |
| 75 | const appendSegment = (segment: string) => { |
| 76 | if (!segment) return |
| 77 | const segWidth = stringWidth(segment) |
| 78 | |
| 79 | if (segWidth > cols) { |
| 80 | for (const ch of Array.from(segment)) { |
| 81 | const w = stringWidth(ch) |
| 82 | if (currentWidth + w > cols) pushLine() |
| 83 | current += ch |
| 84 | currentWidth += w |
| 85 | } |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | if (currentWidth + segWidth > cols) pushLine() |
| 90 | current += segment |
| 91 | currentWidth += segWidth |
| 92 | } |
| 93 | |
| 94 | for (const token of tokens) { |
| 95 | if (!token) continue |
| 96 | if (token.includes('\n')) { |
| 97 | const parts = token.split('\n') |
| 98 | for (let i = 0; i < parts.length; i++) { |
| 99 | appendSegment(parts[i]) |
| 100 | if (i < parts.length - 1) pushLine() |
| 101 | } |
| 102 | continue |
| 103 | } |
| 104 | appendSegment(token) |
| 105 | } |
| 106 | |
| 107 | if (current.length > 0 || lines.length === 0) pushLine() |
| 108 | const hasMore = lines.length > n |
| 109 | const lastLines = lines.slice(-n) |
| 110 | return { lines: lastLines, hasMore } |
| 111 | } |
| 112 | |
| 113 | export function computeInputLayoutMetrics({ |
| 114 | layoutContent, |
no test coverage detected