@using Microsoft.Extensions.Logging @using XtermBlazor @inject IJSRuntime JsRuntime @inject ILogger Logger
@if (IsInitialized) { }
@code { [Parameter] public Func? OnAfterInitialized { get; set; } [Parameter] public Func? OnFirstRender { get; set; } private Xterm Terminal; private bool IsInitialized = false; private bool HadFirstRender = false; private readonly Queue MessageCache = new(); private readonly HashSet Addons = ["addon-fit"]; private readonly TerminalOptions Options = new() { CursorBlink = false, CursorStyle = CursorStyle.Bar, CursorWidth = 1, FontFamily = "Space Mono, monospace", DisableStdin = true, CursorInactiveStyle = CursorInactiveStyle.None, Theme = { Background = "#000000" } }; protected override async Task OnAfterRenderAsync(bool firstRender) { if(!firstRender) return; // Initialize addons try { await JsRuntime.InvokeVoidAsync("moonlightServers.loadAddons"); } catch (Exception e) { Logger.LogError("An error occured while initializing addons: {e}", e); } IsInitialized = true; await InvokeAsync(StateHasChanged); if(OnAfterInitialized != null) await OnAfterInitialized.Invoke(); } private async Task HandleFirstRender() { try { await Terminal.Addon("addon-fit").InvokeVoidAsync("fit"); } catch (Exception e) { Logger.LogError("An error occured while calling addons: {e}", e); } HadFirstRender = true; // Write out cache while (MessageCache.Count > 0) { await Terminal.Write(MessageCache.Dequeue()); } if (OnFirstRender != null) await OnFirstRender.Invoke(); } public async Task Write(string content) { // We cache messages here as there is the chance that the console isn't ready for input while receiving write tasks if (HadFirstRender) await Terminal.Write(content); else MessageCache.Enqueue(content); } }