forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.ts
More file actions
93 lines (81 loc) · 2.53 KB
/
Copy pathoptimizer.ts
File metadata and controls
93 lines (81 loc) · 2.53 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
import type { Diff } from './frame.js'
/**
* Optimize a diff by applying all optimization rules in a single pass.
* This reduces the number of patches that need to be written to the terminal.
*
* Rules applied:
* - Remove empty stdout patches
* - Merge consecutive cursorMove patches
* - Remove no-op cursorMove (0,0) patches
* - Concat adjacent style patches (transition diffs — can't drop either)
* - Dedupe consecutive hyperlinks with same URI
* - Cancel cursor hide/show pairs
* - Remove clear patches with count 0
*/
export function optimize(diff: Diff): Diff {
if (diff.length <= 1) {
return diff
}
const result: Diff = []
let len = 0
for (const patch of diff) {
const type = patch.type
// Skip no-ops
if (type === 'stdout') {
if (patch.content === '') continue
} else if (type === 'cursorMove') {
if (patch.x === 0 && patch.y === 0) continue
} else if (type === 'clear') {
if (patch.count === 0) continue
}
// Try to merge with previous patch
if (len > 0) {
const lastIdx = len - 1
const last = result[lastIdx]!
const lastType = last.type
// Merge consecutive cursorMove
if (type === 'cursorMove' && lastType === 'cursorMove') {
result[lastIdx] = {
type: 'cursorMove',
x: last.x + patch.x,
y: last.y + patch.y,
}
continue
}
// Collapse consecutive cursorTo (only the last one matters)
if (type === 'cursorTo' && lastType === 'cursorTo') {
result[lastIdx] = patch
continue
}
// Concat adjacent style patches. styleStr is a transition diff
// (computed by diffAnsiCodes(from, to)), not a setter — dropping
// the first is only sound if its undo-codes are a subset of the
// second's, which is NOT guaranteed. e.g. [\e[49m, \e[2m]: dropping
// the bg reset leaks it into the next \e[2J/\e[2K via BCE.
if (type === 'styleStr' && lastType === 'styleStr') {
result[lastIdx] = { type: 'styleStr', str: last.str + patch.str }
continue
}
// Dedupe hyperlinks
if (
type === 'hyperlink' &&
lastType === 'hyperlink' &&
patch.uri === last.uri
) {
continue
}
// Cancel cursor hide/show pairs
if (
(type === 'cursorShow' && lastType === 'cursorHide') ||
(type === 'cursorHide' && lastType === 'cursorShow')
) {
result.pop()
len--
continue
}
}
result.push(patch)
len++
}
return result
}