forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-layout.ts
More file actions
149 lines (126 loc) · 3.55 KB
/
Copy pathtext-layout.ts
File metadata and controls
149 lines (126 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import stringWidth from 'string-width'
// Keep measurement logic centralised so components can share consistent wrap behavior.
function measureLines(text: string, cols: number): number {
if (text.length === 0) return 1
let lines = 1
let current = 0
const tokens = text.split(/(\s+)/)
const emitHardWrap = () => {
lines += 1
current = 0
}
const appendSegment = (
segment: string,
{ flushBeforeOversize = true }: { flushBeforeOversize?: boolean } = {},
) => {
if (!segment) return
const segmentWidth = stringWidth(segment)
if (segmentWidth > cols) {
if (flushBeforeOversize && current > 0) emitHardWrap()
let acc = 0
for (const ch of Array.from(segment)) {
const w = stringWidth(ch)
if (acc + w > cols) {
emitHardWrap()
acc = 0
}
acc += w
}
current = acc
return
}
if (current + segmentWidth > cols) emitHardWrap()
current += segmentWidth
}
for (const token of tokens) {
if (!token) continue
if (token.includes('\n')) {
const parts = token.split('\n')
for (let i = 0; i < parts.length; i++) {
appendSegment(parts[i], { flushBeforeOversize: false })
if (i < parts.length - 1) emitHardWrap()
}
continue
}
appendSegment(token)
}
return lines
}
export function getLastNVisualLines(text: string, cols: number, n: number): { lines: string[]; hasMore: boolean } {
if (n <= 0 || cols <= 0) return { lines: [], hasMore: false }
const lines: string[] = []
if (!text) return { lines, hasMore: false }
const tokens = text.split(/(\s+)/)
let current = ''
let currentWidth = 0
const pushLine = () => {
lines.push(current)
current = ''
currentWidth = 0
}
const appendSegment = (segment: string) => {
if (!segment) return
const segWidth = stringWidth(segment)
if (segWidth > cols) {
for (const ch of Array.from(segment)) {
const w = stringWidth(ch)
if (currentWidth + w > cols) pushLine()
current += ch
currentWidth += w
}
return
}
if (currentWidth + segWidth > cols) pushLine()
current += segment
currentWidth += segWidth
}
for (const token of tokens) {
if (!token) continue
if (token.includes('\n')) {
const parts = token.split('\n')
for (let i = 0; i < parts.length; i++) {
appendSegment(parts[i])
if (i < parts.length - 1) pushLine()
}
continue
}
appendSegment(token)
}
if (current.length > 0 || lines.length === 0) pushLine()
const hasMore = lines.length > n
const lastLines = lines.slice(-n)
return { lines: lastLines, hasMore }
}
export function computeInputLayoutMetrics({
layoutContent,
cursorProbe,
cols,
maxHeight,
minHeight = 1,
}: {
layoutContent: string
cursorProbe: string
cols: number
maxHeight: number
minHeight?: number
}): { heightLines: number; gutterEnabled: boolean } {
const safeMaxHeight = Math.max(1, maxHeight)
const effectiveMinHeight = Math.max(
1,
Math.min(minHeight ?? 1, safeMaxHeight),
)
const totalLines = measureLines(layoutContent, cols)
const cursorLines = measureLines(cursorProbe, cols)
// Add bottom gutter when cursor is on line 2 of exactly 2 lines
const gutterEnabled =
totalLines === 2 && cursorLines === 2 && totalLines + 1 <= safeMaxHeight
const rawHeight = Math.min(
totalLines + (gutterEnabled ? 1 : 0),
safeMaxHeight,
)
const heightLines = Math.max(effectiveMinHeight, rawHeight)
return {
heightLines,
gutterEnabled,
}
}