Skip to content

Commit

Permalink
add wsl support (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 1, 2021
1 parent 830fdad commit 43b11da
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/TestConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using TextCopy;

class Program
Expand All @@ -8,6 +9,7 @@ static async Task<int> Main()
var text = "Hello World!";
await ClipboardService.SetTextAsync(text);
var result = await ClipboardService.GetTextAsync();
Console.WriteLine(result);
if (result == text)
{
return 0;
Expand Down
3 changes: 0 additions & 3 deletions src/TestConsole/TestConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<TargetFramework>net5</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TextCopy\TextCopy.csproj" />
</ItemGroup>
Expand Down
34 changes: 27 additions & 7 deletions src/TextCopy/LinuxClipboard_2.0.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#if (NETSTANDARD2_0 || NETFRAMEWORK)
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

static class LinuxClipboard
{
static bool isWsl;

static LinuxClipboard()
{
isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null;
}

public static Task SetTextAsync(string text, CancellationToken cancellation)
{
SetText(text);
Expand All @@ -18,7 +26,14 @@ public static void SetText(string text)
File.WriteAllText(tempFileName, text);
try
{
BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard");
if (isWsl)
{
BashRunner.Run($"cat {tempFileName} | clip.exe ");
}
else
{
BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard ");
}
}
finally
{
Expand All @@ -28,18 +43,23 @@ public static void SetText(string text)

public static Task<string?> GetTextAsync(CancellationToken cancellation)
{
return Task.FromResult(GetText());
return Task.FromResult<string?>(GetText());
}

public static string? GetText()
public static string GetText()
{
var tempFileName = Path.GetTempFileName();
try
{
BashRunner.Run($"xsel -o --clipboard > {tempFileName}");
var readAllText = File.ReadAllText(tempFileName);
// ReSharper disable once RedundantTypeArgumentsOfMethod
return readAllText;
if (isWsl)
{
BashRunner.Run($"powershell.exe Get-Clipboard > {tempFileName}");
}
else
{
BashRunner.Run($"xsel -o --clipboard > {tempFileName}");
}
return File.ReadAllText(tempFileName);
}
finally
{
Expand Down
52 changes: 39 additions & 13 deletions src/TextCopy/LinuxClipboard_2.1.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
#if (NETSTANDARD2_1 || NET5_0)
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

static class LinuxClipboard
{
static bool isWsl;

static LinuxClipboard()
{
isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null;
}

public static async Task SetTextAsync(string text, CancellationToken cancellation)
{
var tempFileName = Path.GetTempFileName();
await File.WriteAllTextAsync(tempFileName, text, cancellation);
try
{
if (cancellation.IsCancellationRequested)
{
return;
}

BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard ");
}
finally
if (cancellation.IsCancellationRequested)
{
File.Delete(tempFileName);
return;
}

InnerSetText(tempFileName);
}

public static void SetText(string text)
{
var tempFileName = Path.GetTempFileName();
File.WriteAllText(tempFileName, text);
InnerSetText(tempFileName);
}

static void InnerSetText(string tempFileName)
{
try
{
BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard ");
if (isWsl)
{
BashRunner.Run($"cat {tempFileName} | clip.exe ");
}
else
{
BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard ");
}
}
finally
{
Expand All @@ -43,7 +57,7 @@ public static void SetText(string text)
var tempFileName = Path.GetTempFileName();
try
{
BashRunner.Run($"xsel -o --clipboard > {tempFileName}");
InnerGetText(tempFileName);
return File.ReadAllText(tempFileName);
}
finally
Expand All @@ -57,13 +71,25 @@ public static void SetText(string text)
var tempFileName = Path.GetTempFileName();
try
{
BashRunner.Run($"xsel -o --clipboard > {tempFileName}");
InnerGetText(tempFileName);
return await File.ReadAllTextAsync(tempFileName, cancellation);
}
finally
{
File.Delete(tempFileName);
}
}

static void InnerGetText(string tempFileName)
{
if (isWsl)
{
BashRunner.Run($"powershell.exe Get-Clipboard > {tempFileName}");
}
else
{
BashRunner.Run($"xsel -o --clipboard > {tempFileName}");
}
}
}
#endif

0 comments on commit 43b11da

Please sign in to comment.