Files
TextLore/TextLore/Components/Shared/Console.razor
T

145 lines
4.5 KiB
Plaintext

@using Microsoft.AspNetCore.Components.Forms
@using TextLore.Models
<div id="console">
<div id="header">
<h4>@Name</h4>
@if (ShowDate)
{
<h5> @DateTime.UtcNow.ToLongDateString()</h5>
}
@if (!string.IsNullOrWhiteSpace(Description))
{
<h6>> @Description</h6>
}
</div>
<div id="container">
<EditForm OnSubmit="Execute" autocomplete="off" Model="ConsoleInput" novalidate>
<div id="input-line" class="input-line">
<div class="prompt">
Command >
</div>
<div>
<InputText @ref="inputText" id="commandline" autocomplete="off" class="cmdline" disabled="@disabled" placeholder="@Placeholder" @bind-Value="@ConsoleInput.Text" />
</div>
</div>
</EditForm>
<pre>
<code>@currentOutput?.Text</code>
<code>
@foreach (ConsoleOutput output in consoleOutputs.Reverse<ConsoleOutput>())
{
<p>
<span class='header'>@output.Time.ToString("HH:mm") > </span><span class='command'>@output.Command</span>
@output.Text
</p>
}
</code>
</pre>
</div>
</div>
@code {
[Parameter, EditorRequired]
public string Name { get; set; } = "Console";
[Parameter]
public string? Description { get; set; }
[Parameter]
public bool ShowDate { get; set; } = true;
[Parameter, EditorRequired]
public IEnumerable<Command> Commands { get; set; } = [];
[Parameter]
public Command? HelpCommand { get; set; }
private string Placeholder => $"Enter a command{(HelpCommand is null ? "." : ", type 'help' for avaliable commands.")}";
private ConsoleInput ConsoleInput { get; set; } = new();
private List<ConsoleOutput> consoleOutputs = new();
private ConsoleOutput? currentOutput;
private bool disabled { get; set; } = false;
private InputText? inputText;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (inputText?.Element is not null)
{
await inputText.Element.Value.FocusAsync();
}
}
public async Task Execute(EditContext context)
{
string commandName = ConsoleInput.Text.Split(' ')[0];
string commandArgs = new string(ConsoleInput.Text.Skip(commandName.Length).ToArray()).TrimStart(' ');
Command? command = Commands.FirstOrDefault(c => c.Name.Equals(commandName) || c.Aliases.Contains(commandName));
command ??= HelpCommand?.Name.Equals(commandName) == true || HelpCommand?.Aliases.Contains(commandName) == true ? HelpCommand : null;
if (command is null)
{
await CommandNotFound(commandName);
return;
}
disabled = true;
ConsoleWriter consoleWriter = new ConsoleWriter();
ConsoleOutput consoleOutput = new ConsoleOutput();
consoleWriter.OnOutput += (_, e) => WriteOutput(consoleOutput, e);
consoleWriter.OnClear += (_, _) => consoleOutputs.Clear();
consoleOutput.Command = ConsoleInput.Text;
currentOutput = consoleOutput;
CommandResult commandResult = await command.Execute(consoleWriter, commandArgs);
currentOutput = new()
{
Text = $"{(commandResult.IsSuccess ? "[Success]" : "[Failed]")} {commandResult.Message}"
};
if (!command.NoHistoryIfNoOutput || !string.IsNullOrWhiteSpace(consoleOutput.Text))
{
consoleOutputs.Add(consoleOutput);
}
ConsoleInput.Text = string.Empty;
disabled = false;
StateHasChanged();
}
public void WriteOutput(ConsoleOutput consoleOutput, ConsoleOutputEventArgs e)
{
consoleOutput.Text += e.Message;
StateHasChanged();
}
private async Task CommandNotFound(string commandName)
{
currentOutput = new()
{
Text = $"[Failed] Command '{commandName}' not found!"
};
if(HelpCommand is null)
{
return;
}
ConsoleWriter consoleWriter = new ConsoleWriter();
ConsoleOutput consoleOutput = new ConsoleOutput();
consoleWriter.OnOutput += (_, e) => WriteOutput(consoleOutput, e);
consoleOutput.Command = ConsoleInput.Text;
consoleOutputs.Add(consoleOutput);
CommandResult commandResult = await HelpCommand.Execute(consoleWriter, string.Empty);
ConsoleInput.Text = string.Empty;
}
}