LANCommander/LANCommander.Launcher/UI/Components/PowerShellConsole.razor

75 lines
No EOL
1.9 KiB
Text

@using LANCommander.Launcher.Services.PowerShell
@using XtermBlazor
@using LogLevel = Microsoft.Extensions.Logging.LogLevel
@namespace LANCommander.Launcher.UI
@inject ScriptDebugger Debugger
<div class="terminal @(Visible ? "" : "hidden")">
<Terminal @ref="_terminal" Options="_options" OnFirstRender="@OnFirstRender" Addons="@_addons" />
</div>
@code {
private bool Visible { get; set; }
private Terminal? _terminal;
private TerminalOptions _options = new()
{
CursorBlink = true,
CursorStyle = CursorStyle.Bar,
};
private HashSet<string> _addons = new()
{
"readline",
"addon-fit"
};
protected override void OnInitialized()
{
Debugger.OnDebugStart = async _ =>
{
if (_terminal is null)
return;
await _terminal.Clear();
Visible = true;
await InvokeAsync(StateHasChanged);
await _terminal.FitAsync();
};
Debugger.OnOutput = async (level, message) =>
{
if (_terminal is not null)
await _terminal.WriteLine(message, level);
};
Debugger.OnDebugBreak = async context =>
{
if (_terminal is null)
return;
while (true)
{
var input = await _terminal.ReadLineAsync();
if (input.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
if (input.StartsWith('$'))
input = "Write-Host " + input;
await context.ExecuteAsync(input);
}
Visible = false;
await InvokeAsync(StateHasChanged);
};
}
private async Task OnFirstRender()
{
if (_terminal is not null)
await _terminal.FitAsync();
}
}