Skip to content

Commit

Permalink
Merge branch 'develop' into match-merging
Browse files Browse the repository at this point in the history
# Conflicts:
#	core/src/main/java/de/jplag/GreedyStringTiling.java
  • Loading branch information
uuqjz committed Aug 18, 2023
2 parents 1271a0d + 2d85029 commit 49633bf
Show file tree
Hide file tree
Showing 70 changed files with 2,021 additions and 774 deletions.
14 changes: 11 additions & 3 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package de.jplag.cli;

import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_FOOTER;
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_DESCRIPTION_HEADING;
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_OPTION_LIST;
import static picocli.CommandLine.Model.UsageMessageSpec.SECTION_KEY_SYNOPSIS;

import java.io.File;
import java.security.SecureRandom;
Expand Down Expand Up @@ -50,12 +51,16 @@ public final class CLI {
"More Abstract than Tree", "Students Nightmare", "No, changing variable names does not work", "The tech is out there!",
"Developed by plagiarism experts."};

private static final String OPTION_LIST_HEADING = "Parameter descriptions: ";

private final CommandLine commandLine;
private final CliOptions options;

private static final String IMPOSSIBLE_EXCEPTION = "This should not have happened."
+ " Please create an issue on github (https://github.com/jplag/JPlag/issues) with the entire output.";

private static final String DESCRIPTION_PATTERN = "%nJPlag - %s%n%s%n%n";

/**
* Main class for using JPlag via the CLI.
* @param args are the CLI arguments that will be passed to JPlag.
Expand Down Expand Up @@ -88,6 +93,8 @@ public CLI() {
this.options = new CliOptions();
this.commandLine = new CommandLine(options);

this.commandLine.setHelpFactory(new HelpFactory());

this.commandLine.getHelpSectionMap().put(SECTION_KEY_OPTION_LIST, help -> help.optionList().lines().map(it -> {
if (it.startsWith(" -")) {
return " " + it;
Expand All @@ -98,7 +105,8 @@ public CLI() {

buildSubcommands().forEach(commandLine::addSubcommand);

this.commandLine.getHelpSectionMap().put(SECTION_KEY_FOOTER, help -> generateDescription());
this.commandLine.getHelpSectionMap().put(SECTION_KEY_SYNOPSIS, help -> help.synopsis(help.synopsisHeadingLength()) + generateDescription());
this.commandLine.getHelpSectionMap().put(SECTION_KEY_DESCRIPTION_HEADING, help -> OPTION_LIST_HEADING);
this.commandLine.setAllowSubcommandsAsOptionParameters(true);
}

Expand Down Expand Up @@ -228,7 +236,7 @@ private static MergingParameters getMergingParameters(CliOptions options) {

private String generateDescription() {
var randomDescription = DESCRIPTIONS[RANDOM.nextInt(DESCRIPTIONS.length)];
return String.format("JPlag - %s%n%n%s", randomDescription, CREDITS);
return String.format(DESCRIPTION_PATTERN, randomDescription, CREDITS);
}

public String getResultFolder() {
Expand Down
26 changes: 26 additions & 0 deletions cli/src/main/java/de/jplag/cli/CustomHelp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.jplag.cli;

import picocli.CommandLine;

/**
* Custom implementation for the help page, including the custom {@link ParamLabelRenderer}
*/
public class CustomHelp extends CommandLine.Help {
private final IParamLabelRenderer paramLabelRenderer;

/**
* New instance
* @param command The {@link picocli.CommandLine.Model.CommandSpec} to build the help for
* @param colorScheme The {@link picocli.CommandLine.Help.ColorScheme} for the help page
*/
public CustomHelp(CommandLine.Model.CommandSpec command, ColorScheme colorScheme) {
super(command, colorScheme);

this.paramLabelRenderer = new ParamLabelRenderer(super.parameterLabelRenderer());
}

@Override
public IParamLabelRenderer parameterLabelRenderer() {
return this.paramLabelRenderer;
}
}
13 changes: 13 additions & 0 deletions cli/src/main/java/de/jplag/cli/HelpFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.jplag.cli;

import picocli.CommandLine;

/**
* Custom help factory, used to add the custom {@link ParamLabelRenderer}.
*/
public class HelpFactory implements CommandLine.IHelpFactory {
@Override
public CommandLine.Help create(CommandLine.Model.CommandSpec commandSpec, CommandLine.Help.ColorScheme colorScheme) {
return new CustomHelp(commandSpec, colorScheme);
}
}
44 changes: 44 additions & 0 deletions cli/src/main/java/de/jplag/cli/ParamLabelRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.jplag.cli;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import picocli.CommandLine;

/**
* Custom implementation of {@link picocli.CommandLine.Help.IParamLabelRenderer}, that show the available options for
* enums. For all other parameter types, the base renderer is called.
*/
public class ParamLabelRenderer implements CommandLine.Help.IParamLabelRenderer {
private final CommandLine.Help.IParamLabelRenderer base;

private static final String PARAM_LABEL_PATTERN = "=<{%s}>";
private static final String VALUE_SEPARATOR = ", ";

/**
* New instance
* @param base The base renderer used for all non enum types
*/
public ParamLabelRenderer(CommandLine.Help.IParamLabelRenderer base) {
this.base = base;
}

@Override
public CommandLine.Help.Ansi.Text renderParameterLabel(CommandLine.Model.ArgSpec argSpec, CommandLine.Help.Ansi ansi,
List<CommandLine.Help.Ansi.IStyle> styles) {
if (argSpec.type().isEnum()) {
@SuppressWarnings("unchecked")
Enum<?>[] enumConstants = ((Class<Enum<?>>) argSpec.type()).getEnumConstants();
String enumValueNames = Arrays.stream(enumConstants).map(Enum::name).collect(Collectors.joining(VALUE_SEPARATOR));
return CommandLine.Help.Ansi.AUTO.text(String.format(PARAM_LABEL_PATTERN, enumValueNames));
}

return base.renderParameterLabel(argSpec, ansi, styles);
}

@Override
public String separator() {
return base.separator();
}
}
29 changes: 29 additions & 0 deletions cli/src/main/java/de/jplag/cli/logger/CollectedLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private String renderLevel(int level) {
};
}

@Override
public boolean isTraceEnabled() {
return isLevelEnabled(LOG_LEVEL_TRACE);
}
Expand All @@ -147,114 +148,142 @@ public void trace(String message) {
log(LOG_LEVEL_TRACE, message, null);
}

@Override
public void trace(String format, Object param1) {
formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
}

@Override
public void trace(String format, Object param1, Object param2) {
formatAndLog(LOG_LEVEL_TRACE, format, param1, param2);
}

@Override
public void trace(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_TRACE, format, argArray);
}

@Override
public void trace(String message, Throwable t) {
log(LOG_LEVEL_TRACE, message, t);
}

@Override
public boolean isDebugEnabled() {
return isLevelEnabled(LOG_LEVEL_DEBUG);
}

@Override
public void debug(String message) {
log(LOG_LEVEL_DEBUG, message, null);
}

@Override
public void debug(String format, Object param1) {
formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
}

@Override
public void debug(String format, Object param1, Object param2) {
formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2);
}

@Override
public void debug(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_DEBUG, format, argArray);
}

@Override
public void debug(String message, Throwable throwable) {
log(LOG_LEVEL_DEBUG, message, throwable);
}

@Override
public boolean isInfoEnabled() {
return isLevelEnabled(LOG_LEVEL_INFO);
}

@Override
public void info(String message) {
log(LOG_LEVEL_INFO, message, null);
}

@Override
public void info(String format, Object arg) {
formatAndLog(LOG_LEVEL_INFO, format, arg, null);
}

@Override
public void info(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2);
}

@Override
public void info(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_INFO, format, argArray);
}

@Override
public void info(String message, Throwable throwable) {
log(LOG_LEVEL_INFO, message, throwable);
}

@Override
public boolean isWarnEnabled() {
return isLevelEnabled(LOG_LEVEL_WARN);
}

@Override
public void warn(String message) {
log(LOG_LEVEL_WARN, message, null);
}

@Override
public void warn(String format, Object arg) {
formatAndLog(LOG_LEVEL_WARN, format, arg, null);
}

@Override
public void warn(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2);
}

@Override
public void warn(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_WARN, format, argArray);
}

@Override
public void warn(String message, Throwable throwable) {
log(LOG_LEVEL_WARN, message, throwable);
}

@Override
public boolean isErrorEnabled() {
return isLevelEnabled(LOG_LEVEL_ERROR);
}

@Override
public void error(String message) {
log(LOG_LEVEL_ERROR, message, null);
}

@Override
public void error(String format, Object arg) {
formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
}

@Override
public void error(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2);
}

@Override
public void error(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_ERROR, format, argArray);
}

@Override
public void error(String message, Throwable throwable) {
log(LOG_LEVEL_ERROR, message, throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public CollectedLoggerFactory() {
/**
* Return an appropriate {@link CollectedLogger} instance by name.
*/
@Override
public Logger getLogger(String name) {
CollectedLogger simpleLogger = loggerMap.get(name);
if (simpleLogger != null) {
Expand Down
32 changes: 32 additions & 0 deletions cli/src/test/java/de/jplag/cli/CustomHelpTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.jplag.cli;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import picocli.CommandLine;

/**
* Tests for the {@link CustomHelp} class
*/
class CustomHelpTests {
private CommandLine.Help help;

/**
* Creates the help object
*/
@BeforeEach
void setup() {
CommandLine.Model.CommandSpec commandSpec = CommandLine.Model.CommandSpec.create();
this.help = new HelpFactory().create(commandSpec, new CommandLine(commandSpec).getColorScheme());
}

/**
* Tests, that the custom help object returns the custom label renderer
*/
@Test
void testReturnsCustomRenderer() {
Assertions.assertTrue(this.help.parameterLabelRenderer() instanceof ParamLabelRenderer,
"The custom help object returned the wrong ParamLabelRenderer type.");
}
}
Loading

0 comments on commit 49633bf

Please sign in to comment.