Skip to content

Commit 826f73e

Browse files
authored
Fix copying text to system clipboard on Linux using xclip (#3937)
1 parent 5fa2a21 commit 826f73e

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

PSReadLine/Clipboard.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ static class Clipboard
1616
// This is useful for testing in CI as well.
1717
private static string _internalClipboard;
1818

19-
private static string StartProcess(
20-
string tool,
21-
string args,
22-
string stdin = ""
23-
)
19+
private static string StartProcess(bool collectOutput, string tool, string args, string stdin = null)
2420
{
25-
ProcessStartInfo startInfo = new ProcessStartInfo();
26-
startInfo.UseShellExecute = false;
27-
startInfo.RedirectStandardInput = true;
28-
startInfo.RedirectStandardOutput = true;
29-
startInfo.RedirectStandardError = true;
30-
startInfo.FileName = tool;
31-
startInfo.Arguments = args;
3221
string stdout;
22+
bool redirectInput = !string.IsNullOrEmpty(stdin);
3323

34-
using (Process process = new Process())
24+
ProcessStartInfo startInfo = new()
25+
{
26+
UseShellExecute = false,
27+
RedirectStandardInput = redirectInput,
28+
RedirectStandardOutput = true,
29+
RedirectStandardError = true,
30+
FileName = tool,
31+
Arguments = args
32+
};
33+
34+
using (Process process = new())
3535
{
3636
process.StartInfo = startInfo;
3737
try
@@ -42,15 +42,16 @@ private static string StartProcess(
4242
{
4343
_clipboardSupported = false;
4444
PSConsoleReadLine.Ding();
45-
return "";
45+
return string.Empty;
4646
}
4747

48-
if (stdin != "")
48+
if (redirectInput)
4949
{
5050
process.StandardInput.Write(stdin);
5151
process.StandardInput.Close();
5252
}
53-
stdout = process.StandardOutput.ReadToEnd();
53+
54+
stdout = collectOutput ? process.StandardOutput.ReadToEnd() : string.Empty;
5455
process.WaitForExit(250);
5556

5657
_clipboardSupported = process.ExitCode == 0;
@@ -91,7 +92,7 @@ public static string GetText()
9192
return "";
9293
}
9394

94-
return StartProcess(tool, args);
95+
return StartProcess(collectOutput: true, tool, args);
9596
}
9697

9798
public static void SetText(string text)
@@ -128,7 +129,7 @@ public static void SetText(string text)
128129
return;
129130
}
130131

131-
StartProcess(tool, args, text);
132+
StartProcess(collectOutput: false, tool, args, text);
132133
if (_clipboardSupported == false)
133134
{
134135
_internalClipboard = text;

0 commit comments

Comments
 (0)