Browse Source

Added -noprofile option to PowerShell and the clip.exe.

BDisp 4 years ago
parent
commit
ce04eeda86
2 changed files with 49 additions and 27 deletions
  1. 30 24
      Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
  2. 19 3
      UnitTests/ClipboardTests.cs

+ 30 - 24
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -1076,10 +1076,7 @@ namespace Terminal.Gui {
 		{
 			try {
 				var result = BashRunner.Run ("which xclip");
-				if (string.IsNullOrEmpty (result) || result.Contains ("not found")) {
-					return false;
-				}
-				return true;
+				return BashRunner.FileExists (result);
 			} catch (Exception) {
 				// Permissions issue.
 				return false;
@@ -1179,6 +1176,11 @@ namespace Terminal.Gui {
 			}
 			return result;
 		}
+
+		public static bool FileExists (string value)
+		{
+			return !string.IsNullOrEmpty (value) && !value.Contains ("not found");
+		}
 	}
 
 	class MacOSXClipboard : ClipboardBase {
@@ -1200,23 +1202,11 @@ namespace Terminal.Gui {
 		bool CheckSupport ()
 		{
 			var result = BashRunner.Run ("which pbcopy");
-			if (!FileExists (result)) {
+			if (!BashRunner.FileExists (result)) {
 				return false;
 			}
 			result = BashRunner.Run ("which pbpaste");
-			if (!FileExists (result)) {
-				return false;
-			}
-
-			bool FileExists (string value)
-			{
-				if (string.IsNullOrEmpty (value) || value.Contains ("not found")) {
-					return false;
-				}
-				return true;
-			}
-
-			return true;
+			return BashRunner.FileExists (result);
 		}
 
 		public MacOSXClipboard ()
@@ -1272,10 +1262,14 @@ namespace Terminal.Gui {
 		bool CheckSupport ()
 		{
 			var result = BashRunner.Run ("which powershell.exe");
-			if (string.IsNullOrEmpty (result) || result.Contains ("not found")) {
-				return false;
-			}
-			return true;
+			return BashRunner.FileExists (result);
+
+			//var result = BashRunner.Run ("which powershell.exe");
+			//if (!BashRunner.FileExists (result)) {
+			//	return false;
+			//}
+			//result = BashRunner.Run ("which clip.exe");
+			//return BashRunner.FileExists (result);
 		}
 
 		protected override string GetClipboardDataImpl ()
@@ -1284,7 +1278,7 @@ namespace Terminal.Gui {
 				StartInfo = new System.Diagnostics.ProcessStartInfo {
 					RedirectStandardOutput = true,
 					FileName = "powershell.exe",
-					Arguments = "-command \"Get-Clipboard\""
+					Arguments = "-noprofile -command \"Get-Clipboard\""
 				}
 			}) {
 				powershell.Start ();
@@ -1300,12 +1294,24 @@ namespace Terminal.Gui {
 			using (var powershell = new System.Diagnostics.Process {
 				StartInfo = new System.Diagnostics.ProcessStartInfo {
 					FileName = "powershell.exe",
-					Arguments = $"-command \"Set-Clipboard -Value \\\"{text}\\\"\""
+					Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{text}\\\"\""
 				}
 			}) {
 				powershell.Start ();
 				powershell.WaitForExit ();
 			}
+
+			//using (var clipExe = new System.Diagnostics.Process {
+			//	StartInfo = new System.Diagnostics.ProcessStartInfo {
+			//		FileName = "clip.exe",
+			//		RedirectStandardInput = true
+			//	}
+			//}) {
+			//	clipExe.Start ();
+			//	clipExe.StandardInput.Write (text);
+			//	clipExe.StandardInput.Close ();
+			//	clipExe.WaitForExit ();
+			//}
 		}
 	}
 }

+ 19 - 3
UnitTests/ClipboardTests.cs

@@ -135,12 +135,28 @@ namespace Terminal.Gui.Core {
 							using (Process bash = new Process {
 								StartInfo = new ProcessStartInfo {
 									FileName = "powershell.exe",
-									Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
+									Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\""
 								}
 							}) {
 								bash.Start ();
 								bash.WaitForExit ();
 							}
+
+							//using (Process clipExe = new Process {
+							//	StartInfo = new ProcessStartInfo {
+							//		RedirectStandardInput = true,
+							//		FileName = "clip.exe"
+							//	}
+							//}) {
+							//	clipExe.Start ();
+							//	clipExe.StandardInput.Write (clipText);
+							//	clipExe.StandardInput.Close ();
+							//	clipExe.WaitForExit ();
+							//	//var result = clipExe.WaitForExit (500);
+							//	//if (result) {
+							//	//	clipExe.WaitForExit ();
+							//	//}
+							//}
 						} catch {
 							exit = true;
 						}
@@ -196,7 +212,7 @@ namespace Terminal.Gui.Core {
 						StartInfo = new ProcessStartInfo {
 							RedirectStandardOutput = true,
 							FileName = "powershell.exe",
-							Arguments = "-command \"Get-Clipboard\""
+							Arguments = "-noprofile -command \"Get-Clipboard\""
 						}
 					}) {
 						pwsh.Start ();
@@ -223,7 +239,7 @@ namespace Terminal.Gui.Core {
 								StartInfo = new ProcessStartInfo {
 									RedirectStandardOutput = true,
 									FileName = "powershell.exe",
-									Arguments = "-command \"Get-Clipboard\""
+									Arguments = "-noprofile -command \"Get-Clipboard\""
 								}
 							}) {
 								bash.Start ();