Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better linux clipboard error message #264

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Prompt(
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);
}
}

}