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

Directly use the PTY to restore state #21294

Merged
merged 1 commit into from
Nov 9, 2021
Merged
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
51 changes: 25 additions & 26 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -27,14 +28,13 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.aesh.readline.tty.terminal.TerminalConnection;
import org.aesh.readline.terminal.impl.ExecPty;
import org.aesh.readline.terminal.impl.Pty;
import org.aesh.terminal.Attributes;
import org.aesh.terminal.Connection;
import org.aesh.terminal.utils.ANSI;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -337,7 +337,7 @@ public class DevMojo extends AbstractMojo {
private Attributes attributes;
private int windowsAttributes;
private boolean windowsAttributesSet;
private Connection connection;
private Pty pty;
private boolean windowsColorSupport;

@Override
Expand Down Expand Up @@ -438,21 +438,15 @@ private void saveTerminalState() {
}
}
} catch (Throwable t) {
//this only works with a proper PTY based terminal
//Aesh creates an input pump thread, that will steal
//input from the dev mode process
try {
//this does not work on windows
//jansi creates an input pump thread, that will steal
//input from the dev mode process
new TerminalConnection(new Consumer<Connection>() {
@Override
public void accept(Connection connection) {
attributes = connection.getAttributes();
DevMojo.this.connection = connection;
}
});
} catch (IOException e) {
getLog().error(
"Failed to setup console restore, console may be left in an inconsistent state if the process is killed",
e);
Pty pty = ExecPty.current();
attributes = pty.getAttr();
DevMojo.this.pty = pty;
} catch (Exception e) {
getLog().debug("Failed to get a local tty", e);
}
}
}
Expand All @@ -461,16 +455,21 @@ private void restoreTerminalState() {
if (windowsAttributesSet) {
WindowsSupport.setConsoleMode(windowsAttributes);
} else {
if (attributes == null || connection == null) {
if (attributes == null || pty == null) {
return;
}
connection.setAttributes(attributes);
int height = connection.size().getHeight();
connection.write(ANSI.MAIN_BUFFER);
connection.write(ANSI.CURSOR_SHOW);
connection.write("\u001B[0m");
connection.write("\033[" + height + ";0H");
connection.close();
Pty finalPty = pty;
try (finalPty) {
finalPty.setAttr(attributes);
int height = finalPty.getSize().getHeight();
String sb = ANSI.MAIN_BUFFER +
ANSI.CURSOR_SHOW +
"\u001B[0m" +
"\033[" + height + ";0H";
finalPty.getSlaveOutput().write(sb.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
getLog().error("Error restoring console state", e);
}
}
}

Expand Down