mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-27 16:08:21 +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="))) {
|
if (localMavenRepository != null && args.stream().noneMatch(arg -> arg.startsWith("-Dmaven.repo.local="))) {
|
||||||
args.add("-Dmaven.repo.local=" + localMavenRepository.toString());
|
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);
|
final DaemonConnector connector = new DaemonConnector(parameters, registry);
|
||||||
try (DaemonClientConnection daemon = connector.connect(output)) {
|
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,
|
MVND_DUPLICATE_DAEMON_GRACE_PERIOD("mvnd.duplicateDaemonGracePeriod", null, "10 seconds", OptionType.DURATION,
|
||||||
Flags.DISCRIMINATING),
|
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;
|
static Properties properties;
|
||||||
|
|
||||||
|
@@ -33,4 +33,6 @@ public interface ClientOutput extends AutoCloseable {
|
|||||||
void accept(List<Message> messages);
|
void accept(List<Message> messages);
|
||||||
|
|
||||||
void describeTerminal();
|
void describeTerminal();
|
||||||
|
|
||||||
|
int getTerminalWidth();
|
||||||
}
|
}
|
||||||
|
@@ -355,6 +355,11 @@ public class TerminalOutput implements ClientOutput {
|
|||||||
this.accept(Message.log(sb.toString()));
|
this.accept(Message.log(sb.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTerminalWidth() {
|
||||||
|
return terminal.getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
void readInputLoop() {
|
void readInputLoop() {
|
||||||
try {
|
try {
|
||||||
while (!closing) {
|
while (!closing) {
|
||||||
|
@@ -182,6 +182,7 @@ public class DaemonMavenCli {
|
|||||||
initialize(cliRequest);
|
initialize(cliRequest);
|
||||||
cli(cliRequest);
|
cli(cliRequest);
|
||||||
properties(cliRequest);
|
properties(cliRequest);
|
||||||
|
help(cliRequest);
|
||||||
logging(cliRequest);
|
logging(cliRequest);
|
||||||
container(cliRequest);
|
container(cliRequest);
|
||||||
configure(cliRequest);
|
configure(cliRequest);
|
||||||
@@ -266,9 +267,11 @@ public class DaemonMavenCli {
|
|||||||
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(cliManager));
|
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(cliManager));
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void help(CliRequest cliRequest) throws Exception {
|
||||||
if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
|
if (cliRequest.commandLine.hasOption(CLIManager.HELP)) {
|
||||||
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(cliManager));
|
AbstractLoggingSpy.instance().append(MvndHelpFormatter.displayHelp(new CLIManager()));
|
||||||
throw new ExitException(0);
|
throw new ExitException(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -17,6 +17,7 @@ package org.apache.maven.cli;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -47,9 +48,15 @@ public class MvndHelpFormatter {
|
|||||||
* @return the string containing the help message
|
* @return the string containing the help message
|
||||||
*/
|
*/
|
||||||
public static String displayHelp(CLIManager cliManager) {
|
public static String displayHelp(CLIManager cliManager) {
|
||||||
|
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
|
||||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
try (PrintStream out = new PrintStream(baos, false, StandardCharsets.UTF_8.name())) {
|
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) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -68,7 +75,7 @@ public class MvndHelpFormatter {
|
|||||||
final Environment env = entry.getEntry();
|
final Environment env = entry.getEntry();
|
||||||
help.append(lineSeparator);
|
help.append(lineSeparator);
|
||||||
int indentPos = help.length() + indent.length();
|
int indentPos = help.length() + indent.length();
|
||||||
int lineEnd = help.length() + HelpFormatter.DEFAULT_WIDTH;
|
int lineEnd = help.length() + terminalWidth;
|
||||||
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
|
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
|
||||||
final String property = env.getProperty();
|
final String property = env.getProperty();
|
||||||
if (property != null) {
|
if (property != null) {
|
||||||
@@ -108,7 +115,7 @@ public class MvndHelpFormatter {
|
|||||||
help.append(' ');
|
help.append(' ');
|
||||||
|
|
||||||
spaces(help, indentPos - help.length());
|
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, "Default", env.getDefault(), indent);
|
||||||
indentedLine(help, "Env. variable", env.getEnvironmentVariable(), indent);
|
indentedLine(help, "Env. variable", env.getEnvironmentVariable(), indent);
|
||||||
@@ -126,11 +133,11 @@ public class MvndHelpFormatter {
|
|||||||
final OptionType type = entry.getEntry();
|
final OptionType type = entry.getEntry();
|
||||||
help.append(lineSeparator);
|
help.append(lineSeparator);
|
||||||
int indentPos = help.length() + indent.length();
|
int indentPos = help.length() + indent.length();
|
||||||
int lineEnd = help.length() + HelpFormatter.DEFAULT_WIDTH;
|
int lineEnd = help.length() + terminalWidth;
|
||||||
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
|
spaces(help, HelpFormatter.DEFAULT_LEFT_PAD);
|
||||||
help.append(type.name().toLowerCase(Locale.ROOT));
|
help.append(type.name().toLowerCase(Locale.ROOT));
|
||||||
spaces(help, indentPos - help.length());
|
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();
|
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) {
|
private static void indentedLine(final StringBuilder stringBuilder, String key, final String value, final String indent) {
|
||||||
int lineEnd;
|
int lineEnd;
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
lineEnd = stringBuilder.length() + HelpFormatter.DEFAULT_WIDTH;
|
int terminalWidth = Environment.MVND_TERMINAL_WIDTH.asInt();
|
||||||
|
lineEnd = stringBuilder.length() + terminalWidth;
|
||||||
stringBuilder
|
stringBuilder
|
||||||
.append(System.lineSeparator())
|
.append(System.lineSeparator())
|
||||||
.append(indent);
|
.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"));
|
accept(Message.display("Test terminal"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getTerminalWidth() {
|
||||||
|
return 74;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Message> getMessages() {
|
public List<Message> getMessages() {
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
@@ -70,6 +70,7 @@ public class NativeTestClient implements Client {
|
|||||||
final String threads = parameters.threads();
|
final String threads = parameters.threads();
|
||||||
Environment.MVND_THREADS.appendAsCommandLineOption(cmd::add, 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]))
|
final ProcessBuilder builder = new ProcessBuilder(cmd.toArray(new String[0]))
|
||||||
.directory(parameters.userDir().toFile()) //
|
.directory(parameters.userDir().toFile()) //
|
||||||
|
Reference in New Issue
Block a user