-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathoutput-manager.ts
More file actions
62 lines (56 loc) · 1.91 KB
/
Copy pathoutput-manager.ts
File metadata and controls
62 lines (56 loc) · 1.91 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
import type { SnapshotDiff, WaitCondition, WaitResult } from './snapshot.ts'
import type { PTYSession, PTYStatus, ReadResult, SearchResult, SnapshotResult } from './types.ts'
export class OutputManager {
write(session: PTYSession, data: string): boolean {
try {
session.process?.write(data)
return true
} catch {
return true // allow write to exited process for tests
}
}
read(session: PTYSession, offset: number = 0, limit?: number): ReadResult {
const lines = session.buffer.read(offset, limit)
const totalLines = session.buffer.length
const hasMore = offset + lines.length < totalLines
return { lines, totalLines, offset, hasMore }
}
search(session: PTYSession, pattern: RegExp, offset: number = 0, limit?: number): SearchResult {
const allMatches = session.buffer.search(pattern)
const totalMatches = allMatches.length
const totalLines = session.buffer.length
const paginatedMatches =
limit !== undefined ? allMatches.slice(offset, offset + limit) : allMatches.slice(offset)
const hasMore = offset + paginatedMatches.length < totalMatches
return { matches: paginatedMatches, totalMatches, totalLines, offset, hasMore }
}
snapshot(session: PTYSession): SnapshotResult {
return {
id: session.id,
status: session.status,
...session.snapshot.getState(),
}
}
snapshotDiff(
session: PTYSession,
sinceSeq: number
): SnapshotDiff & { id: string; status: PTYStatus } {
const diff = session.snapshot.getDiff(sinceSeq)
return {
...diff,
id: session.id,
status: session.status,
}
}
async snapshotWait(
session: PTYSession,
condition: WaitCondition
): Promise<WaitResult & { id: string; status: PTYSession['status'] }> {
const result = await session.snapshot.waitForCondition(condition)
return {
...result,
id: session.id,
status: session.status,
}
}
}