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

69 lines
1.7 KiB
Text
Raw Normal View History

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