Skip to content

Commit

Permalink
Issue#305: Restore terminal on close()
Browse files Browse the repository at this point in the history
Previously we had to wait for JVM to exit and the shutdown hook to trigger. This also removes the shutdown hook if the terminal is closed manually.
  • Loading branch information
mabe02 committed Apr 15, 2017
1 parent 5bd000f commit 713514b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ protected UnixLikeTTYTerminal(
this.ttyDev = ttyDev;

// Take ownership of the terminal
realAquire();
realAcquire();
}

@Override
protected void aquire() throws IOException {
protected void acquire() throws IOException {
// Hack!
}

private void realAquire() throws IOException {
super.aquire();
private void realAcquire() throws IOException {
super.acquire();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,29 @@ public enum CtrlCBehaviour {

private final CtrlCBehaviour terminalCtrlCBehaviour;
private final boolean catchSpecialCharacters;
private final Thread shutdownHook;
private boolean acquired;

protected UnixLikeTerminal(InputStream terminalInput,
OutputStream terminalOutput,
Charset terminalCharset,
CtrlCBehaviour terminalCtrlCBehaviour) throws IOException {

super(terminalInput, terminalOutput, terminalCharset);
this.acquired = false;

String catchSpecialCharactersPropValue = System.getProperty(
"com.googlecode.lanterna.terminal.UnixTerminal.catchSpecialCharacters",
"");
this.catchSpecialCharacters = !"false".equals(catchSpecialCharactersPropValue.trim().toLowerCase());
this.terminalCtrlCBehaviour = terminalCtrlCBehaviour;
aquire();
shutdownHook = new Thread("Lanterna STTY restore") {
@Override
public void run() {
exitPrivateModeAndRestoreState();
}
};
acquire();
}

/**
Expand All @@ -70,7 +79,7 @@ protected UnixLikeTerminal(InputStream terminalInput,
* of any end-user class extending from {@link UnixLikeTerminal}
* @throws IOException If there was an I/O error
*/
protected void aquire() throws IOException {
protected void acquire() throws IOException {
//Make sure to set an initial size
onResized(80, 24);

Expand All @@ -92,9 +101,17 @@ public void run() {
}
}
});
setupShutdownHook();
Runtime.getRuntime().addShutdownHook(shutdownHook);
acquired = true;
}

@Override
public void close() throws IOException {
exitPrivateModeAndRestoreState();
Runtime.getRuntime().removeShutdownHook(shutdownHook);
acquired = false;
super.close();
}

@Override
public KeyStroke pollInput() throws IOException {
Expand Down Expand Up @@ -188,24 +205,21 @@ private void isCtrlC(KeyStroke key) throws IOException {
}
}

private void setupShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread("Lanterna STTY restore") {
@Override
public void run() {
try {
if (isInPrivateMode()) {
exitPrivateMode();
}
}
catch(IOException ignored) {}
catch(IllegalStateException ignored) {} // still possible!

try {
restoreTerminalSettingsAndKeyStrokeSignals();
}
catch(IOException ignored) {}
private void exitPrivateModeAndRestoreState() {
if(!acquired) {
return;
}
try {
if (isInPrivateMode()) {
exitPrivateMode();
}
});
}
}
catch(IOException ignored) {}
catch(IllegalStateException ignored) {} // still possible!

try {
restoreTerminalSettingsAndKeyStrokeSignals();
}
catch(IOException ignored) {}
}
}
54 changes: 54 additions & 0 deletions src/test/java/com/googlecode/lanterna/issue/Issue305.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.googlecode.lanterna.issue;

import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Created by martin on 2017-04-15.
*/
public class Issue305 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, InterruptedException{
Terminal terminal = new DefaultTerminalFactory().createTerminal();
System.out.println("Class: " + terminal.getClass());

terminal.close();

BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));

while (true) {

System.out.print("Enter something: ");
String input = br.readLine();

if ("q".equals(input)) {
System.out.println("Exit!");
System.exit(0);
}

System.out.println("input : " + input);
System.out.println("-----------\n");
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
}

0 comments on commit 713514b

Please sign in to comment.