mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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:
+28
-1
@@ -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" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user