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

91 lines
2.3 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 {
2026-03-15 17:06:37 -05:00
[Parameter] public bool Visible { get; set; }
[Parameter] public EventCallback<bool> VisibleChanged { 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 _ =>
{
2026-03-15 17:06:37 -05:00
await InvokeAsync(async () =>
{
if (_terminal is null)
return;
2026-03-15 17:06:37 -05:00
await _terminal.Clear();
Visible = true;
if (VisibleChanged.HasDelegate)
await VisibleChanged.InvokeAsync(Visible);
StateHasChanged();
await _terminal.FitAsync();
});
};
Debugger.OnOutput = async (level, message) =>
2024-08-14 13:24:50 -05:00
{
2026-03-15 17:06:37 -05:00
await InvokeAsync(async () =>
{
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
{
2026-03-15 17:06:37 -05:00
await InvokeAsync(async () =>
2024-08-14 13:24:50 -05:00
{
2026-03-15 17:06:37 -05:00
if (_terminal is null)
return;
while (true)
{
var input = await _terminal.ReadLineAsync();
if (input.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
2024-08-14 13:24:50 -05:00
2026-03-15 17:06:37 -05:00
if (input.StartsWith('$'))
input = "Write-Host " + input;
2024-08-14 13:24:50 -05:00
2026-03-15 17:06:37 -05:00
await context.ExecuteAsync(input);
}
2026-03-15 17:06:37 -05:00
Visible = false;
2026-03-15 17:06:37 -05:00
if (VisibleChanged.HasDelegate)
await VisibleChanged.InvokeAsync(Visible);
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();
2026-03-15 17:06:37 -05:00
Debugger.SignalReady();
2024-08-14 13:24:50 -05:00
}
}