-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterminal.ts
More file actions
136 lines (112 loc) · 3.03 KB
/
Copy pathterminal.ts
File metadata and controls
136 lines (112 loc) · 3.03 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { forEach } from "@effectionx/stream-helpers";
import {
createTerm,
cursor,
type CursorEvent,
DSR,
type Op,
saveCursorPosition,
} from "clayterm";
import {
createChannel,
ensure,
type Operation,
scoped,
spawn,
until,
} from "effection";
import process from "node:process";
import { resize } from "./resize.ts";
import { useInput } from "./use-input.ts";
const renders = createChannel<Op[], never>();
export function render(ops: Op[]): Operation<void> {
return renders.send(ops);
}
export function withRegion<T>(
height: number,
body: () => Operation<T>,
): Operation<T> {
return process.stdout.isTTY
? withTerminalRegion(height, body)
: withPipeRegion(height, body);
}
function withTerminalRegion<T>(
...[height, body]: Parameters<typeof withRegion<T>>
): Operation<T> {
let encoder = new TextEncoder();
let hideCursor = cursor(false);
let saveCursor = saveCursorPosition();
return scoped(function* () {
let term = yield* until(createTerm({
height,
width: process.stdout.columns,
}));
process.stdout.write(hideCursor.apply);
yield* ensure(() => {
process.stdout.write(hideCursor.revert);
});
yield* spawn(() =>
forEach(function* ({ width }) {
term = yield* until(createTerm({ height, width }));
}, resize())
);
// allocate region
let lines = encoder.encode("\n".repeat(height));
process.stdout.write(lines);
// save the cursor and make sure it's in the same place when we're done.
process.stdout.write(saveCursor.apply);
yield* ensure(() => {
process.stdout.write(saveCursor.revert);
});
// find the ending row
let end = yield* queryCursorPosition();
yield* spawn(() =>
forEach(function* (ops) {
let { output } = term.render(ops, { row: end.row - height });
process.stdout.write(output);
}, renders)
);
return yield* body();
});
}
function withPipeRegion<T>(
...[height, body]: Parameters<typeof withRegion<T>>
): Operation<T> {
return scoped(function* () {
let last: Op[] = [];
yield* spawn(() =>
forEach(function* (ops) {
last = ops;
}, renders)
);
let result = yield* body();
let term = yield* until(createTerm({
height,
width: process.stdout.columns ?? 80,
}));
let { output } = term.render(last, { mode: "line" });
process.stdout.write(output);
return result;
});
}
function queryCursorPosition(): Operation<CursorEvent> {
return scoped(function* () {
let events = yield* useInput();
let originalRawMode = process.stdin.isRaw;
try {
process.stdin.setRawMode(true);
process.stdout.write(DSR());
let next = yield* events.next();
while (!next.done) {
let event = next.value;
if (event.type === "cursor") {
return event;
}
next = yield* events.next();
}
throw new Error(`input closed, but never received cursor position`);
} finally {
process.stdin.setRawMode(originalRawMode);
}
});
}