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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ internal static class Clipboard
private static string StartProcess(
string tool,
string args,
string stdin = "")
string stdin = "",
bool readStdout = true)
{
ProcessStartInfo startInfo = new();
startInfo.UseShellExecute = false;
Expand All @@ -28,7 +29,7 @@ private static string StartProcess(
startInfo.RedirectStandardError = true;
startInfo.FileName = tool;
startInfo.Arguments = args;
string stdout;
string stdout = string.Empty;

using (Process process = new())
{
Expand All @@ -43,15 +44,15 @@ private static string StartProcess(
return string.Empty;
}

if (!string.IsNullOrEmpty(stdin))
process.StandardInput.Write(stdin);
process.StandardInput.Close();

if (readStdout)
{
process.StandardInput.Write(stdin);
process.StandardInput.Close();
stdout = process.StandardOutput.ReadToEnd();
}

stdout = process.StandardOutput.ReadToEnd();
process.WaitForExit(250);

_clipboardSupported = process.ExitCode == 0;
}

Expand Down Expand Up @@ -93,11 +94,6 @@ public static string GetText()

public static void SetText(string text)
{
if (string.IsNullOrEmpty(text))
{
return;
}

if (_clipboardSupported == false)
{
_internalClipboard = text;
Expand All @@ -114,7 +110,14 @@ public static void SetText(string text)
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
tool = "xclip";
args = "-selection clipboard -in";
if (string.IsNullOrEmpty(text))
{
args = "-selection clipboard /dev/null";
}
else
{
args = "-selection clipboard -in";
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Expand All @@ -126,7 +129,7 @@ public static void SetText(string text)
return;
}

StartProcess(tool, args, text);
StartProcess(tool, args, text, readStdout: false);
if (_clipboardSupported == false)
{
_internalClipboard = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ Describe 'Clipboard cmdlet tests' -Tag CI {
'world' | Set-Clipboard -Append
Get-Clipboard -Raw | Should -BeExactly "hello$([Environment]::NewLine)world"
}

It 'Set-Clipboard accepts <value> string' -TestCases @(
@{ value = 'empty'; text = "" }
@{ value = 'null' ; text = $null }
){
param ($text)

$text | Set-Clipboard
Get-Clipboard -Raw | Should -BeNullOrEmpty
}
}
}