-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from waf/add-better-linux-clipboard-error-mes…
…sage Add better linux clipboard error message
- Loading branch information
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using TextCopy; | ||
|
||
namespace PrettyPrompt.TextSelection; | ||
internal class WrappedClipboard : IClipboard | ||
{ | ||
private const string MissingExecutableError = "Could not execute process"; | ||
private const string HelpfulErrorMessage = "Could not access clipboard. Check that xsel (Linux) or clip.exe (WSL) is installed."; | ||
private readonly Clipboard clipboard; | ||
|
||
public WrappedClipboard() | ||
{ | ||
this.clipboard = new Clipboard(); | ||
} | ||
|
||
public string? GetText() | ||
{ | ||
try | ||
{ | ||
return clipboard.GetText(); | ||
} | ||
catch (Exception ex) when (ex.Message.Contains(MissingExecutableError)) | ||
{ | ||
throw new Exception(HelpfulErrorMessage, ex); | ||
} | ||
} | ||
|
||
public async Task<string?> GetTextAsync(CancellationToken cancellation = default) | ||
{ | ||
try | ||
{ | ||
return await clipboard.GetTextAsync(cancellation).ConfigureAwait(false); | ||
} | ||
catch (Exception ex) when (ex.Message.Contains(MissingExecutableError)) | ||
{ | ||
throw new Exception(HelpfulErrorMessage, ex); | ||
} | ||
} | ||
|
||
public void SetText(string text) | ||
{ | ||
try | ||
{ | ||
clipboard.SetText(text); | ||
} | ||
catch (Exception ex) when (ex.Message.Contains(MissingExecutableError)) | ||
{ | ||
throw new Exception(HelpfulErrorMessage, ex); | ||
} | ||
} | ||
|
||
public async Task SetTextAsync(string text, CancellationToken cancellation = default) | ||
{ | ||
try | ||
{ | ||
await clipboard.SetTextAsync(text, cancellation).ConfigureAwait(false); | ||
} | ||
catch (Exception ex) when (ex.Message.Contains(MissingExecutableError)) | ||
{ | ||
throw new Exception(HelpfulErrorMessage, ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using PrettyPrompt.TextSelection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace PrettyPrompt.Tests; | ||
public class ClipboardTests | ||
{ | ||
private readonly WrappedClipboard clipboard; | ||
|
||
public ClipboardTests() | ||
{ | ||
this.clipboard = new WrappedClipboard(); | ||
} | ||
|
||
[Fact] | ||
public async Task Clipboard_WrappedCopyPasting() | ||
{ | ||
var console = ConsoleStub.NewConsole(); | ||
using (console.ProtectClipboard()) | ||
{ | ||
clipboard.SetText("hello"); | ||
await Task.Delay(100); | ||
var pasted = clipboard.GetText(); | ||
Assert.Equal("hello", pasted); | ||
|
||
await clipboard.SetTextAsync("world"); | ||
await Task.Delay(100); | ||
pasted = await clipboard.GetTextAsync(); | ||
Assert.Equal("world", pasted); | ||
} | ||
} | ||
|
||
} |