From 50c30e3a7eb8b63074aa79bf524bfd1804629616 Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Mon, 15 Jul 2024 04:08:10 +0200 Subject: [PATCH] Better console now usable --- OpenBirchWebsite/Pages/Home.razor | 66 +++++++++---------- OpenBirchWebsite/Pages/OpenBirch.razor | 40 +++++++++-- OpenBirchWebsite/Program.cs | 3 + OpenBirchWebsite/Services/OpenBirchConsole.cs | 41 ++++++++++++ OpenBirchWebsite/Services/StateContainer.cs | 7 ++ OpenBirchWebsite/_Imports.razor | 1 + .../wwwroot/JavaScript🤮/helperFunctions.js | 1 + 7 files changed, 120 insertions(+), 39 deletions(-) create mode 100644 OpenBirchWebsite/Services/OpenBirchConsole.cs create mode 100644 OpenBirchWebsite/Services/StateContainer.cs create mode 100644 OpenBirchWebsite/wwwroot/JavaScript🤮/helperFunctions.js diff --git a/OpenBirchWebsite/Pages/Home.razor b/OpenBirchWebsite/Pages/Home.razor index 76cc775..521660a 100644 --- a/OpenBirchWebsite/Pages/Home.razor +++ b/OpenBirchWebsite/Pages/Home.razor @@ -1,4 +1,5 @@ @page "/" +@inject OpenBirchConsole console @inject IJSRuntime jsRuntime @inject NavigationManager nav @@ -10,13 +11,13 @@
- @for (int i = 0; i < consoleHistory.Count; i++) + @for (int i = 0; i < console.history.Count; i++) { int temp = i; - @((consoleHistory[temp].source == ConsoleSource.User) ? ">" : "#") @consoleHistory[temp].text + @((console.history[temp].source == ConsoleSource.User) ? ">" : "#") @console.history[temp].text }
- +
@@ -24,6 +25,7 @@ @code { public string inputField = " "; public bool hasInteracted = false; // Has the user interacted with the input field? + private Task AutoTypingTask; readonly string[] exampleInputs = { "2+2;", @@ -50,17 +52,16 @@ Console.WriteLine(e); } - public struct consoleLine - { - public ConsoleSource source; - public string text; - } - - public List consoleHistory = new(); - protected override async Task OnInitializedAsync() { - StartAutoTyping(); + AutoTypingTask = StartAutoTyping(); + } + + private async void OnInputClicked() + { + hasInteracted = true; + await AutoTypingTask; + nav.NavigateTo("openbirch"); } async void OnKeyDown(KeyboardEventArgs args) @@ -68,28 +69,21 @@ if (args.Key == "Enter") { await Task.Delay(100); - pushCommand(inputField); - inputField = ""; + sendCommand(inputField); StateHasChanged(); } } - private void pushCommand(string command) + private async void sendCommand(string command) { - inputField = ""; // Clear input field - consoleHistory.Add(new consoleLine() { source = ConsoleSource.User, text = command }); - InvokeAsync(StateHasChanged); - ExecuteCommand(command); - } - - private async Task ExecuteCommand(string command) - { - string result = await jsRuntime.InvokeAsync("runEval", command); - consoleHistory.Add(new consoleLine() { source = ConsoleSource.OpenBirch, text = result }); + inputField = ""; + await console.pushCommand(command); await InvokeAsync(StateHasChanged); } - private async void StartAutoTyping() + int typingDelay = 200; + + private async Task StartAutoTyping() { await Task.Delay(1000); @@ -101,21 +95,25 @@ await InvokeAsync(StateHasChanged); // Trigger UI update await Task.Delay(200); // Non-blocking delay - if (!hasInteracted) continue; + // Makes it check if user inputted while waiting + const int waitTime = 10; + int waitedTime = 0; + while (waitedTime < typingDelay) + { + await Task.Delay(waitTime); + waitedTime += waitTime; + if (!hasInteracted) goto earlyExit; + } inputField = ""; // Clear input and let user input await InvokeAsync(StateHasChanged); return; + + earlyExit:; } - pushCommand(inputField); + sendCommand(inputField); await Task.Delay(500); } } - - public enum ConsoleSource - { - User, // From the user - OpenBirch, // Guess - } } \ No newline at end of file diff --git a/OpenBirchWebsite/Pages/OpenBirch.razor b/OpenBirchWebsite/Pages/OpenBirch.razor index 041b6c5..58889fb 100644 --- a/OpenBirchWebsite/Pages/OpenBirch.razor +++ b/OpenBirchWebsite/Pages/OpenBirch.razor @@ -1,20 +1,50 @@ @page "/openbirch" +@inject OpenBirchConsole console +@inject IJSRuntime js +
-
+
- @for (int i = 0; i < 100; i++) + @for (int i = 0; i < console.history.Count; i++) { int temp = i; - test @temp + ConsoleLine line = console.history[temp]; + @((line.source == ConsoleSource.User) ? ">" : "") @line.text + @if (line.source == ConsoleSource.OpenBirch) + { +
+ } }
- +
@code { - public string test = ""; + public string inputField = ""; + + async void OnKeyDown(KeyboardEventArgs args) + { + if (args.Key == "Enter") + { + await Task.Delay(100); + sendCommand(inputField); + StateHasChanged(); + } + } + + private async void sendCommand(string command) + { + inputField = ""; + await console.pushCommand(command); + await InvokeAsync(StateHasChanged); + } + + protected override async Task OnInitializedAsync() + { + + } } diff --git a/OpenBirchWebsite/Program.cs b/OpenBirchWebsite/Program.cs index 32a917f..eb625b0 100644 --- a/OpenBirchWebsite/Program.cs +++ b/OpenBirchWebsite/Program.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using MudBlazor.Services; +using OpenBirchWebsite.Services; namespace OpenBirchWebsite { @@ -13,6 +14,8 @@ namespace OpenBirchWebsite builder.RootComponents.Add("head::after"); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); + builder.Services.AddScoped(); + builder.Services.AddMudServices(); diff --git a/OpenBirchWebsite/Services/OpenBirchConsole.cs b/OpenBirchWebsite/Services/OpenBirchConsole.cs new file mode 100644 index 0000000..df0d4e1 --- /dev/null +++ b/OpenBirchWebsite/Services/OpenBirchConsole.cs @@ -0,0 +1,41 @@ +using Microsoft.JSInterop; + +namespace OpenBirchWebsite.Services +{ + public class OpenBirchConsole(IJSRuntime js) : IDisposable + { + public List history = new(); + private readonly IJSRuntime js = js; + + public Task pushCommand(string command) + { + history.Add(new ConsoleLine() { source = ConsoleSource.User, text = command }); + return ExecuteCommand(command); + } + + private async Task ExecuteCommand(string command) + { + string result = await js.InvokeAsync("runEval", command); + history.Add(new ConsoleLine() { source = ConsoleSource.OpenBirch, text = result }); + } + + public void Dispose() + { + // The following prevents derived types that introduce a + // finalizer from needing to re-implement IDisposable. + GC.SuppressFinalize(this); + } + } + + public struct ConsoleLine + { + public ConsoleSource source; + public string text; + } + + public enum ConsoleSource + { + User, // From the user + OpenBirch, // Guess + } +} diff --git a/OpenBirchWebsite/Services/StateContainer.cs b/OpenBirchWebsite/Services/StateContainer.cs new file mode 100644 index 0000000..c71c632 --- /dev/null +++ b/OpenBirchWebsite/Services/StateContainer.cs @@ -0,0 +1,7 @@ +namespace OpenBirchWebsite.Services +{ + public class StateContainer + { + public readonly Dictionary ObjectTunnel = new(); + } +} diff --git a/OpenBirchWebsite/_Imports.razor b/OpenBirchWebsite/_Imports.razor index df32cae..395a072 100644 --- a/OpenBirchWebsite/_Imports.razor +++ b/OpenBirchWebsite/_Imports.razor @@ -9,3 +9,4 @@ @using OpenBirchWebsite @using OpenBirchWebsite.Layout @using MudBlazor +@using OpenBirchWebsite.Services \ No newline at end of file diff --git a/OpenBirchWebsite/wwwroot/JavaScript🤮/helperFunctions.js b/OpenBirchWebsite/wwwroot/JavaScript🤮/helperFunctions.js new file mode 100644 index 0000000..6d42a20 --- /dev/null +++ b/OpenBirchWebsite/wwwroot/JavaScript🤮/helperFunctions.js @@ -0,0 +1 @@ +clickElement = (elementId) => document.getElementById(elementId).click(); \ No newline at end of file