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

feature: Copy snippet to clipboard in case of single result #184

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ jobs:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Install xvfb for headless unit tests
run: sudo apt install xvfb
- name: Build with Maven
run: mvn -B verify
run: xvfb-run mvn -B verify
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ This setup does not touch your computer - as soon as you close your browser tab,

## Ideas for future development
* [ ] Proper support for multiple classifiers at a particular coordinate.
* [ ] Immediately copy the pom.xml snippet to the clipboard.
* [x] Immediately copy the pom.xml snippet to the clipboard.
* [x] Show the coordinates in a different form (Ivy, Gradle, SBT).


Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.Clipboard;
import it.mulders.mcs.search.printer.clipboard.SystemClipboard;

import java.io.PrintStream;

Expand All @@ -18,8 +20,18 @@ default void print(final SearchQuery query, final SearchResponse.Response respon
}

var doc = response.docs()[0];
String coordinates = provideCoordinates(doc.g(), doc.a(), first(doc.v(), doc.latestVersion()));

stream.println();
stream.println(coordinates);
stream.println();
stream.println(provideCoordinates(doc.g(), doc.a(), first(doc.v(), doc.latestVersion())));

copyToClipboard(stream, new SystemClipboard(), coordinates);
}

private void copyToClipboard(PrintStream stream, Clipboard clipboard, String coordinates) {
clipboard.copy(coordinates);
stream.println("Snippet copied to clipboard.");
stream.println();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.mulders.mcs.search.printer.clipboard;

public sealed interface Clipboard permits SystemClipboard {
void copy(String text);
String paste();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package it.mulders.mcs.search.printer.clipboard;

import java.awt.*;
hannotify marked this conversation as resolved.
Show resolved Hide resolved
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import static java.awt.datatransfer.DataFlavor.stringFlavor;

public final class SystemClipboard implements Clipboard {
java.awt.datatransfer.Clipboard systemClipboard;

public SystemClipboard() {
this.systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
}

@Override
public void copy(String text) {
var clipboardData = new StringSelection(text);
systemClipboard.setContents(clipboardData, clipboardData);
}

@Override
public String paste() {
Transferable clipboardData = systemClipboard.getContents(null);
String text = "";
boolean hasTransferableText = clipboardData != null && clipboardData.isDataFlavorSupported(stringFlavor);

if (hasTransferableText) {
try {
text = (String) clipboardData.getTransferData(stringFlavor);
} catch (IOException | UnsupportedFlavorException e) {
throw new RuntimeException(e);
}
}

return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.Clipboard;
import it.mulders.mcs.search.printer.clipboard.SystemClipboard;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -34,7 +36,7 @@ class CoordinatePrinterTest implements WithAssertions {
</dependency>
""";
private static final String GRADLE_GROOVY_OUTPUT = "implementation group: 'org.codehaus.plexus', name: 'plexus-utils', version: '3.4.1'";
private static final String GRADLE_GROOVY_SHORT_OUTPUT = "'org.codehaus.plexus:plexus-utils:3.4.1'";
private static final String GRADLE_GROOVY_SHORT_OUTPUT = "implementation 'org.codehaus.plexus:plexus-utils:3.4.1'";
mthmulders marked this conversation as resolved.
Show resolved Hide resolved
private static final String GRADLE_KOTLIN_OUTPUT = "implementation(\"org.codehaus.plexus:plexus-utils:3.4.1\")";
private static final String SBT_OUTPUT = """
libraryDependencies += "org.codehaus.plexus" % "plexus-utils" % "3.4.1"
Expand All @@ -51,6 +53,7 @@ class CoordinatePrinterTest implements WithAssertions {
private static final String BUILDR_OUTPUT = "'org.codehaus.plexus:plexus-utils:jar:3.4.1'";

private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
private final Clipboard clipboard = new SystemClipboard();

private static Stream<Arguments> coordinatePrinters() {
return Stream.of(
Expand All @@ -74,4 +77,12 @@ void should_print_snippet(CoordinatePrinter printer, String expected) {

assertThat(xml).containsIgnoringWhitespaces(expected);
}

@ParameterizedTest
@MethodSource("coordinatePrinters")
void should_copy_to_clipboard(CoordinatePrinter printer, String expected) {
printer.print(QUERY, RESPONSE, new PrintStream(buffer));

assertThat(clipboard.paste()).isEqualToIgnoringWhitespace(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.mulders.mcs.search.printer.clipboard;

import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.Test;

class ClipboardTest implements WithAssertions {
private final Clipboard clipboard = new SystemClipboard();

@Test
void paste_result_should_be_equal_to_copied_text() {
var singleLineText = "'org.codehaus.plexus:plexus-utils:jar:3.4.1'";
clipboard.copy(singleLineText);
assertThat(clipboard.paste()).isEqualTo(singleLineText);

var multiLineText = """
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.1</version>
</dependency>
""";
clipboard.copy(multiLineText);
assertThat(clipboard.paste()).isEqualTo(multiLineText);
}
}