Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion plugins/VelaShell.Plugin.S3/S3Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public Task ActivateAsync(IPluginContext context, CancellationToken cancellation
_actions.Attach(_fileSystem);
_registration = context.Protocols.Register(BuildDescriptor(context), _fileSystem);
// 语言切换后重注册:描述里的字段标签是插件自己的文案,宿主不会替我们翻。
context.Events.LocaleChanged += _ => Reregister();
context.Events.LocaleChanged += OnLocaleChanged;
return Task.CompletedTask;
}

/// <inheritdoc />
public async Task DeactivateAsync(CancellationToken cancellationToken)
{
// 不退订的话宿主的事件表一直握着本插件实例,可收集 ALC 就卸不掉这份程序集。
if (_context is { } context)
{
context.Events.LocaleChanged -= OnLocaleChanged;
}
_registration?.Dispose();
_registration = null;
if (_fileSystem is { } fileSystem)
Expand All @@ -53,6 +58,9 @@ public async Task DeactivateAsync(CancellationToken cancellationToken)
_context = null;
}

/// <summary>语言切换:按新语言重注册。命名方法而不是 lambda —— 停用时要能退订。</summary>
private void OnLocaleChanged(string locale) => Reregister();

private void Reregister()
{
if (_context is not { } context || _fileSystem is not { } fileSystem)
Expand Down
160 changes: 160 additions & 0 deletions plugins/VelaShell.Plugin.Serial/LocalEchoRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
namespace VelaShell.Plugin.Serial;

/// <summary>
/// 本地回显:把用户敲的键翻成"屏幕上该出现什么",而不是把原始按键字节原样喂回终端引擎。
/// <para>
/// 原样喂回的三个症状,都是"能用但看着不对":
/// </para>
/// <list type="bullet">
/// <item>回车只是 <c>CR</c> —— 光标回到行首却不下移,对端的应答随即盖掉刚敲的那一行;</item>
/// <item>退格是 <c>DEL</c>(0x7F)—— VT 引擎不画它,屏幕上的字删不掉;</item>
/// <item>方向键是 <c>ESC [ A</c> 之类 —— 喂回去就是在本地真的移动光标。</item>
/// </list>
/// <para>
/// 这里做的是终端驱动在 ECHO + ICANON 下做的那部分显示:CR / LF / CR LF 一律显示成换行,
/// 退格擦掉本行上一个字符(擦不过行首,提示符不会被擦掉),控制序列与其余 C0 控制字符不显示。
/// 线上的字节不受影响 —— 这只是显示。
/// </para>
/// <para>
/// 宽字符按 UTF-8 首字节粗判(CJK、韩文、补充平面的 emoji 记两格):退格要擦几格只取决于它,
/// 判错的代价只是半个字残影,不值得为此带一张 Unicode 宽度表。
/// </para>
/// <para>状态跨调用保持(一个控制序列、一对 CR LF 都可能被切在两次写入之间),调用方须串行调用。</para>
/// </summary>
internal sealed class LocalEchoRenderer
{
private const byte Cr = 0x0D;
private const byte Lf = 0x0A;

/// <summary>本行最多记这么多个字符的宽度;更长的行只是退格擦不回最早那部分。</summary>
private const int MaxTrackedCells = 4096;

private enum State
{
Text,
Escape,
ControlSequence,
SingleShift
}

/// <summary>本行已回显字符的显示宽度(1 或 2),退格按它擦。换行即清空。</summary>
private readonly List<byte> _widths = [];

private State _state;
private bool _afterCr;

/// <summary>把一次写入翻成要喂回终端的显示字节。</summary>
/// <param name="input">用户这次写入的字节。</param>
/// <returns>要显示的字节;可能为空。</returns>
public byte[] Render(ReadOnlySpan<byte> input)
{
var output = new List<byte>(input.Length + 8);
foreach (byte value in input)
{
switch (_state)
{
case State.Escape:
if (value == (byte)'[')
{
_state = State.ControlSequence;
continue;
}
if (value == (byte)'O')
{
_state = State.SingleShift;
continue;
}
// 单独按下的 ESC(或 Alt+键):ESC 本身不显示,这个字节照常处理。
_state = State.Text;
break;
case State.ControlSequence:
// CSI 的参数与中间字节在 0x20–0x3F,终止字节在 0x40–0x7E。
if (value is >= 0x40 and <= 0x7E)
{
_state = State.Text;
}
continue;
case State.SingleShift:
_state = State.Text; // ESC O x(小键盘、部分终端的方向键):x 是终止字节。
continue;
}
RenderText(value, output);
}
return [.. output];
}

private void RenderText(byte value, List<byte> output)
{
bool afterCr = _afterCr;
_afterCr = false;
switch (value)
{
case 0x1B:
_state = State.Escape;
break;
case Cr:
NewLine(output);
_afterCr = true;
break;
case Lf:
if (!afterCr)
{
NewLine(output); // 裸 LF 也得回到行首,否则就是阶梯。
}
break;
case 0x08 or 0x7F:
Erase(output);
break;
case 0x09:
output.Add(value);
Track(1); // 制表位的真实宽度不可知;按一格记,退格时最多少擦几格。
break;
case < 0x20:
break; // Ctrl-C / Ctrl-D 之类没有字形。
case >= 0x80 and < 0xC0:
output.Add(value); // UTF-8 续字节:属于前一个字符,不另占格。
break;
default:
output.Add(value);
Track(value is >= 0xE3 and <= 0xED or >= 0xF0 ? (byte)2 : (byte)1);
break;
}
}

private void NewLine(List<byte> output)
{
output.Add(Cr);
output.Add(Lf);
_widths.Clear();
}

private void Erase(List<byte> output)
{
if (_widths.Count == 0)
{
return; // 已在行首:别把提示符也擦了。
}
int width = _widths[^1];
_widths.RemoveAt(_widths.Count - 1);
for (int i = 0; i < width; i++)
{
output.Add(0x08);
}
for (int i = 0; i < width; i++)
{
output.Add((byte)' ');
}
for (int i = 0; i < width; i++)
{
output.Add(0x08);
}
}

private void Track(byte width)
{
if (_widths.Count < MaxTrackedCells)
{
_widths.Add(width);
}
}
}
10 changes: 7 additions & 3 deletions plugins/VelaShell.Plugin.Serial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ Break 与 DTR/RTS)。
| 打开时置 DTR / RTS | 开 / 开 | 高级。RTS 在流控制取 RTS/CTS 时自动隐藏(那时归驱动) |
| 收到裸 CR 补 LF | 关 | 高级。**输出每行盖在上一行上**时打开它 |
| 收到裸 LF 补 CR | 关 | 高级。**输出呈阶梯状**时打开它 |
| 本地回显 | 关 | 高级。对着从不回显的设备才需要开 |
| 发送延时(字符 / 行) | 0 / 0 | 高级。给没有流控、粘贴配置会丢字符的老设备用 |
| 本地回显 | 关 | 高级。对着从不回显的设备才需要开。回显按屏幕该有的样子画:CR / LF 都显示成换行,退格擦字(不擦过行首),方向键等控制序列不显示 |
| 发送延时(字符 / 行) | 0 / 0 | 高级。给没有流控、粘贴配置会丢字符的老设备用。在专用线程上按毫秒精度等待(`Task.Delay` 在 Windows 上粒度约 15.6ms);CR LF 只算一行;关会话即停 |

用户名/口令两栏由 `ProtocolFeatures.NoCredentials` 收起(登录发生在带内),
端口那一栏由 `ProtocolFeatures.NoEndpoint` 收起(串口的目标不是 `host:port`)。
Expand Down Expand Up @@ -97,7 +97,11 @@ Unix 上响应 —— 一个平台间行为不一致的地基,换来的只是省
`/sys/class/tty/<名>/device` 是否存在。
- **macOS 绝不能列 `/dev/tty.*`**。那是 dial-in 设备,打开时会**阻塞等待 DCD 载波** ——
对着一个没接线的适配器打开它,进程永久挂起,而用户看到的是"点了连接没反应"。
`/dev/cu.*` 是 call-out,立即打开。
`/dev/cu.*` 是 call-out,立即打开。手输或旧配置里的 `/dev/tty.xxx`,打开时自动换成存在的
`/dev/cu.xxx`(同一个硬件)。

打开端口本身放在线程池上做,而且可取消:`SerialPort.Open` 是阻塞调用(设备不在场的蓝牙 COM 口
能卡十几秒),中途打断不了 —— 取消立即生效,那次迟到的打开返回后再由插件把端口关掉。

## 打包

Expand Down
49 changes: 19 additions & 30 deletions plugins/VelaShell.Plugin.Serial/SerialLineDiscipline.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace VelaShell.Plugin.Serial;

/// <summary>
/// 串口的"行规程":换行改写与本地回显。全部是纯字节变换,不碰硬件 ——
/// 串口的"行规程":收发两个方向的换行改写(本地回显怎么显示见 <see cref="LocalEchoRenderer" />)。
/// 全部是纯字节变换,不碰硬件 ——
/// 于是这一层可以脱离真实串口单测,而这恰恰是串口最难验证的部分
/// (没有 telnetd 那样的环回可用,调研文档 §五.4 记的就是这条)。
/// <para>
Expand All @@ -28,6 +29,9 @@ internal sealed class SerialLineDiscipline(SerialConfig config)
/// </summary>
private bool _swallowLeadingLf;

/// <summary>上一次**出方向**写入以裸 CR 收尾(已按 <see cref="SerialConfig.EnterMode" /> 发成换行)。</summary>
private bool _transmitEndedWithCr;

/// <summary>
/// 入方向:按 <see cref="SerialConfig.ImplicitLf" /> / <see cref="SerialConfig.ImplicitCr" />
/// 补齐换行。两项都关时**原样返回同一段内存**(零拷贝,这是默认路径)。
Expand Down Expand Up @@ -101,8 +105,8 @@ public ReadOnlyMemory<byte> Receive(ReadOnlyMemory<byte> data)
/// </para>
/// <para>
/// 粘贴的 CRLF 文本不会被打成 <c>CR LF LF</c>:CR 后面已经跟着 LF 时不再补。
/// 这个判断按**本次写入**的缓冲区做,不跨调用 —— 一次粘贴是一次写入,
/// 要跨调用切开 CRLF 得宿主正好在这两个字节之间断开,不会发生。
/// 宿主把一大段粘贴分块写入时,CRLF 也可能正好被切在两次写入之间 —— 所以"上一次以裸 CR 收尾"
/// 要跨调用记住,下一次若以 LF 开头就吞掉它。调用方须串行调用(会话在写锁里调)。
/// </para>
/// </summary>
/// <param name="data">用户输入的字节。</param>
Expand All @@ -115,7 +119,18 @@ public ReadOnlyMemory<byte> Transmit(ReadOnlyMemory<byte> data)
}
ReadOnlySpan<byte> input = data.Span;
var output = new List<byte>(input.Length + 8);
for (int i = 0; i < input.Length; i++)
int start = 0;
if (_transmitEndedWithCr)
{
_transmitEndedWithCr = false;
// 上一块末尾那个 CR 已经按换行发出去了:这个 LF 是同一个换行的后半截。
if (input.Length > 0 && input[0] == Lf)
{
start = 1;
}
}
_transmitEndedWithCr = input.Length > start && input[^1] == Cr;
for (int i = start; i < input.Length; i++)
{
byte b = input[i];
if (b != Cr)
Expand Down Expand Up @@ -145,30 +160,4 @@ public ReadOnlyMemory<byte> Transmit(ReadOnlyMemory<byte> data)
}
return output.ToArray();
}

/// <summary>
/// 本地回显要往屏幕上打的字节。
/// <para>
/// 取的是**改写之后**要上线的那一份,并且把裸 CR 展开成 CR LF ——
/// 屏幕上的"回车"必须同时回到行首并下移一行,只回不移的话用户看到的是
/// 自己敲的下一行盖在上一行上。这一步只影响显示,不影响线上字节。
/// </para>
/// </summary>
/// <param name="transmitted">已按 <see cref="Transmit" /> 改写过的字节。</param>
/// <returns>喂回终端的字节;无需回显时为空。</returns>
public static ReadOnlyMemory<byte> BuildEcho(ReadOnlyMemory<byte> transmitted)
{
ReadOnlySpan<byte> input = transmitted.Span;
var output = new List<byte>(input.Length + 4);
for (int i = 0; i < input.Length; i++)
{
byte b = input[i];
output.Add(b);
if (b == Cr && (i + 1 >= input.Length || input[i + 1] != Lf))
{
output.Add(Lf);
}
}
return output.ToArray();
}
}
10 changes: 9 additions & 1 deletion plugins/VelaShell.Plugin.Serial/SerialPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ public Task ActivateAsync(IPluginContext context, CancellationToken cancellation
_registration = context.Protocols.Register(BuildDescriptor(context), _terminal);
RegisterCommands(context);
// 语言切换后重注册:表单标签与命令标题都是插件自己的文案,宿主不会替我们翻。
context.Events.LocaleChanged += _ => Reregister();
context.Events.LocaleChanged += OnLocaleChanged;
return Task.CompletedTask;
}

/// <inheritdoc />
public Task DeactivateAsync(CancellationToken cancellationToken)
{
// 不退订的话宿主的事件表一直握着本插件实例,可收集 ALC 就卸不掉这份程序集。
if (_context is { } context)
{
context.Events.LocaleChanged -= OnLocaleChanged;
}
foreach (IDisposable command in _commands)
{
command.Dispose();
Expand All @@ -55,6 +60,9 @@ public Task DeactivateAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}

/// <summary>语言切换:按新语言重注册。命名方法而不是 lambda —— 停用时要能退订。</summary>
private void OnLocaleChanged(string locale) => Reregister();

private void Reregister()
{
if (_context is not { } context || _terminal is not { } terminal)
Expand Down
Loading
Loading