Skip to content

Commit 9f4baa0

Browse files
committed
Add configuration, command parser, and Monaco editor integration
1 parent 32c2121 commit 9f4baa0

16 files changed

+715
-386
lines changed

bun.lockb

1.51 KB
Binary file not shown.

lib/package-lock.json

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

page/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"install": "^0.13.0",
2323
"nanotar": "^0.1.1",
2424
"rubrc": "file:",
25-
"solid-js": "^1.8.11"
25+
"solid-js": "^1.8.11",
26+
"solid-monaco": "^0.3.0"
2627
}
2728
}

page/src/App.tsx

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@ import type { Component } from "solid-js";
22
import { SetupMyTerminal } from "./xterm";
33
import type { WASIFarmRef } from "@oligami/browser_wasi_shim-threads";
44
import type { Ctx } from "./ctx";
5+
import { MonacoEditor } from "solid-monaco";
6+
import { default_value } from "./config";
57

68
const App = (props: {
79
ctx: Ctx;
810
callback: (wasi_ref: WASIFarmRef) => void;
911
}) => {
12+
const handleMount = (monaco, editor) => {
13+
// Use monaco and editor instances here
14+
};
15+
const handleEditorChange = (value) => {
16+
// Handle editor change
17+
};
18+
1019
return (
1120
<>
12-
<p class="text-4xl text-green-700 text-center py-20">Hello tailwind!</p>
13-
<SetupMyTerminal xterm_id={props.ctx.terminal_id} callback={props.callback} />
21+
<MonacoEditor
22+
language="rust"
23+
value={default_value}
24+
height="30vh"
25+
onMount={handleMount}
26+
/>
27+
{/* <p class="text-4xl text-green-700 text-center py-20">Hello tailwind!</p> */}
28+
<SetupMyTerminal ctx={props.ctx} callback={props.callback} />
1429
</>
1530
);
1631
};

page/src/cmd_parser.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { SharedObject, SharedObjectRef } from "@oligami/shared-object";
2+
import type { Ctx } from "./ctx";
3+
4+
let waiter: SharedObject;
5+
let is_all_done = false;
6+
7+
export const parser_setup = async (ctx: Ctx) => {
8+
const n = 1;
9+
10+
const resolvers: PromiseWithResolvers<void>[] = [];
11+
for (let i = 0; i < n; i++) {
12+
resolvers.push(Promise.withResolvers<void>());
13+
}
14+
15+
waiter = new SharedObject(
16+
{
17+
rustc: () => {
18+
resolvers[0].resolve();
19+
},
20+
is_all_done: (): boolean => {
21+
return is_all_done;
22+
},
23+
},
24+
ctx.waiter_id,
25+
);
26+
27+
await Promise.all(resolvers.map((r) => r.promise));
28+
29+
is_all_done = true;
30+
31+
await all_done(ctx);
32+
};
33+
34+
let cmd_parser: SharedObject;
35+
36+
const all_done = async (ctx: Ctx) => {
37+
const rustc = new SharedObjectRef(ctx.rustc_id).proxy<(...string) => void>();
38+
const terminal = new SharedObjectRef(ctx.terminal_id).proxy<
39+
(string) => void
40+
>();
41+
const ls = new SharedObjectRef(ctx.ls_id).proxy<(...string) => void>();
42+
const tree = new SharedObjectRef(ctx.tree_id).proxy<(...string) => void>();
43+
44+
cmd_parser = new SharedObject((...args) => {
45+
(async (args: string[]) => {
46+
console.log(args);
47+
48+
const cmd = args[0];
49+
50+
console.log(cmd);
51+
52+
if (cmd === "rustc") {
53+
console.log("rustc");
54+
await terminal("executing rustc...\r\n");
55+
await rustc(...args.slice(1));
56+
} else if (cmd === "echo") {
57+
console.log("echo");
58+
await terminal(`${args.slice(1).join(" ")}\r\n`);
59+
} else if (cmd === "ls") {
60+
console.log("ls");
61+
await terminal("executing ls...\r\n");
62+
await ls(...args.slice(1));
63+
} else if (cmd === "tree") {
64+
console.log("tree");
65+
await terminal("executing tree...\r\n");
66+
await tree(...args.slice(1));
67+
} else {
68+
await terminal(`command not found: ${cmd}\r\n`);
69+
}
70+
await terminal(">");
71+
})(args);
72+
}, ctx.cmd_parser_id);
73+
74+
await terminal("rustc -h\r\n");
75+
await rustc("-h");
76+
};

page/src/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const default_value = `// /main.rs
2+
fn main() {
3+
println!("Hello, world!");
4+
}
5+
`;

page/src/ctx.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ export type Ctx = {
22
terminal_id: string;
33
rustc_id: string;
44
waiter_id: string;
5-
}
5+
cmd_parser_id: string;
6+
tree_id: string;
7+
ls_id: string;
8+
};
69

710
const gen_id = () => Math.random().toString(36).substring(7);
811

@@ -11,5 +14,8 @@ export const gen_ctx = (): Ctx => {
1114
terminal_id: gen_id(),
1215
rustc_id: gen_id(),
1316
waiter_id: gen_id(),
17+
cmd_parser_id: gen_id(),
18+
tree_id: gen_id(),
19+
ls_id: gen_id(),
1420
};
15-
}
21+
};

page/src/index.tsx

+3-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import App from "./App";
66
import { gen_ctx } from "./ctx";
77
import { SharedObject, SharedObjectRef } from "@oligami/shared-object";
88
import MainWorker from "./worker?worker";
9+
import { parser_setup } from "./cmd_parser";
10+
import "./monaco_worker";
911

1012
const root = document.getElementById("root");
1113

@@ -20,21 +22,7 @@ const ctx = gen_ctx();
2022
// create worker
2123
const worker = new MainWorker();
2224

23-
const waiter = new SharedObject(
24-
{
25-
rustc: () => {
26-
const rustc = new SharedObjectRef(ctx.rustc_id).proxy<
27-
(...string) => void
28-
>();
29-
const terminal = new SharedObjectRef(ctx.terminal_id).proxy<
30-
(string) => void
31-
>();
32-
terminal("rustc -h\r\n");
33-
rustc("-h");
34-
},
35-
},
36-
ctx.waiter_id,
37-
);
25+
parser_setup(ctx);
3826

3927
// send message to worker
4028
worker.postMessage({ ctx });

page/src/monaco_worker.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as monaco from 'monaco-editor';
2+
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
3+
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
4+
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
5+
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
6+
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
7+
8+
// @ts-ignore
9+
self.MonacoEnvironment = {
10+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
11+
getWorker(_: any, label: string) {
12+
if (label === 'json') {
13+
return new jsonWorker();
14+
}
15+
if (label === 'css' || label === 'scss' || label === 'less') {
16+
return new cssWorker();
17+
}
18+
if (label === 'html' || label === 'handlebars' || label === 'razor') {
19+
return new htmlWorker();
20+
}
21+
if (label === 'typescript' || label === 'javascript') {
22+
return new tsWorker();
23+
}
24+
return new editorWorker();
25+
}
26+
};

page/src/rustc.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ import { get_rustc_wasm } from "../../lib/src/get_rustc_wasm";
33
import { WASIFarmAnimal } from "@oligami/browser_wasi_shim-threads";
44
import type { Ctx } from "./ctx";
55

6-
import thread_spawn_path from './thread_spawn.ts?worker&url'
6+
import thread_spawn_path from "./thread_spawn.ts?worker&url";
7+
import util_cmd_worker from "./util_cmd.ts?worker";
78

89
let terminal: (string) => void;
910
let compiler: WebAssembly.Module;
1011
const wasi_refs = [];
1112
let ctx: Ctx;
1213
let rustc_shared: SharedObject;
1314
let waiter: {
14-
rustc: () => void
15+
rustc: () => void;
1516
};
1617

17-
globalThis.addEventListener('message', async (event) => {
18+
globalThis.addEventListener("message", async (event) => {
1819
if (event.data.ctx) {
1920
ctx = event.data.ctx;
2021
terminal = new SharedObjectRef(ctx.terminal_id).proxy<(string) => void>();
21-
terminal("loading rustc\r\n");
22+
await terminal("loading rustc\r\n");
2223
waiter = new SharedObjectRef(ctx.waiter_id).proxy<{
23-
rustc: () => void
24+
rustc: () => void;
2425
}>();
2526
compiler = await get_rustc_wasm();
2627
} else if (event.data.wasi_ref) {
@@ -33,13 +34,19 @@ globalThis.addEventListener('message', async (event) => {
3334
await new Promise((resolve) => setTimeout(resolve, 100));
3435
}
3536

36-
terminal("loaded rustc\r\n");
37+
await terminal("loaded rustc\r\n");
3738

3839
while (wasi_refs.length === 1) {
3940
await new Promise((resolve) => setTimeout(resolve, 100));
4041
}
4142

42-
terminal("loaded wasi\r\n");
43+
await terminal("loaded wasi\r\n");
44+
45+
const util_worker = new util_cmd_worker();
46+
util_worker.postMessage({
47+
wasi_refs,
48+
ctx,
49+
});
4350

4451
const wasi = new WASIFarmAnimal(
4552
wasi_refs,

0 commit comments

Comments
 (0)