-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-session.ts
More file actions
48 lines (38 loc) · 1.58 KB
/
Copy pathmulti-session.ts
File metadata and controls
48 lines (38 loc) · 1.58 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
import { TerminalManager } from "@termwright/core";
async function main() {
const manager = TerminalManager;
// Spawn multiple terminal sessions
const session1 = manager.spawn({ cols: 80, rows: 24 });
const session2 = manager.spawn({ cols: 80, rows: 24 });
const session3 = manager.spawn({ cols: 80, rows: 24 });
console.log("Created 3 terminal sessions");
console.log(`Session IDs: ${session1.id}, ${session2.id}, ${session3.id}`);
// Run commands in parallel
const commands = [
session1.runCommand("echo 'Session 1: $(date)'", { timeout: 2000 }),
session2.runCommand("echo 'Session 2: $(whoami)'", { timeout: 2000 }),
session3.runCommand("echo 'Session 3: $(pwd)'", { timeout: 2000 }),
];
const results = await Promise.all(commands);
console.log("\nResults from parallel execution:");
results.forEach((result, index) => {
// Extract the actual command output from the result
const output = typeof result === "string" ? result : result.output || "";
const lines = output.split("\n");
// Find the line with "Session X:"
const sessionLine = lines.find((line) => line.includes(`Session ${index + 1}:`));
console.log(`Session ${index + 1}:`, sessionLine || output);
});
// List all active sessions
const allSessions = manager.list();
console.log(`\nActive sessions: ${allSessions.length}`);
allSessions.forEach((s) => {
console.log(` - ${s.id} (PID: ${s.pid})`);
});
// Close all sessions
session1.destroy();
session2.destroy();
session3.destroy();
console.log("\nAll sessions destroyed");
}
main().catch(console.error);