Skip to content

Commit

Permalink
Merge pull request #264 from waf/add-better-linux-clipboard-error-mes…
Browse files Browse the repository at this point in the history
…sage

Add better linux clipboard error message
  • Loading branch information
waf committed Jul 16, 2023
2 parents fec0637 + 207607b commit 0580320
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/PrettyPrompt/Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using PrettyPrompt.History;
using PrettyPrompt.Panes;
using PrettyPrompt.Rendering;
using PrettyPrompt.TextSelection;
using TextCopy;

namespace PrettyPrompt;
Expand Down Expand Up @@ -50,7 +51,7 @@ public sealed class Prompt : IPrompt, IAsyncDisposable
this.configuration = configuration ?? new PromptConfiguration();
this.history = new HistoryLog(persistentHistoryFilepath, this.configuration.KeyBindings);
this.cancellationManager = new CancellationManager(this.console);
this.clipboard = (console is IConsoleWithClipboard consoleWithClipboard) ? consoleWithClipboard.Clipboard : new Clipboard();
this.clipboard = (console is IConsoleWithClipboard consoleWithClipboard) ? consoleWithClipboard.Clipboard : new WrappedClipboard();

promptCallbacks = callbacks ?? new PromptCallbacks();
this.highlighter = new SyntaxHighlighter(promptCallbacks, PromptConfiguration.HasUserOptedOutFromColor);
Expand Down
65 changes: 65 additions & 0 deletions src/PrettyPrompt/TextSelection/WrappedClipboard.cs
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);
}
}
}
37 changes: 37 additions & 0 deletions tests/PrettyPrompt.Tests/ClipboardTests.cs
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);
}
}

}

0 comments on commit 0580320

Please sign in to comment.