From 46c06773cda05bca548f91f812ad796ee4931ced Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Wed, 31 Jul 2024 04:37:29 +0200 Subject: [PATCH] Added speculative/preview to commands --- src/lib/OpenBirchService.ts | 11 ++++++-- src/routes/console/+page.svelte | 34 +++++++++++++++++------- static/OpenBirchWasm/OpenBirchWrapper.js | 4 +-- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/lib/OpenBirchService.ts b/src/lib/OpenBirchService.ts index bc7ec3d..ecbee8b 100644 --- a/src/lib/OpenBirchService.ts +++ b/src/lib/OpenBirchService.ts @@ -1,7 +1,7 @@ import { writable } from 'svelte/store'; // Adds window.runEval into typescript env -declare function runEval(expression: string): any; +declare function runEval(expression: string, stopAssignments?: boolean): any; const OpenBirchHistory: OpenBirchConsoleEntry[] = []; @@ -21,7 +21,14 @@ function createOpenBirchInstance(){ set(OpenBirchHistory); // Publishes changes to subscribers //console.log(OpenBirchHistory); - } + }, + speculateCommand: (command: string): string => { + try { + return runEval(command, true); + } catch (e) { + return ''; + } + }, // TODO: Replace with actual implimentation } } diff --git a/src/routes/console/+page.svelte b/src/routes/console/+page.svelte index 40858b9..0c958a7 100644 --- a/src/routes/console/+page.svelte +++ b/src/routes/console/+page.svelte @@ -12,6 +12,12 @@ if (event.key === 'Enter') { pushCommand(); } + + if (inputField.value == '' && event.key !== 'Enter') return; + + setTimeout(() => { + updatePreviewCommand(); + }, 25); } // Locally start sending command @@ -19,6 +25,11 @@ OpenBirch.sendCommand(inputField.value) inputField.value = ''; } + + let previewText: HTMLElement; + function updatePreviewCommand(){ + previewText.innerHTML = OpenBirch.speculateCommand(inputField.value); + }
@@ -37,16 +48,21 @@
-
- - - - +
+
+
+ + + + +
+
+
- +
diff --git a/static/OpenBirchWasm/OpenBirchWrapper.js b/static/OpenBirchWasm/OpenBirchWrapper.js index 5dd6dc0..6f02b85 100644 --- a/static/OpenBirchWasm/OpenBirchWrapper.js +++ b/static/OpenBirchWasm/OpenBirchWrapper.js @@ -8,7 +8,7 @@ function str2C(s) { } window.runEval = function (inputText, stopAssignments = false) { - console.log("input: " + inputText); + if (!stopAssignments) console.log("input: " + inputText); var inputPtr = str2C(inputText); var resultPointer = Module._eval(inputPtr, stopAssignments); @@ -25,7 +25,7 @@ window.runEval = function (inputText, stopAssignments = false) { i++; } } - console.log("Result from eval:", resultString); + if (!stopAssignments) console.log("Result from eval:", resultString); // Free allocated memory _free(resultPointer);