@yanzc/claude-pro 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+ // The terminal of a background session (claude-pro --headless, started by the computer service): no window, the
2
+ // screen it draws goes to whoever is attached (`claude-pro attach`, see attach.js), and what they type comes in as
3
+ // if typed into a terminal. Nobody attached: the drawing goes nowhere, and the session is used from the web page.
4
+ //
5
+ // `output` stands for process.stdout and `input` for process.stdin as the App uses them (a TTY in raw mode, its
6
+ // size, 'resize'). The size is the one of the client that attached or resized last.
7
+
8
+ import { EventEmitter } from 'node:events';
9
+ import { PassThrough } from 'node:stream';
10
+
11
+ export class VirtualTerminal {
12
+ constructor({ columns = 120, rows = 40 } = {}) {
13
+ this.clients = new Set();
14
+ const out = new EventEmitter();
15
+ out.isTTY = true;
16
+ out.columns = columns;
17
+ out.rows = rows;
18
+ out.write = (data) => {
19
+ for (const client of this.clients) {
20
+ try { client.write(data); } catch { /* gone: its close removes it */ }
21
+ }
22
+ return true;
23
+ };
24
+ this.output = out;
25
+ const input = new PassThrough();
26
+ input.isTTY = true;
27
+ input.isRaw = true;
28
+ input.setRawMode = () => input;
29
+ this.input = input;
30
+ }
31
+
32
+ resize(columns, rows) {
33
+ const c = Math.max(20, Math.min(1000, Math.floor(columns) || 0));
34
+ const r = Math.max(10, Math.min(500, Math.floor(rows) || 0));
35
+ if (!columns || !rows || (c === this.output.columns && r === this.output.rows)) return;
36
+ this.output.columns = c;
37
+ this.output.rows = r;
38
+ this.output.emit('resize');
39
+ }
40
+
41
+ // client: {write(data), close()}
42
+ attach(client, { cols, rows } = {}) {
43
+ this.clients.add(client);
44
+ this.resize(cols, rows);
45
+ }
46
+
47
+ detach(client) {
48
+ this.clients.delete(client);
49
+ }
50
+
51
+ // Keys from a client, as a terminal would send them.
52
+ type(data) {
53
+ this.input.write(data);
54
+ }
55
+
56
+ closeAll() {
57
+ for (const client of [...this.clients]) {
58
+ try { client.close(); } catch { /* gone */ }
59
+ }
60
+ this.clients.clear();
61
+ }
62
+ }