org.apache.maven.cli.jline.JLineMessageBuilderFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-embedder Show documentation
Show all versions of maven-embedder Show documentation
Maven embeddable component, with CLI and logging support.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.cli.jline;
import javax.annotation.Priority;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.services.MessageBuilder;
import org.apache.maven.api.services.MessageBuilderFactory;
import org.codehaus.plexus.components.interactivity.InputHandler;
import org.codehaus.plexus.components.interactivity.OutputHandler;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.StyleResolver;
import static org.jline.utils.AttributedStyle.DEFAULT;
@Experimental
@Named
@Singleton
@Priority(10)
public class JLineMessageBuilderFactory implements MessageBuilderFactory, Prompter, InputHandler, OutputHandler {
private final StyleResolver resolver;
public JLineMessageBuilderFactory() {
this.resolver = new MavenStyleResolver();
}
@Override
public boolean isColorEnabled() {
return false;
}
@Override
public int getTerminalWidth() {
return MessageUtils.getTerminalWidth();
}
@Override
public MessageBuilder builder() {
return new JlineMessageBuilder();
}
@Override
public MessageBuilder builder(int size) {
return new JlineMessageBuilder(size);
}
@Override
public String readLine() throws IOException {
return doPrompt(null, true);
}
@Override
public String readPassword() throws IOException {
return doPrompt(null, true);
}
@Override
public List readMultipleLines() throws IOException {
List lines = new ArrayList<>();
for (String line = this.readLine(); line != null && !line.isEmpty(); line = readLine()) {
lines.add(line);
}
return lines;
}
@Override
public void write(String line) throws IOException {
doDisplay(line);
}
@Override
public void writeLine(String line) throws IOException {
doDisplay(line + System.lineSeparator());
}
@Override
public String prompt(String message) throws PrompterException {
return prompt(message, null, null);
}
@Override
public String prompt(String message, String defaultReply) throws PrompterException {
return prompt(message, null, defaultReply);
}
@Override
public String prompt(String message, List possibleValues) throws PrompterException {
return prompt(message, possibleValues, null);
}
@Override
public String prompt(String message, List possibleValues, String defaultReply) throws PrompterException {
return doPrompt(message, possibleValues, defaultReply, false);
}
@Override
public String promptForPassword(String message) throws PrompterException {
return doPrompt(message, null, null, true);
}
@Override
public void showMessage(String message) throws PrompterException {
try {
doDisplay(message);
} catch (IOException e) {
throw new PrompterException("Failed to present prompt", e);
}
}
String doPrompt(String message, List