MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / measureLines

Function measureLines

cli/src/utils/text-layout.ts:4–58  ·  view source on GitHub ↗
(text: string, cols: number)

Source from the content-addressed store, hash-verified

2
3// Keep measurement logic centralised so components can share consistent wrap behavior.
4function measureLines(text: string, cols: number): number {
5 if (text.length === 0) return 1
6
7 let lines = 1
8 let current = 0
9 const tokens = text.split(/(\s+)/)
10
11 const emitHardWrap = () => {
12 lines += 1
13 current = 0
14 }
15
16 const appendSegment = (
17 segment: string,
18 { flushBeforeOversize = true }: { flushBeforeOversize?: boolean } = {},
19 ) => {
20 if (!segment) return
21
22 const segmentWidth = stringWidth(segment)
23 if (segmentWidth > cols) {
24 if (flushBeforeOversize && current > 0) emitHardWrap()
25 let acc = 0
26 for (const ch of Array.from(segment)) {
27 const w = stringWidth(ch)
28 if (acc + w > cols) {
29 emitHardWrap()
30 acc = 0
31 }
32 acc += w
33 }
34 current = acc
35 return
36 }
37
38 if (current + segmentWidth > cols) emitHardWrap()
39 current += segmentWidth
40 }
41
42 for (const token of tokens) {
43 if (!token) continue
44
45 if (token.includes('\n')) {
46 const parts = token.split('\n')
47 for (let i = 0; i < parts.length; i++) {
48 appendSegment(parts[i], { flushBeforeOversize: false })
49 if (i < parts.length - 1) emitHardWrap()
50 }
51 continue
52 }
53
54 appendSegment(token)
55 }
56
57 return lines
58}
59
60export function getLastNVisualLines(text: string, cols: number, n: number): { lines: string[]; hasMore: boolean } {
61 if (n <= 0 || cols <= 0) return { lines: [], hasMore: false }

Callers 1

Calls 2

appendSegmentFunction · 0.85
emitHardWrapFunction · 0.85

Tested by

no test coverage detected