using LANCommander.UI.Extensions; using LANCommander.UI.Providers; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using XtermBlazor; using LogLevel = Microsoft.Extensions.Logging.LogLevel; namespace LANCommander.UI.Components; public partial class Terminal : Xterm { [Inject] ScriptProvider ScriptProvider { get; set; } IJSObjectReference? _interop; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { _interop ??= await ScriptProvider.ImportModuleAsync(); if (Addons == null) Addons = new HashSet(); Addons.Add("readline"); Addons.Add("addon-fit"); } await base.OnAfterRenderAsync(firstRender); if (firstRender) await FitAsync(); } /// /// Write a line to the terminal with a specified log level /// /// Message to send /// LogLevel to write public async Task WriteLine(string message, LogLevel level = LogLevel.Information) { var code = level switch { LogLevel.Error => 31, LogLevel.Warning => 33, LogLevel.Debug => 37, LogLevel.Trace => 36, LogLevel.Information => 37, _ => 37, }; await base.WriteLine($"\x1b[0;{code}m{message}"); } /// /// Rerenders the terminal and changes the size to fit within the element's rect. Utilizes the `addon-fit` addon. /// public async Task FitAsync() => await Addon("addon-fit").InvokeVoidAsync("fit"); /// /// Wait for user input in the terminal /// /// public async Task ReadLineAsync(string prefix = "> ") => await Addon("readline").InvokeAsync("read", prefix); }