Use client terminal width to format help

This commit is contained in:
Guillaume Nodet
2020-12-08 22:28:59 +01:00
parent 79137867e5
commit 27528cec37
8 changed files with 37 additions and 9 deletions

View File

@@ -242,6 +242,7 @@ public class DefaultClient implements Client {
if (localMavenRepository != null && args.stream().noneMatch(arg -> arg.startsWith("-Dmaven.repo.local="))) {
args.add("-Dmaven.repo.local=" + localMavenRepository.toString());
}
Environment.MVND_TERMINAL_WIDTH.appendAsCommandLineOption(args::add, Integer.toString(output.getTerminalWidth()));
final DaemonConnector connector = new DaemonConnector(parameters, registry);
try (DaemonClientConnection daemon = connector.connect(output)) {

View File

@@ -199,7 +199,10 @@ public enum Environment {
*/
MVND_DUPLICATE_DAEMON_GRACE_PERIOD("mvnd.duplicateDaemonGracePeriod", null, "10 seconds", OptionType.DURATION,
Flags.DISCRIMINATING),
;
/**
* Internal property to tell the daemon the width of the terminal
*/
MVND_TERMINAL_WIDTH("mvnd.terminalWidth", null, 0, OptionType.INTEGER, Flags.INTERNAL);
static Properties properties;

View File

@@ -33,4 +33,6 @@ public interface ClientOutput extends AutoCloseable {
void accept(List<Message> messages);
void describeTerminal();
int getTerminalWidth();
}

View File

@@ -355,6 +355,11 @@ public class TerminalOutput implements ClientOutput {
this.accept(Message.log(sb.toString()));
}
@Override
public int getTerminalWidth() {
return terminal.getWidth();
}
void readInputLoop() {
try {
while (!closing) {

View File

@@ -182,6 +182,7 @@ public class DaemonMavenCli {
initialize(cliRequest);
cli(cliRequest);
properties(cliRequest);
help(cliRequest);
logging(cliRequest);
container(cliRequest);
configure(cliRequest);
@@ -266,9 +267,11 @@ public class DaemonMavenCli {
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(cliManager));
throw e;
}
}
private void help(CliRequest cliRequest) throws Exception {
if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(cliManager));
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(new CLIManager()));
throw new ExitException(0);
}

View File

@@ -17,6 +17,7 @@ package org.apache.maven.cli;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
@@ -47,9 +48,15 @@ public class MvndHelpFormatter {
* @return the string containing the help message
*/
public static String displayHelp(CLIManager cliManager) {
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintStream out = new PrintStream(baos, false, StandardCharsets.UTF_8.name())) {
cliManager.displayHelp(out);
out.println();
PrintWriter pw = new PrintWriter(out);
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(pw, terminalWidth, "mvn [options] [<goal(s)>] [<phase(s)>]", "\nOptions:", cliManager.options,
1, 3, "\n", false);
pw.flush();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
@@ -68,7 +75,7 @@ public class MvndHelpFormatter {
final Environment env = entry.getEntry();
help.append(lineSeparator);
int indentPos = help.length() + indent.length();
int lineEnd = help.length() + HelpFormatter.DEFAULT_WIDTH;
int lineEnd = help.length() + terminalWidth;
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
final String property = env.getProperty();
if (property != null) {
@@ -108,7 +115,7 @@ public class MvndHelpFormatter {
help.append(' ');
spaces(help, indentPos - help.length());
wrap(help, toPlainText(entry.getJavaDoc()), HelpFormatter.DEFAULT_WIDTH, lineEnd, indent);
wrap(help, toPlainText(entry.getJavaDoc()), terminalWidth, lineEnd, indent);
indentedLine(help, "Default", env.getDefault(), indent);
indentedLine(help, "Env. variable", env.getEnvironmentVariable(), indent);
@@ -126,11 +133,11 @@ public class MvndHelpFormatter {
final OptionType type = entry.getEntry();
help.append(lineSeparator);
int indentPos = help.length() + indent.length();
int lineEnd = help.length() + HelpFormatter.DEFAULT_WIDTH;
int lineEnd = help.length() + terminalWidth;
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
help.append(type.name().toLowerCase(Locale.ROOT));
spaces(help, indentPos - help.length());
wrap(help, toPlainText(entry.getJavaDoc()), HelpFormatter.DEFAULT_WIDTH, lineEnd, indent);
wrap(help, toPlainText(entry.getJavaDoc()), terminalWidth, lineEnd, indent);
});
return help.toString();
@@ -139,11 +146,12 @@ public class MvndHelpFormatter {
private static void indentedLine(final StringBuilder stringBuilder, String key, final String value, final String indent) {
int lineEnd;
if (value != null) {
lineEnd = stringBuilder.length() + HelpFormatter.DEFAULT_WIDTH;
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
lineEnd = stringBuilder.length() + terminalWidth;
stringBuilder
.append(System.lineSeparator())
.append(indent);
wrap(stringBuilder, key + ": " + value, HelpFormatter.DEFAULT_WIDTH, lineEnd, indent);
wrap(stringBuilder, key + ": " + value, terminalWidth, lineEnd, indent);
}
}

View File

@@ -57,6 +57,11 @@ public class TestClientOutput implements ClientOutput {
accept(Message.display("Test terminal"));
}
@Override
public int getTerminalWidth() {
return 74;
}
public List<Message> getMessages() {
return messages;
}

View File

@@ -70,6 +70,7 @@ public class NativeTestClient implements Client {
final String threads = parameters.threads();
Environment.MVND_THREADS.appendAsCommandLineOption(cmd::add, threads);
}
Environment.MVND_TERMINAL_WIDTH.appendAsCommandLineOption(cmd::add, Integer.toString(output.getTerminalWidth()));
final ProcessBuilder builder = new ProcessBuilder(cmd.toArray(new String[0]))
.directory(parameters.userDir().toFile()) //