(diff: Diff)
| 15 | * - Remove clear patches with count 0 |
| 16 | */ |
| 17 | export function optimize(diff: Diff): Diff { |
| 18 | const inputPatchTypeCounts = { |
| 19 | stdout: 0, |
| 20 | clear: 0, |
| 21 | clearTerminal: 0, |
| 22 | cursorHide: 0, |
| 23 | cursorShow: 0, |
| 24 | cursorMove: 0, |
| 25 | cursorTo: 0, |
| 26 | carriageReturn: 0, |
| 27 | hyperlink: 0, |
| 28 | styleStr: 0, |
| 29 | } satisfies Record<Diff[number]['type'], number> |
| 30 | let stdoutMergeCount = 0 |
| 31 | let noopCursorMoveDropCount = 0 |
| 32 | let cursorMoveMergeCount = 0 |
| 33 | let styleStrMergeCount = 0 |
| 34 | let cursorVisibilityCancelCount = 0 |
| 35 | |
| 36 | if (diff.length <= 1) { |
| 37 | for (const patch of diff) { |
| 38 | inputPatchTypeCounts[patch.type] += 1 |
| 39 | } |
| 40 | recordOptimizerStats({ |
| 41 | inputPatchCount: diff.length, |
| 42 | inputPatchTypeCounts, |
| 43 | outputPatchCount: diff.length, |
| 44 | stdoutMergeCount, |
| 45 | noopCursorMoveDropCount, |
| 46 | cursorMoveMergeCount, |
| 47 | styleStrMergeCount, |
| 48 | cursorVisibilityCancelCount, |
| 49 | }) |
| 50 | return diff |
| 51 | } |
| 52 | |
| 53 | const result: Diff = [] |
| 54 | let len = 0 |
| 55 | |
| 56 | for (const patch of diff) { |
| 57 | const type = patch.type |
| 58 | inputPatchTypeCounts[type] += 1 |
| 59 | |
| 60 | // Skip no-ops |
| 61 | if (type === 'stdout') { |
| 62 | if (patch.content === '') continue |
| 63 | } else if (type === 'cursorMove') { |
| 64 | if (patch.x === 0 && patch.y === 0) { |
| 65 | noopCursorMoveDropCount += 1 |
| 66 | continue |
| 67 | } |
| 68 | } else if (type === 'clear') { |
| 69 | if (patch.count === 0) continue |
| 70 | } |
| 71 | |
| 72 | // Try to merge with previous patch |
| 73 | if (len > 0) { |
| 74 | const lastIdx = len - 1 |
no test coverage detected