Skip to content

Commit

Permalink
xterm
Browse files Browse the repository at this point in the history
  • Loading branch information
weibaohui committed Nov 22, 2023
1 parent 9c83208 commit 78fec78
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
1 change: 1 addition & 0 deletions BlazorApp/Pages/Pod/PodAction.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<a>删除</a>
</Popconfirm>
<a onclick="@(() => OnPodLogClick(PodItem))">日志</a>
<a onclick="@(() => OnPodExecClick(PodItem))">Exec</a>

@if (Enable)
{
Expand Down
6 changes: 6 additions & 0 deletions BlazorApp/Pages/Pod/PodAction.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ private async Task OnPodLogClick(V1Pod pod)
await PageDrawerService.ShowDrawerAsync<PodLogsView, V1Pod, bool>(options, pod);
}

private async Task OnPodExecClick(V1Pod pod)
{
var options = PageDrawerService.DefaultOptions($"Logs:{pod.Name()}", width: 1000);
await PageDrawerService.ShowDrawerAsync<PodExecView, V1Pod, bool>(options, pod);
}

private async Task OnAnalyzeClick(V1Pod pod)
{
var options = PageDrawerService.DefaultOptions($"AI智能分析:{pod.Name()}", width: 1000);
Expand Down
54 changes: 33 additions & 21 deletions BlazorApp/Pages/Pod/PodExecView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using k8s.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using XtermBlazor;

namespace BlazorApp.Pages.Pod;
Expand All @@ -25,6 +26,9 @@ protected override void OnInitialized()
base.OnInitialized();
}

[Inject]
protected IJSRuntime JSRuntime { get; set; }

private Xterm _terminal, _terminalEvent;

private TerminalOptions _options = new TerminalOptions
Expand Down Expand Up @@ -54,35 +58,43 @@ protected override void OnInitialized()
private int _eventId = 0, _columns, _rows;
private string _searchInput = "", _input = "Hello World";


private async Task OnFirstRender()
{
var logs = await PodService.Logs(PodItem, true);

await using (var xs = new XtermStream(_terminal))
{
await logs.CopyToAsync(xs);
}


await _terminalEvent.WriteLine($"({++_eventId}) OnFirstRender()");

bool isWebAssembly = JSRuntime is IJSInProcessRuntime;

// Blazor Server
await _terminal.AttachCustomKeyEventHandlerEvaluate("(event) => true");

_terminal.AttachCustomKeyEventHandler((args) =>
if (isWebAssembly)
{
if (showAttachCustomKeyEventHandlerLog)
// Blazor WebAssembly
_terminal.AttachCustomKeyEventHandler((args) =>
{
_terminalEvent.WriteLine(
$"({++_eventId}) AttachCustomKeyEventHandler(): Key: {args.Key}, Ctrl: {args.CtrlKey} {JsonSerializer.Serialize(args)}");
}

// The return value is ignored on Blazor Server, AttachCustomKeyEventHandlerEvaluate is used for handling that
return true;
});
if (showAttachCustomKeyEventHandlerLog)
{
_terminalEvent.WriteLine(
$"({++_eventId}) AttachCustomKeyEventHandler(): Key: {args.Key}, Ctrl: {args.CtrlKey} {JsonSerializer.Serialize(args)}");
}

return true;
});
}
else
{
// Blazor Server
await _terminal.AttachCustomKeyEventHandlerEvaluate("(event) => true");

_terminal.AttachCustomKeyEventHandler((args) =>
{
if (showAttachCustomKeyEventHandlerLog)
{
_terminalEvent.WriteLine(
$"({++_eventId}) AttachCustomKeyEventHandler(): Key: {args.Key}, Ctrl: {args.CtrlKey} {JsonSerializer.Serialize(args)}");
}

// The return value is ignored on Blazor Server, AttachCustomKeyEventHandlerEvaluate is used for handling that
return true;
});
}

await _terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
await _terminalEvent.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
Expand Down
1 change: 1 addition & 0 deletions BlazorApp/Pages/Pod/PodLogsView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<Xterm
@ref="_terminal"
Options="_options"
AddonIds="_addonIds"
OnFirstRender="@OnFirstRender"/>
5 changes: 3 additions & 2 deletions BlazorApp/Pages/Pod/PodLogsView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ protected override void OnInitialized()

private string[] _addonIds = new string[]
{
"xterm-addon-attach",
// "xterm-addon-attach",
"xterm-addon-fit"
};


Expand All @@ -60,7 +61,7 @@ private async Task OnFirstRender()

// Blazor Server
// await _terminal.InvokeAddonFunctionVoidAsync("xterm-addon-attach", "new AttachAddon()");
// await _terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");
await _terminal.InvokeAddonFunctionVoidAsync("xterm-addon-fit", "fit");

_columns = await _terminal.GetColumns();
_rows = await _terminal.GetRows();
Expand Down
19 changes: 14 additions & 5 deletions BlazorApp/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="_content/XtermBlazor/XtermBlazor.min.js"></script>
@* <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-attach.min.js"></script> *@
@* <script> *@
@* window.XtermBlazor.registerAddons({"xterm-addon-attach": window.AttachAddon.AttachAddon}); *@
@* </script> *@

<script src="_framework/blazor.server.js"></script>
<script src="_content/XtermBlazor/XtermBlazor.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-fit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-search.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-web-links.min.js"></script>

<script>
XtermBlazor.registerAddons({
"xterm-addon-fit": new FitAddon.FitAddon(),
"xterm-addon-search": new SearchAddon.SearchAddon(),
"xterm-addon-web-links": new WebLinksAddon.WebLinksAddon(),
});
</script>
</body>
</html>

0 comments on commit 78fec78

Please sign in to comment.