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 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
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).


30 changes: 28 additions & 2 deletions src/main/java/it/mulders/mcs/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.mulders.mcs.search.SearchCommandHandler;
import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.printer.CoordinatePrinter;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;
import picocli.CommandLine;

import java.util.concurrent.Callable;
Expand All @@ -15,6 +16,8 @@
versionProvider = ClasspathVersionProvider.class
)
public class Cli {
final static String COPY_SHORT_FLAG_NAME = "-c";
final static String COPY_LONG_FLAG_NAME = "--copy";

@CommandLine.Option(
names = { "-V", "--version" },
Expand All @@ -39,6 +42,13 @@ public ClassSearchCommand createClassSearchCommand() {
return new ClassSearchCommand();
}

private CopyToClipboardConfig createCopyToClipboardConfiguration(boolean copyToClipboard) {
return new CopyToClipboardConfig(
COPY_SHORT_FLAG_NAME,
COPY_LONG_FLAG_NAME,
copyToClipboard);
}

@CommandLine.Command(
name = "search",
description = "Search artifacts in Maven Central by coordinates",
Expand All @@ -55,6 +65,14 @@ public class SearchCommand implements Callable<Integer> {
)
private String[] query;

@CommandLine.Option(
names = { COPY_SHORT_FLAG_NAME, COPY_LONG_FLAG_NAME },
negatable = true,
arity = "0",
description = "Copy dependency definition to the clipboard when a single result is found"
)
private boolean copyToClipboard;

@CommandLine.Option(
names = { "-l", "--limit" },
description = "Show <count> results",
Expand Down Expand Up @@ -83,7 +101,7 @@ public Integer call() {

CoordinatePrinter coordinatePrinter = FormatType.providePrinter(responseFormat);
var searchCommandHandler = new SearchCommandHandler(coordinatePrinter);
searchCommandHandler.search(searchQuery);
searchCommandHandler.search(searchQuery, createCopyToClipboardConfiguration(copyToClipboard));
return 0;
}
}
Expand All @@ -102,6 +120,14 @@ public class ClassSearchCommand implements Callable<Integer> {
)
private String query;

@CommandLine.Option(
names = { COPY_SHORT_FLAG_NAME, COPY_LONG_FLAG_NAME },
negatable = true,
arity = "0",
description = "Copy dependency definition to the clipboard when a single result is found"
)
private boolean copyToClipboard;

@CommandLine.Option(
names = { "-f", "--full-name" },
negatable = true,
Expand All @@ -125,7 +151,7 @@ public Integer call() {
.withLimit(limit)
.build();
var searchCommandHandler = new SearchCommandHandler();
searchCommandHandler.search(searchQuery);
searchCommandHandler.search(searchQuery, createCopyToClipboardConfiguration(copyToClipboard));
return 0;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/it/mulders/mcs/search/SearchCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.mulders.mcs.common.Result;
import it.mulders.mcs.search.printer.DelegatingOutputPrinter;
import it.mulders.mcs.search.printer.OutputPrinter;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;

import static it.mulders.mcs.search.Constants.MAX_LIMIT;

Expand All @@ -24,10 +25,10 @@ public SearchCommandHandler(final OutputPrinter coordinateOutput) {
this.outputPrinter = outputPrinter;
}

public void search(final SearchQuery query) {
public void search(final SearchQuery query, final CopyToClipboardConfig configuration) {
performSearch(query)
.map(response -> performAdditionalSearch(query, response))
.ifPresent(response -> printResponse(query, response));
.ifPresent(response -> printResponse(query, response, configuration));
}

private SearchResponse.Response performAdditionalSearch(final SearchQuery query,
Expand Down Expand Up @@ -64,7 +65,8 @@ private Result<SearchResponse.Response> performSearch(final SearchQuery query) {
.map(SearchResponse::response);
}

private void printResponse(final SearchQuery query, final SearchResponse.Response response) {
outputPrinter.print(query, response, System.out);
private void printResponse(final SearchQuery query, final SearchResponse.Response response,
final CopyToClipboardConfig configuration) {
outputPrinter.print(query, response, System.out, configuration);
}
}
33 changes: 30 additions & 3 deletions src/main/java/it/mulders/mcs/search/printer/CoordinatePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,54 @@

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.CopyToClipboardConfig;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardPrinter;
import it.mulders.mcs.search.printer.clipboard.SystemClipboard;

import java.io.PrintStream;

public sealed interface CoordinatePrinter extends OutputPrinter
public sealed interface CoordinatePrinter extends OutputPrinter, CopyToClipboardPrinter
permits BuildrOutput, GradleGroovyOutput, GradleGroovyShortOutput, GradleKotlinOutput, GrapeOutput,
IvyXmlOutput, LeiningenOutput, PomXmlOutput, SbtOutput {

String provideCoordinates(final String group, final String artifact, final String version);

@Override
default void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) {
default void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream,
final CopyToClipboardConfig copyToClipboardConfig) {
if (response.numFound() != 1) {
throw new IllegalArgumentException("Search response with more than one result not expected here");
}

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())));

if (copyToClipboardConfig.copyToClipboardEnabled()) {
copyToClipboard(stream, new SystemClipboard(), coordinates);
} else {
printCopyToClipboardHint(stream, copyToClipboardConfig);
}

stream.println();
}

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

private static void printCopyToClipboardHint(PrintStream stream,
CopyToClipboardConfig copyToClipboardConfig) {
stream.printf("(To directly copy this snippet to the clipboard, run mcs with the %s or %s flag.)%n",
copyToClipboardConfig.shortFlagName(),
copyToClipboardConfig.longFlagName());
}

private String first(final String... values) {
for (var value : values) {
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;

import java.io.PrintStream;

Expand All @@ -25,11 +26,12 @@ public DelegatingOutputPrinter(final OutputPrinter coordinateOutput) {
}

@Override
public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) {
public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream,
final CopyToClipboardConfig copyToClipboardConfig) {
switch (response.numFound()) {
case 0 -> noOutput.print(query, response, stream);
case 1 -> coordinateOutput.print(query, response, stream);
default -> tabularSearchOutput.print(query, response, stream);
case 0 -> noOutput.print(query, response, stream, null);
case 1 -> coordinateOutput.print(query, response, stream, copyToClipboardConfig);
default -> tabularSearchOutput.print(query, response, stream, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;

import java.io.PrintStream;

public class NoOutputPrinter implements OutputPrinter {
@Override
public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) {
public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream,
final CopyToClipboardConfig configuration) {
if (response.numFound() != 0) {
throw new IllegalArgumentException("Search response with any result not expected here");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;

import java.io.PrintStream;

public interface OutputPrinter {
void print(final SearchQuery query,
final SearchResponse.Response response,
final PrintStream stream);
final PrintStream stream,
final CopyToClipboardConfig configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;
import picocli.CommandLine;
import picocli.CommandLine.Help;
import picocli.CommandLine.Help.Ansi;
Expand Down Expand Up @@ -29,7 +30,8 @@ private String header(final SearchQuery query, final SearchResponse.Response res
response.numFound(), additionalMessage);
}

public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream) {
public void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream,
final CopyToClipboardConfig configuration) {
stream.println(CommandLine.Help.Ansi.AUTO.string(header(query, response)));

var colorScheme = Help.defaultColorScheme(Ansi.AUTO);
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,4 @@
package it.mulders.mcs.search.printer.clipboard;

public record CopyToClipboardConfig(String shortFlagName, String longFlagName, boolean copyToClipboardEnabled) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package it.mulders.mcs.search.printer.clipboard;

import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.SearchResponse;

import java.io.PrintStream;

public interface CopyToClipboardPrinter {
void print(final SearchQuery query, final SearchResponse.Response response, final PrintStream stream,
final CopyToClipboardConfig copyToClipboardConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package it.mulders.mcs.search.printer.clipboard;

import java.awt.Toolkit;
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;
}
}
6 changes: 5 additions & 1 deletion src/test/java/it/mulders/mcs/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.mulders.mcs.search.Constants;
import it.mulders.mcs.search.SearchCommandHandler;
import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.printer.clipboard.CopyToClipboardConfig;
import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand Down Expand Up @@ -73,11 +74,14 @@ void accepts_full_name_parameter() {
}

private void verifySearchExecution(SearchQuery query, String... args) {
var dontCopyToClipboard = new CopyToClipboardConfig(Cli.COPY_SHORT_FLAG_NAME, Cli.COPY_LONG_FLAG_NAME,
false);

try (MockedConstruction<SearchCommandHandler> mocked = Mockito.mockConstruction(SearchCommandHandler.class)) {
program.execute(args);
assertThat(mocked.constructed()).hasSize(1);
SearchCommandHandler searchCommandHandler = mocked.constructed().get(0);
verify(searchCommandHandler).search(query);
verify(searchCommandHandler).search(query, dontCopyToClipboard);
}
}
}
Loading