From 77c2b0d4beb3b27faeb3b6e5b984723094aac109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sun, 31 Dec 2023 06:32:23 +0100 Subject: [PATCH] Support OSC 133 escape sequences for shell integration. Closes #91. --- src/core/Text/Control/ControlBuilder.cs | 31 +++++++++++++++++++++++ src/core/Text/Control/ControlSequences.cs | 20 +++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/core/Text/Control/ControlBuilder.cs b/src/core/Text/Control/ControlBuilder.cs index d6bb80a..5bd1697 100644 --- a/src/core/Text/Control/ControlBuilder.cs +++ b/src/core/Text/Control/ControlBuilder.cs @@ -699,6 +699,37 @@ public ControlBuilder CloseHyperlink() return Print(OSC).Print("8;;").Print(ST); } + public ControlBuilder BeginShellPrompt() + { + return Print(OSC).Print("133;A").Print(ST); + } + + public ControlBuilder EndShellPrompt() + { + return Print(OSC).Print("133;B").Print(ST); + } + + public ControlBuilder BeginShellExecution() + { + return Print(OSC).Print("133;C").Print(ST); + } + + public ControlBuilder EndShellExecution(int? code = null) + { + _ = Print(OSC).Print("133;D"); + + if (code is { } c) + { + var codeSpan = (stackalloc char[StackBufferSize]); + + _ = c.TryFormat(codeSpan, out var codeLen, provider: _culture); + + _ = Print(";").Print(codeSpan[..codeLen]); + } + + return Print(ST); + } + public ControlBuilder SaveScreenshot(ScreenshotFormat format = ScreenshotFormat.Html) { Check.Enum(format); diff --git a/src/core/Text/Control/ControlSequences.cs b/src/core/Text/Control/ControlSequences.cs index a61f0d2..bf4015a 100644 --- a/src/core/Text/Control/ControlSequences.cs +++ b/src/core/Text/Control/ControlSequences.cs @@ -372,6 +372,26 @@ public static string CloseHyperlink() return Create(static cb => cb.CloseHyperlink()); } + public static string BeginShellPrompt() + { + return Create(static cb => cb.BeginShellPrompt()); + } + + public static string EndShellPrompt() + { + return Create(static cb => cb.EndShellPrompt()); + } + + public static string BeginShellExecution() + { + return Create(static cb => cb.BeginShellExecution()); + } + + public static string EndShellExecution(int? code = null) + { + return Create(static (cb, code) => cb.EndShellExecution(code), code); + } + public static string SaveScreenshot(ScreenshotFormat format = ScreenshotFormat.Html) { return Create(static (cb, format) => cb.SaveScreenshot(format), format);