-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTerm.razor.cs
More file actions
208 lines (183 loc) · 5.22 KB
/
Term.razor.cs
File metadata and controls
208 lines (183 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using Microsoft.AspNetCore.Components;
namespace BootstrapBlazor.Components;
/// <summary>
/// Term 终端组件
/// </summary>
[JSModuleAutoLoader("./_content/BootstrapBlazor.Term/Components/Term/Term.razor.js", JSObjectReference = true)]
public partial class Term
{
/// <summary>
/// 获得/设置 UI Element
/// </summary>
private ElementReference Element { get; set; }
/// <summary>
/// 获得/设置 Options
/// </summary>
[Parameter]
public TermOptions Options { get; set; } = new TermOptions();
/// <summary>
/// 获得/设置 收到数据回调
/// </summary>
[Parameter]
public Func<string, Task>? OnData { get; set; }
/// <summary>
/// 获得/设置 终端 Resize 回调
/// </summary>
[Parameter]
public Func<int, int, Task>? OnResize { get; set; }
/// <summary>
/// 获得/设置 高度 默认 300px
/// </summary>
[Parameter]
public string Height { get; set; } = "300px";
/// <summary>
/// GetClassString
/// </summary>
/// <returns></returns>
private string? GetClassString() => CssBuilder.Default("bb-term")
.AddClassFromAttributes(AdditionalAttributes)
.Build();
/// <summary>
/// GetStyleString
/// </summary>
/// <returns></returns>
private string? GetStyleString() => CssBuilder.Default()
.AddClass($"height: {Height};", !string.IsNullOrEmpty(Height))
.AddStyleFromAttributes(AdditionalAttributes)
.Build();
/// <summary>
/// OnInitialized
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
}
/// <summary>
/// InvokeInitAsync
/// </summary>
/// <returns></returns>
protected override async Task InvokeInitAsync()
{
await InvokeVoidAsync("init", Id, Interop, Options);
}
/// <summary>
/// 写入数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public async Task Write(string data)
{
await InvokeVoidAsync("write", Id, data);
}
/// <summary>
/// 写入一行数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public async Task WriteLine(string data)
{
await InvokeVoidAsync("writeln", Id, data);
}
/// <summary>
/// 清空终端
/// </summary>
/// <returns></returns>
public async Task Clear()
{
await InvokeVoidAsync("clear", Id);
}
/// <summary>
/// 连接流
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public async Task Open(Stream stream)
{
_stream = stream;
_cancellationTokenSource = new CancellationTokenSource();
_ = ReadStreamAsync();
await Task.CompletedTask;
}
private Stream? _stream;
private CancellationTokenSource? _cancellationTokenSource;
private async Task ReadStreamAsync()
{
if (_stream == null) return;
var buffer = new byte[1024];
try
{
while (!_cancellationTokenSource!.IsCancellationRequested)
{
var read = await _stream.ReadAsync(buffer, _cancellationTokenSource.Token);
if (read == 0) break;
var data = System.Text.Encoding.UTF8.GetString(buffer, 0, read);
await Write(data);
}
}
catch (TaskCanceledException)
{
// ignored
}
catch (Exception ex)
{
// Handle error
await WriteLine($"\r\nError: {ex.Message}");
}
}
/// <summary>
/// 收到数据 JSInvoke
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[JSInvokable]
public async Task OnDataAsync(string data)
{
if (_stream != null && _stream.CanWrite)
{
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
await _stream.WriteAsync(buffer);
await _stream.FlushAsync();
}
if (OnData != null)
{
await OnData(data);
}
}
/// <summary>
/// Dispose
/// </summary>
/// <param name="disposing"></param>
protected override async ValueTask DisposeAsync(bool disposing)
{
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
await base.DisposeAsync(disposing);
}
/// <summary>
/// 获得/设置 终端行数
/// </summary>
public int Rows { get; private set; }
/// <summary>
/// 获得/设置 终端列数
/// </summary>
public int Columns { get; private set; }
/// <summary>
/// Resize JSInvoke
/// </summary>
/// <param name="rows"></param>
/// <param name="cols"></param>
/// <returns></returns>
[JSInvokable]
public async Task OnResizeAsync(int rows, int cols)
{
Rows = rows;
Columns = cols;
if (OnResize != null)
{
await OnResize(rows, cols);
}
}
}