Set-Clipboard -AsOSC52 (#18222)

* Set remote clipboard using OSC52

* Fix codefactor comment

* Rename parameter from Using to As

* Rename alias
This commit is contained in:
Dominik Kaszewski
2023-05-01 14:53:31 -07:00
committed by GitHub
parent 698742ea9f
commit ea68edc686
@@ -44,6 +44,13 @@ namespace Microsoft.PowerShell.Commands
[Parameter]
public SwitchParameter PassThru { get; set; }
/// <summary>
/// Gets or sets whether to use OSC52 escape sequence to set the clipboard of host instead of target.
/// </summary>
[Parameter]
[Alias("ToLocalhost")]
public SwitchParameter AsOSC52 { get; set; }
/// <summary>
/// This method implements the BeginProcessing method for Set-Clipboard command.
/// </summary>
@@ -129,8 +136,28 @@ namespace Microsoft.PowerShell.Commands
if (ShouldProcess(setClipboardShouldProcessTarget, "Set-Clipboard"))
{
Clipboard.SetText(content.ToString());
SetClipboardContent(content.ToString());
}
}
/// <summary>
/// Set the clipboard content.
/// </summary>
/// <param name="content">The content to store into the clipboard.</param>
private void SetClipboardContent(string content)
{
if (!AsOSC52)
{
Clipboard.SetText(content);
return;
}
var bytes = System.Text.Encoding.UTF8.GetBytes(content);
var encoded = System.Convert.ToBase64String(bytes);
var osc = $"\u001B]52;;{encoded}\u0007";
var message = new HostInformationMessage { Message = osc, NoNewLine = true };
WriteInformation(message, new string[] { "PSHOST" });
}
}
}