Skip to content
Merged
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
37 changes: 19 additions & 18 deletions PSReadLine/Clipboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ static class Clipboard
// This is useful for testing in CI as well.
private static string _internalClipboard;

private static string StartProcess(
string tool,
string args,
string stdin = ""
)
private static string StartProcess(bool collectOutput, string tool, string args, string stdin = null)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = tool;
startInfo.Arguments = args;
string stdout;
bool redirectInput = !string.IsNullOrEmpty(stdin);

using (Process process = new Process())
ProcessStartInfo startInfo = new()
{
UseShellExecute = false,
RedirectStandardInput = redirectInput,
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = tool,
Arguments = args
};

using (Process process = new())
{
process.StartInfo = startInfo;
try
Expand All @@ -42,15 +42,16 @@ private static string StartProcess(
{
_clipboardSupported = false;
PSConsoleReadLine.Ding();
return "";
return string.Empty;
}

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

stdout = collectOutput ? process.StandardOutput.ReadToEnd() : string.Empty;
process.WaitForExit(250);

_clipboardSupported = process.ExitCode == 0;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static string GetText()
return "";
}

return StartProcess(tool, args);
return StartProcess(collectOutput: true, tool, args);
}

public static void SetText(string text)
Expand Down Expand Up @@ -128,7 +129,7 @@ public static void SetText(string text)
return;
}

StartProcess(tool, args, text);
StartProcess(collectOutput: false, tool, args, text);
if (_clipboardSupported == false)
{
_internalClipboard = text;
Expand Down