Skip to content

Commit

Permalink
Fixes gui-cs#1445. Fixing more the Curses and WSL clipboard. (gui-cs#…
Browse files Browse the repository at this point in the history
…1448)

* Fixes gui-cs#1445. Fixing more the Curses and WSL clipboard.

* Fixing unit tests.

* Changing namespace.

* Fixes WSL2 clipboard unit test.

* Upgrades devcontainer with the MainLoop fix.

* Fixes pasting with no selection and with lines break.

* Prevents the event button click being fired after a button pressed with mouse move.

* Fixes the char [ not being processed.
  • Loading branch information
BDisp committed Oct 2, 2021
1 parent e0aa4c5 commit a7e5912
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 62 deletions.
7 changes: 4 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Terminal.Gui Codespace",
"image": "mcr.microsoft.com/vscode/devcontainers/dotnet:0.201.7-5.0",
"image": "mcr.microsoft.com/vscode/devcontainers/dotnet:5.0",
"settings": {
"terminal.integrated.defaultProfile.linux": "pwsh"
},
Expand All @@ -16,9 +16,10 @@
"jmrog.vscode-nuget-package-manager",
"coenraads.bracket-pair-colorizer",
"vscode-icons-team.vscode-icons",
"editorconfig.editorconfig"
"editorconfig.editorconfig",
"formulahendry.dotnet-test-explorer"
],
"postCreateCommand": "dotnet restore && dotnet build --configuration Release --no-restore && dotnet test --configuration Debug --no-restore --verbosity normal --collect:'XPlat Code Coverage' --settings UnitTests/coverlet.runsettings",
"postCreateCommand": "dotnet restore && dotnet clean && dotnet build --configuration Release --no-restore && dotnet test --configuration Debug --no-restore --verbosity normal --collect:'XPlat Code Coverage' --settings UnitTests/coverlet.runsettings"
}

// Built with ❤ by [Pipeline Foundation](https://pipeline.foundation)
91 changes: 68 additions & 23 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ void UpdateOffScreen ()

public static bool Is_WSL_Platform ()
{
var result = BashRunner.Run ("uname -a");
var result = BashRunner.Run ("uname -a", runCurses: false);
if (result.Contains ("microsoft") && result.Contains ("WSL")) {
return true;
}
Expand Down Expand Up @@ -1229,13 +1229,18 @@ static public bool Suspend ()
}

class CursesClipboard : ClipboardBase {
public override bool IsSupported => CheckSupport ();
public CursesClipboard ()
{
IsSupported = CheckSupport ();
}

public override bool IsSupported { get; }

bool CheckSupport ()
{
try {
var result = BashRunner.Run ("which xclip");
return BashRunner.FileExists (result);
var result = BashRunner.Run ("which xclip", runCurses: false);
return result.FileExists ();
} catch (Exception) {
// Permissions issue.
return false;
Expand Down Expand Up @@ -1270,7 +1275,7 @@ protected override void SetClipboardDataImpl (string text)
}

static class BashRunner {
public static string Run (string commandLine, bool output = true, string inputText = "")
public static string Run (string commandLine, bool output = true, string inputText = "", bool runCurses = true)
{
var arguments = $"-c \"{commandLine}\"";

Expand Down Expand Up @@ -1300,6 +1305,10 @@ public static string Run (string commandLine, bool output = true, string inputTe
throw new Exception (timeoutError);
}
if (process.ExitCode == 0) {
if (runCurses && Application.Driver is CursesDriver) {
Curses.raw ();
Curses.noecho ();
}
return outputBuilder.ToString ();
}

Expand All @@ -1322,12 +1331,16 @@ public static string Run (string commandLine, bool output = true, string inputTe
process.StandardInput.Write (inputText);
process.StandardInput.Close ();
process.WaitForExit ();
if (runCurses && Application.Driver is CursesDriver) {
Curses.raw ();
Curses.noecho ();
}
return inputText;
}
}
}

static bool DoubleWaitForExit (this System.Diagnostics.Process process)
public static bool DoubleWaitForExit (this System.Diagnostics.Process process)
{
var result = process.WaitForExit (500);
if (result) {
Expand All @@ -1336,7 +1349,7 @@ static bool DoubleWaitForExit (this System.Diagnostics.Process process)
return result;
}

public static bool FileExists (string value)
public static bool FileExists (this string value)
{
return !string.IsNullOrEmpty (value) && !value.Contains ("not found");
}
Expand All @@ -1356,23 +1369,24 @@ class MacOSXClipboard : ClipboardBase {
IntPtr generalPasteboardRegister = sel_registerName ("generalPasteboard");
IntPtr clearContentsRegister = sel_registerName ("clearContents");

public override bool IsSupported => CheckSupport ();
public MacOSXClipboard ()
{
utfTextType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "public.utf8-plain-text");
nsStringPboardType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "NSStringPboardType");
generalPasteboard = objc_msgSend (nsPasteboard, generalPasteboardRegister);
IsSupported = CheckSupport ();
}

public override bool IsSupported { get; }

bool CheckSupport ()
{
var result = BashRunner.Run ("which pbcopy");
if (!BashRunner.FileExists (result)) {
if (!result.FileExists ()) {
return false;
}
result = BashRunner.Run ("which pbpaste");
return BashRunner.FileExists (result);
}

public MacOSXClipboard ()
{
utfTextType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "public.utf8-plain-text");
nsStringPboardType = objc_msgSend (objc_msgSend (nsString, allocRegister), initWithUtf8Register, "NSStringPboardType");
generalPasteboard = objc_msgSend (nsPasteboard, generalPasteboardRegister);
return result.FileExists ();
}

protected override string GetClipboardDataImpl ()
Expand Down Expand Up @@ -1416,19 +1430,28 @@ protected override void SetClipboardDataImpl (string text)
}

class WSLClipboard : ClipboardBase {
public override bool IsSupported => CheckSupport ();
public WSLClipboard ()
{
IsSupported = CheckSupport ();
}

public override bool IsSupported { get; }

bool CheckSupport ()
{
var result = BashRunner.Run ("which powershell.exe");
return BashRunner.FileExists (result);
try {
var result = BashRunner.Run ("which powershell.exe");
return result.FileExists ();
} catch (System.Exception) {
return false;
}

//var result = BashRunner.Run ("which powershell.exe");
//if (!BashRunner.FileExists (result)) {
//if (!result.FileExists ()) {
// return false;
//}
//result = BashRunner.Run ("which clip.exe");
//return BashRunner.FileExists (result);
//return result.FileExists ();
}

protected override string GetClipboardDataImpl ()
Expand All @@ -1437,13 +1460,25 @@ protected override string GetClipboardDataImpl ()
StartInfo = new System.Diagnostics.ProcessStartInfo {
RedirectStandardOutput = true,
FileName = "powershell.exe",
Arguments = "-noprofile -command \"Get-Clipboard\""
Arguments = "-noprofile -command \"Get-Clipboard\"",
UseShellExecute = Application.Driver is CursesDriver,
CreateNoWindow = true
}
}) {
powershell.Start ();
var result = powershell.StandardOutput.ReadToEnd ();
powershell.StandardOutput.Close ();
powershell.WaitForExit ();
if (!powershell.DoubleWaitForExit ()) {
var timeoutError = $@"Process timed out. Command line: bash {powershell.StartInfo.Arguments}.
Output: {powershell.StandardOutput.ReadToEnd ()}
Error: {powershell.StandardError.ReadToEnd ()}";
throw new Exception (timeoutError);
}
if (Application.Driver is CursesDriver) {
Curses.raw ();
Curses.noecho ();
}
return result.TrimEnd ();
}
}
Expand All @@ -1458,6 +1493,16 @@ protected override void SetClipboardDataImpl (string text)
}) {
powershell.Start ();
powershell.WaitForExit ();
if (!powershell.DoubleWaitForExit ()) {
var timeoutError = $@"Process timed out. Command line: bash {powershell.StartInfo.Arguments}.
Output: {powershell.StandardOutput.ReadToEnd ()}
Error: {powershell.StandardError.ReadToEnd ()}";
throw new Exception (timeoutError);
}
if (Application.Driver is CursesDriver) {
Curses.raw ();
Curses.noecho ();
}
}

//using (var clipExe = new System.Diagnostics.Process {
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void GetConsoleInputType (ConsoleKeyInfo consoleKeyInfo)
}
break;
case 27:
case 91:
//case 91:
ConsoleKeyInfo [] cki = new ConsoleKeyInfo [] { consoleKeyInfo };
ConsoleModifiers mod = consoleKeyInfo.Modifiers;
int delay = 0;
Expand Down
14 changes: 10 additions & 4 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,13 +864,14 @@ void ProcessInput (WindowsConsole.InputRecord inputEvent)
case WindowsConsole.EventType.Mouse:
var me = ToDriverMouse (inputEvent.MouseEvent);
mouseHandler (me);
if (isButtonReleased) {
if (processButtonClick) {
mouseHandler (
new MouseEvent () {
X = me.X,
Y = me.Y,
Flags = ProcessButtonClick (inputEvent.MouseEvent)
});
processButtonClick = false;
}
break;

Expand Down Expand Up @@ -900,7 +901,7 @@ void ProcessInput (WindowsConsole.InputRecord inputEvent)
Point point;
int buttonPressedCount;
bool isOneFingerDoubleClicked = false;
bool cancelButtonReleased;
bool processButtonClick;

MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
{
Expand Down Expand Up @@ -1011,7 +1012,7 @@ MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
mouseFlag |= MouseFlags.ReportMousePosition;
point = new Point ();
isButtonReleased = false;
cancelButtonReleased = true;
processButtonClick = false;
} else {
point = new Point () {
X = mouseEvent.MousePosition.X,
Expand Down Expand Up @@ -1865,7 +1866,12 @@ void IMainLoopDriver.MainIteration ()
}

class WindowsClipboard : ClipboardBase {
public override bool IsSupported => IsClipboardFormatAvailable (cfUnicodeText) ? true : false;
public WindowsClipboard ()
{
IsSupported = IsClipboardFormatAvailable (cfUnicodeText);
}

public override bool IsSupported { get; }

protected override string GetClipboardDataImpl ()
{
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Core/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void AddTimeout (TimeSpan time, Timeout timeout)
while (timeouts.ContainsKey (k)) {
k = (DateTime.UtcNow + time).Ticks;
}
timeouts.Add (k, timeout);
}
timeouts.Add (k, timeout);
}

/// <summary>
Expand Down
16 changes: 12 additions & 4 deletions Terminal.Gui/Views/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2738,15 +2738,23 @@ public void Paste ()
}

SetWrapModel ();
var contents = Clipboard.Contents;
if (copyWithoutSelection) {
var runeList = Clipboard.Contents == null ? new List<Rune> () : Clipboard.Contents.ToRuneList ();
model.AddLine (currentRow, runeList);
currentRow++;
var runeList = contents == null ? new List<Rune> () : contents.ToRuneList ();
if ((runeList?.Count > 0 && runeList [0] == '\n')
|| (runeList?.Count > 1 && runeList [0] == '\r'
&& runeList [1] == '\n')) {

InsertText (contents);
} else {
model.AddLine (currentRow, runeList);
currentRow++;
}
} else {
if (selecting) {
ClearRegion ();
}
InsertText (Clipboard.Contents);
InsertText (contents);
copyWithoutSelection = false;
}
UpdateWrapModel ();
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/AutocompleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Terminal.Gui;
using Xunit;

namespace UnitTests {
namespace Terminal.Gui.Core {
public class AutocompleteTests {

[Fact][AutoInitShutdown]
Expand Down
Loading

0 comments on commit a7e5912

Please sign in to comment.