Skip to content

Commit

Permalink
Improve display with an easy opt-out option and support for dumb term…
Browse files Browse the repository at this point in the history
…inals, fixes #116 and #131
  • Loading branch information
gnodet committed Nov 11, 2020
1 parent e40d2bb commit c5e56d0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ public int maxLostKeepAlive() {
return property(Environment.DAEMON_MAX_LOST_KEEP_ALIVE).orFail().asInt();
}

public boolean noBuffering() {
return property(Environment.MVND_NO_BUFERING).orFail().asBoolean();
}

public static String findDefaultMultimoduleProjectDirectory(Path pwd) {
Path dir = pwd;
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public static void main(String[] argv) throws Exception {
}
}

try (TerminalOutput output = new TerminalOutput(logFile)) {
DaemonParameters parameters = new DaemonParameters();
try (TerminalOutput output = new TerminalOutput(parameters.noBuffering(), logFile)) {
try {
new DefaultClient(new DaemonParameters()).execute(output, args);
new DefaultClient(parameters).execute(output, args);
} catch (DaemonException.InterruptedException e) {
final AttributedStyle s = new AttributedStyle().bold().foreground(AttributedStyle.RED);
String str = new AttributedString(System.lineSeparator() + "Canceled by user", s).toAnsi();
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/org/jboss/fuse/mvnd/common/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@
* Collects system properties and environment variables used by mvnd client or server.
*/
public enum Environment {
//
// Log properties
//
LOGBACK_CONFIGURATION_FILE("logback.configurationFile", null, null, false),
//
// System properties
//
JAVA_HOME("java.home", "JAVA_HOME", null, false),
MVND_HOME("mvnd.home", "MVND_HOME", null, false),
USER_HOME("user.home", null, null, false),
USER_DIR("user.dir", null, null, false),
//
// Maven properties
//
MAVEN_REPO_LOCAL("maven.repo.local", null, null, false),
MAVEN_SETTINGS("maven.settings", null, null, false) {
@Override
Expand All @@ -45,8 +54,16 @@ public String asCommandLineProperty(String value) {
}
},
MAVEN_MULTIMODULE_PROJECT_DIRECTORY("maven.multiModuleProjectDirectory", null, null, false),
//
// mvnd properties
//
MVND_PROPERTIES_PATH("mvnd.properties.path", "MVND_PROPERTIES_PATH", null, false),
MVND_DAEMON_STORAGE("mvnd.daemon.storage", null, null, false),
/**
* Property that can be set to avoid buffering the output and display events continuously, closer to the usual maven
* display.
*/
MVND_NO_BUFERING("mvnd.noBuffering", null, "false", false),
/**
* The path to the daemon registry
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand All @@ -53,8 +52,8 @@
*/
public class TerminalOutput implements ClientOutput {

public static final int CTRL_B = 'B' & 0x1f;
public static final int CTRL_L = 'L' & 0x1f;

public static final int CTRL_M = 'M' & 0x1f;

private final Terminal terminal;
Expand All @@ -65,7 +64,6 @@ public class TerminalOutput implements ClientOutput {
private final Thread reader;
private volatile Exception exception;
private volatile boolean closing;
private final CountDownLatch closed = new CountDownLatch(1);
private final long start;
private final ReadWriteLock readInput = new ReentrantReadWriteLock();

Expand All @@ -90,6 +88,8 @@ public class TerminalOutput implements ClientOutput {
private int doneProjects = 0;
private String buildStatus;
private boolean displayDone = false;
private boolean noBuffering;
private boolean dumb;

/**
* {@link Project} is owned by the display loop thread and is accessed only from there. Therefore it does not need
Expand All @@ -105,9 +105,11 @@ public Project(String id) {
}
}

public TerminalOutput(Path logFile) throws IOException {
public TerminalOutput(boolean noBuffering, Path logFile) throws IOException {
this.start = System.currentTimeMillis();
this.terminal = TerminalBuilder.terminal();
this.dumb = terminal.getType().startsWith("dumb");
this.noBuffering = noBuffering;
terminal.enterRawMode();
Thread mainThread = Thread.currentThread();
daemonDispatch = m -> {
Expand All @@ -119,9 +121,13 @@ public TerminalOutput(Path logFile) throws IOException {
sig -> daemonDispatch.accept(Message.CANCEL_BUILD_SINGLETON));
this.display = new Display(terminal, false);
this.log = logFile == null ? new MessageCollector() : new FileLog(logFile);
final Thread r = new Thread(this::readInputLoop);
r.start();
this.reader = r;
if (!dumb) {
final Thread r = new Thread(this::readInputLoop);
r.start();
this.reader = r;
} else {
this.reader = null;
}
}

@Override
Expand Down Expand Up @@ -232,15 +238,19 @@ private boolean doAccept(Message entry) {
}
case Message.DISPLAY: {
Message.StringMessage d = (Message.StringMessage) entry;
display.update(Collections.emptyList(), 0);
clearDisplay();
terminal.writer().printf("%s%n", d.getMessage());
break;
}
case Message.PROMPT: {
Message.Prompt prompt = (Message.Prompt) entry;
if (dumb) {
terminal.writer().println("");
break;
}
readInput.writeLock().lock();
try {
display.update(Collections.emptyList(), 0);
clearDisplay();
terminal.writer().printf("[%s] %s", prompt.getProjectId(), prompt.getMessage());
terminal.flush();
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -273,29 +283,21 @@ private boolean doAccept(Message entry) {
}
case Message.BUILD_LOG_MESSAGE: {
StringMessage sm = (StringMessage) entry;
if (closing) {
try {
closed.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.err.println(sm.getMessage());
} else {
log.accept(sm.getMessage());
}
log.accept(sm.getMessage());
break;
}
case Message.PROJECT_LOG_MESSAGE: {
final ProjectEvent bm = (ProjectEvent) entry;
if (closing) {
try {
closed.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
final Project prj = projects.computeIfAbsent(bm.getProjectId(), Project::new);
if (noBuffering || dumb) {
String msg;
if (maxThreads > 1) {
msg = String.format("[%s] %s", bm.getProjectId(), bm.getMessage());
} else {
msg = bm.getMessage();
}
System.err.println(bm.getMessage());
log.accept(msg);
} else {
final Project prj = projects.computeIfAbsent(bm.getProjectId(), Project::new);
prj.log.add(bm.getMessage());
}
break;
Expand All @@ -309,8 +311,17 @@ private boolean doAccept(Message entry) {
case '-':
linesPerProject = Math.max(0, linesPerProject - 1);
break;
case CTRL_B:
noBuffering = !noBuffering;
if (noBuffering) {
projects.values().stream().flatMap(p -> p.log.stream()).forEach(log);
projects.clear();
} else {
clearDisplay();
}
break;
case CTRL_L:
display.update(Collections.emptyList(), 0);
clearDisplay();
break;
case CTRL_M:
displayDone = !displayDone;
Expand Down Expand Up @@ -344,7 +355,7 @@ void readInputLoop() {
if (c == -1) {
break;
}
if (c == '+' || c == '-' || c == CTRL_L || c == CTRL_M) {
if (c == '+' || c == '-' || c == CTRL_L || c == CTRL_M || c == CTRL_B) {
accept(Message.keyboardInput((char) c));
}
readInput.readLock().unlock();
Expand All @@ -360,7 +371,10 @@ void readInputLoop() {
}

private void clearDisplay() {
display.update(Collections.emptyList(), 0);
if (!noBuffering && !dumb) {
display.update(Collections.emptyList(), 0);
}

}

private void displayDone() {
Expand All @@ -376,18 +390,27 @@ private void displayDone() {
@Override
public void close() throws Exception {
closing = true;
reader.interrupt();
if (reader != null) {
reader.interrupt();
reader.join();
}
log.close();
reader.join();
terminal.handle(Terminal.Signal.INT, previousIntHandler);
terminal.close();
closed.countDown();
if (exception != null) {
throw exception;
}
}

private void update() {
if (noBuffering || dumb) {
try {
log.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return;
}
// no need to refresh the display at every single step
final Size size = terminal.getSize();
final int rows = size.getRows();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ public abstract class AbstractLoggingSpy {

public static AbstractLoggingSpy instance() {
if (instance == null) {
if ("mvns".equals(System.getProperty("mvnd.logging", "mvn"))) {
instance = new MavenLoggingSpy();
} else {
instance = new AbstractLoggingSpy() {
};
}
instance = new AbstractLoggingSpy() {
};
}
return instance;
}
Expand Down

This file was deleted.

Loading

0 comments on commit c5e56d0

Please sign in to comment.