-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathmain.tsx
More file actions
159 lines (140 loc) · 4.94 KB
/
Copy pathmain.tsx
File metadata and controls
159 lines (140 loc) · 4.94 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bun
import { createCliRenderer } from "@opentui/core";
import { createRoot } from "@opentui/react";
import { formatCliError } from "./core/errors";
import {
installJobControlInterruptSupport,
installJobControlSuspendSupport,
type JobControlInterruptSupport,
type JobControlSuspendSupport,
} from "./core/jobControl";
import { pagePlainText } from "./core/pager";
import { sanitizeTerminalText } from "./lib/terminalText";
import { shutdownSession } from "./core/shutdown";
import { renderStaticDiffPager } from "./ui/staticDiffPager";
import { prepareStartupPlan } from "./core/startup";
import { shouldUseMouseForApp } from "./core/terminal";
import { resolveStartupUpdateNotice } from "./core/updateNotice";
import { AppHost } from "./ui/AppHost";
import { SessionBrokerClient } from "./session-broker/brokerClient";
import { serveSessionBrokerDaemon } from "./session-broker/brokerServer";
import {
createInitialSessionSnapshot,
createSessionRegistration,
} from "./hunk-session/sessionRegistration";
import type {
HunkSessionCommandResult,
HunkSessionInfo,
HunkSessionServerMessage,
HunkSessionState,
} from "./hunk-session/types";
import { runSessionCommand } from "./session/commands";
async function main() {
const startupPlan = await prepareStartupPlan();
if (startupPlan.kind === "help") {
process.stdout.write(startupPlan.text);
process.exit(0);
}
if (startupPlan.kind === "daemon-serve") {
const server = serveSessionBrokerDaemon();
await server.stopped;
return;
}
if (startupPlan.kind === "session-command") {
process.stdout.write(await runSessionCommand(startupPlan.input));
process.exit(0);
}
if (startupPlan.kind === "markup-guide") {
const { runMarkupGuideCommand } = await import("./ui/lib/stml/cli");
process.exit(runMarkupGuideCommand({ stdout: (text) => process.stdout.write(text) }));
}
if (startupPlan.kind === "markup-render") {
const { runMarkupRenderCommand } = await import("./ui/lib/stml/cli");
process.exit(
await runMarkupRenderCommand(startupPlan.input, {
stdout: (text) => process.stdout.write(text),
stderr: (text) => process.stderr.write(text),
stdoutIsTTY: Boolean(process.stdout.isTTY),
readStdinText: () => new Response(Bun.stdin.stream()).text(),
}),
);
}
if (startupPlan.kind === "plain-text-pager") {
await pagePlainText(startupPlan.text);
process.exit(0);
}
if (startupPlan.kind === "passthrough") {
process.stdout.write(sanitizeTerminalText(startupPlan.text));
process.exit(0);
}
if (startupPlan.kind === "static-diff-pager") {
process.stdout.write(
await renderStaticDiffPager(startupPlan.text, startupPlan.options, {
customTheme: startupPlan.customTheme,
stderr: process.stderr,
}),
);
process.exit(0);
}
if (startupPlan.kind !== "app") {
throw new Error("Unreachable startup plan.");
}
const { bootstrap, controllingTerminal } = startupPlan;
const hostClient = new SessionBrokerClient<
HunkSessionInfo,
HunkSessionState,
HunkSessionServerMessage,
HunkSessionCommandResult
>(createSessionRegistration(bootstrap), createInitialSessionSnapshot(bootstrap));
hostClient.start();
const renderer = await createCliRenderer({
stdin: controllingTerminal?.stdin,
stdout: process.stdout,
useMouse: shouldUseMouseForApp({
hasControllingTerminal: Boolean(controllingTerminal),
}),
screenMode: "alternate-screen",
useThread: false,
exitOnCtrlC: false,
openConsoleOnError: true,
onDestroy: () => controllingTerminal?.close(),
});
const appRenderer = renderer;
const root = createRoot(appRenderer);
const shutdownSignals: NodeJS.Signals[] = ["SIGINT", "SIGTERM"];
let shuttingDown = false;
let jobControlSuspendSupport: JobControlSuspendSupport = { dispose: () => undefined };
let jobControlInterruptSupport: JobControlInterruptSupport = { dispose: () => undefined };
/** Tear down the renderer before exit so the primary terminal screen comes back cleanly. */
function shutdown() {
if (shuttingDown) {
return;
}
shuttingDown = true;
for (const signal of shutdownSignals) {
process.off(signal, shutdown);
}
jobControlInterruptSupport.dispose();
jobControlSuspendSupport.dispose();
hostClient.stop();
shutdownSession({ root, renderer: appRenderer });
}
for (const signal of shutdownSignals) {
process.once(signal, shutdown);
}
jobControlInterruptSupport = installJobControlInterruptSupport(appRenderer, shutdown);
jobControlSuspendSupport = installJobControlSuspendSupport(appRenderer);
// The app owns the full alternate screen session from this point on.
root.render(
<AppHost
bootstrap={bootstrap}
hostClient={hostClient}
onQuit={shutdown}
startupNoticeResolver={resolveStartupUpdateNotice}
/>,
);
}
await main().catch((error) => {
process.stderr.write(formatCliError(error));
process.exit(1);
});