From e7940d5c4cbe291870f680803a8cbe569b191399 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 31 Jan 2024 22:32:31 +0100 Subject: [PATCH] Add command system --- TextLore/Commands/TestCommand.cs | 15 +++ TextLore/Components/App.razor | 6 +- TextLore/Components/Pages/Counter.razor | 1 - TextLore/Components/Pages/Home.razor | 9 +- TextLore/Components/Shared/Console.razor | 107 +++++++++++++++++----- TextLore/Models/Command.cs | 11 +++ TextLore/Models/ConsoleMessageType.cs | 9 ++ TextLore/Models/ConsoleOutput.cs | 46 ++++++++++ TextLore/Models/ConsoleOutputEventArgs.cs | 8 ++ TextLore/wwwroot/console.css | 4 +- 10 files changed, 184 insertions(+), 32 deletions(-) create mode 100644 TextLore/Commands/TestCommand.cs create mode 100644 TextLore/Models/Command.cs create mode 100644 TextLore/Models/ConsoleMessageType.cs create mode 100644 TextLore/Models/ConsoleOutput.cs create mode 100644 TextLore/Models/ConsoleOutputEventArgs.cs diff --git a/TextLore/Commands/TestCommand.cs b/TextLore/Commands/TestCommand.cs new file mode 100644 index 0000000..8fe64c9 --- /dev/null +++ b/TextLore/Commands/TestCommand.cs @@ -0,0 +1,15 @@ +using TextLore.Models; + +namespace TextLore.Commands; + +public class TestCommand : Command +{ + public override string Name => "test"; + public override string Description => "A test command."; + + public override Task Execute(ConsoleOutput consoleOutput, string[] args) + { + consoleOutput.WriteLine("This is a test command."); + return Task.FromResult(true); + } +} \ No newline at end of file diff --git a/TextLore/Components/App.razor b/TextLore/Components/App.razor index d5e37aa..ff496fa 100644 --- a/TextLore/Components/App.razor +++ b/TextLore/Components/App.razor @@ -9,12 +9,12 @@ - - + + - + diff --git a/TextLore/Components/Pages/Counter.razor b/TextLore/Components/Pages/Counter.razor index 1a4f8e7..ef23cb3 100644 --- a/TextLore/Components/Pages/Counter.razor +++ b/TextLore/Components/Pages/Counter.razor @@ -1,5 +1,4 @@ @page "/counter" -@rendermode InteractiveServer Counter diff --git a/TextLore/Components/Pages/Home.razor b/TextLore/Components/Pages/Home.razor index be145aa..8907ade 100644 --- a/TextLore/Components/Pages/Home.razor +++ b/TextLore/Components/Pages/Home.razor @@ -1,5 +1,12 @@ @page "/" +@using TextLore.Commands +@using TextLore.Models Home - \ No newline at end of file + + +@code +{ + Command[] commands = [new TestCommand()]; +} \ No newline at end of file diff --git a/TextLore/Components/Shared/Console.razor b/TextLore/Components/Shared/Console.razor index d671c30..e48f26b 100644 --- a/TextLore/Components/Shared/Console.razor +++ b/TextLore/Components/Shared/Console.razor @@ -1,8 +1,6 @@ @using Microsoft.AspNetCore.Components.Forms @using TextLore.Models -@rendermode InteractiveServer -
- +
Command >
- +
-        @((MarkupString)Running)
-        @((MarkupString)Output)
+        @((MarkupString)running)
+        @((MarkupString)output)
         
@code { - public string Output { get; set; } = ""; - public string Running { get; set; } = ""; - public string Name { get; set; } = "Main Menu"; - public string Description { get; set; } = "Select a game mode"; - public bool ShowDate { get; set; } = true; - public bool Disabled { get; set; } = false; - public string Placeholder { get; set; } = "Enter a command, type 'help' for avaliable commands."; - public ConsoleInput ConsoleInput { get; set; } = new ConsoleInput(); - public void Execute(EditContext context) + [Parameter, EditorRequired] + public string Name { get; set; } = "Console"; + + [Parameter] + public string? Description { get; set; } + + [Parameter] + public bool ShowDate { get; set; } = true; + + + public string Placeholder => $"Enter a command{(HelpCommand is null ? "." : ", type 'help' for avaliable commands.")}"; + + public ConsoleInput ConsoleInput { get; set; } = new(); + public ConsoleOutput ConsoleOutput { get; set; } = new(); + + [Parameter, EditorRequired] + public IEnumerable Commands { get; set; } = []; + + [Parameter] + public Command? HelpCommand { get; set; } + + private string output { get; set; } = ""; + private string running { get; set; } = ""; + private bool disabled { get; set; } = false; + private InputText? inputText; + + protected override async Task OnAfterRenderAsync(bool firstRender) { - Disabled = true; - Running = ConsoleInput.Text; - string newOutput = $"

"; - newOutput += $"{DateTime.UtcNow.ToString("HH:mm")} > {ConsoleInput.Text}{Environment.NewLine}"; - newOutput += $"1+1=3"; - newOutput += $"

"; - Output = newOutput + Output; - ConsoleInput.Text = ""; - Disabled = false; + if (inputText?.Element is not null) + { + await inputText.Element.Value.FocusAsync(); + } + } + + protected override void OnInitialized() + { + ConsoleOutput.OnOutput += WriteOutput; + } + + public async Task Execute(EditContext context) + { + Command? command = Commands.FirstOrDefault(c=>c.Name.Equals(ConsoleInput.Text)); + command ??= HelpCommand; + + if(command is null) + { + running = $"[Failed] Command '{ConsoleInput.Text}' not found!"; + ConsoleInput.Text = string.Empty; + return; + } + + disabled = true; + + running = $"

"; + running += $"{ConsoleInput.Time.ToString("HH:mm")} > {ConsoleInput.Text}{Environment.NewLine}"; + + bool success = await command.Execute(ConsoleOutput, []); + + running += "

"; + + output = running + output; + + running = success ? $"[Success]" : $"[Failed]"; + + ConsoleInput.Text = string.Empty; + disabled = false; + StateHasChanged(); + } + + public void WriteOutput(object? sender, ConsoleOutputEventArgs e) + { + string newOutput = e.Message; + output = newOutput + output; + StateHasChanged(); } } \ No newline at end of file diff --git a/TextLore/Models/Command.cs b/TextLore/Models/Command.cs new file mode 100644 index 0000000..60adc92 --- /dev/null +++ b/TextLore/Models/Command.cs @@ -0,0 +1,11 @@ +namespace TextLore.Models; + +public abstract class Command +{ + public abstract string Name { get; } + public abstract string Description { get; } + public virtual string Usage { get; } = string.Empty; + public virtual string[] Aliases { get; } = []; + + public abstract Task Execute(ConsoleOutput consoleOutput, string[] args); +} \ No newline at end of file diff --git a/TextLore/Models/ConsoleMessageType.cs b/TextLore/Models/ConsoleMessageType.cs new file mode 100644 index 0000000..dda32f0 --- /dev/null +++ b/TextLore/Models/ConsoleMessageType.cs @@ -0,0 +1,9 @@ +namespace TextLore.Models; + +public enum ConsoleMessageType +{ + Default, + Info, + Warning, + Error +} \ No newline at end of file diff --git a/TextLore/Models/ConsoleOutput.cs b/TextLore/Models/ConsoleOutput.cs new file mode 100644 index 0000000..4b74ae8 --- /dev/null +++ b/TextLore/Models/ConsoleOutput.cs @@ -0,0 +1,46 @@ +namespace TextLore.Models; + +public class ConsoleOutput +{ + public event EventHandler? OnOutput; + + public void Write(string message, ConsoleMessageType consoleMessageType = ConsoleMessageType.Default) + { + OnOutput?.Invoke(this, new(message, consoleMessageType)); + } + + public void WriteLine(string message) + { + Write(message + Environment.NewLine); + } + + public void WriteError(string message) + { + Write($"[ERROR] {message}", ConsoleMessageType.Error); + } + + public void WriteErrorLine(string message) + { + WriteError(message + Environment.NewLine); + } + + public void WriteWarning(string message) + { + Write($"[WARNING] {message}", ConsoleMessageType.Warning); + } + + public void WriteWarningLine(string message) + { + WriteWarning(message + Environment.NewLine); + } + + public void WriteInfo(string message) + { + Write($"[INFO] {message}", ConsoleMessageType.Info); + } + + public void WriteInfoLine(string message) + { + WriteInfo(message + Environment.NewLine); + } +} \ No newline at end of file diff --git a/TextLore/Models/ConsoleOutputEventArgs.cs b/TextLore/Models/ConsoleOutputEventArgs.cs new file mode 100644 index 0000000..5d682b4 --- /dev/null +++ b/TextLore/Models/ConsoleOutputEventArgs.cs @@ -0,0 +1,8 @@ +namespace TextLore.Models; + +public class ConsoleOutputEventArgs(string message, ConsoleMessageType messageType) : EventArgs +{ + public string Message { get; } = message; + + public ConsoleMessageType MessageType { get; } = messageType; +} \ No newline at end of file diff --git a/TextLore/wwwroot/console.css b/TextLore/wwwroot/console.css index 92c60c2..2ab1609 100644 --- a/TextLore/wwwroot/console.css +++ b/TextLore/wwwroot/console.css @@ -2,7 +2,7 @@ width: 100%; height: 100%; margin: 0; - font-size: 11pt; + font-size: 13pt; font-family: monospace; color: white; background-color: black; @@ -36,7 +36,7 @@ #console #container p { margin: 0px; - margin-bottom: 10px; + margin-top: 10px; } #console #container .prgs {