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

@@ -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);
}
}