mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-27 07:55:25 +00:00
Use client terminal width to format help
This commit is contained in:
@@ -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)) {
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -33,4 +33,6 @@ public interface ClientOutput extends AutoCloseable {
|
||||
void accept(List<Message> messages);
|
||||
|
||||
void describeTerminal();
|
||||
|
||||
int getTerminalWidth();
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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()) //
|
||||
|
Reference in New Issue
Block a user