Skip to content

Commit

Permalink
Set-Clipboard -AsOSC52 (#18222)
Browse files Browse the repository at this point in the history
* Set remote clipboard using OSC52

* Fix codefactor comment

* Rename parameter from Using to As

* Rename alias
  • Loading branch information
dkaszews committed May 1, 2023
1 parent 698742e commit ea68edc
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public class SetClipboardCommand : PSCmdlet
[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>
Expand Down Expand Up @@ -129,8 +136,28 @@ private void SetClipboardContent(List<string> contentList, bool append)

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" });
}
}
}

0 comments on commit ea68edc

Please sign in to comment.