Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use client terminal width to format help #252

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface ClientOutput extends AutoCloseable {
void accept(List<Message> messages);

void describeTerminal();

int getTerminalWidth();
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ public void describeTerminal() {
this.accept(Message.log(sb.toString()));
}

@Override
public int getTerminalWidth() {
return terminal.getWidth();
}

void readInputLoop() {
try {
while (!closing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public int doMain(CliRequest cliRequest, Map<String, String> clientEnv) throws E
initialize(cliRequest);
cli(cliRequest);
properties(cliRequest);
help(cliRequest);
logging(cliRequest);
container(cliRequest);
configure(cliRequest);
Expand Down Expand Up @@ -266,9 +267,11 @@ void cli(CliRequest cliRequest)
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);
}

Expand Down
22 changes: 15 additions & 7 deletions daemon/src/main/java/org/apache/maven/cli/MvndHelpFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;
Expand Down Expand Up @@ -47,9 +48,15 @@ static String toPlainText(String javadocText) {
* @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);
}
Expand All @@ -68,7 +75,7 @@ public static String displayHelp(CLIManager cliManager) {
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);
help
.append("-D")
Expand All @@ -92,7 +99,7 @@ public static String displayHelp(CLIManager cliManager) {
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);
Expand All @@ -110,11 +117,11 @@ public static String displayHelp(CLIManager cliManager) {
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();
Expand All @@ -123,11 +130,12 @@ public static String displayHelp(CLIManager cliManager) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public void describeTerminal() {
accept(Message.display("Test terminal"));
}

@Override
public int getTerminalWidth() {
return 74;
}

public List<Message> getMessages() {
return messages;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public ExecutionResult execute(ClientOutput output, List<String> args) throws In
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()) //
Expand Down