-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-matching.ts
More file actions
61 lines (48 loc) · 1.44 KB
/
Copy pathpattern-matching.ts
File metadata and controls
61 lines (48 loc) · 1.44 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
import { TerminalManager } from "@termwright/core";
async function main() {
const manager = TerminalManager;
const session = manager.spawn({
cols: 80,
rows: 24,
});
console.log(`Session created: ${session.id}`);
// Example 1: Wait for command completion
session.write("echo 'TASK_START'\n");
session.write("sleep 2\n");
session.write("echo 'TASK_COMPLETE'\n");
console.log("Waiting for task completion...");
const matched = await session.waitForPattern(/TASK_COMPLETE/, {
timeout: 5000,
});
if (matched) {
console.log("Task completed successfully!");
}
// Example 2: Wait for multiple patterns
session.write(
"echo 'Processing...'; sleep 1; echo 'Status: SUCCESS' || echo 'Status: FAILED'\n"
);
const output = await session.waitForPattern(/Status: (SUCCESS|FAILED)/, {
timeout: 3000,
});
if (output) {
const match = output.match(/Status: (\w+)/);
if (match) {
console.log(`Process finished with status: ${match[1]}`);
}
}
// Example 3: Git operations with pattern matching
session.write("git status\n");
await session.waitForPattern(/On branch|Not a git repository/, {
timeout: 2000,
});
const gitOutput = session.readOutput();
if (gitOutput.includes("On branch")) {
console.log("In a git repository");
} else {
console.log("Not in a git repository");
}
// Clean up
session.destroy();
console.log("Session destroyed");
}
main().catch(console.error);