Fixed thread safety for Terminal implementations. Changed Terminal implementation on windows to Flush System.out/System.err
This commit is contained in:
@@ -3,15 +3,5 @@ package net.rubygrapefruit.platform.internal;
|
||||
import net.rubygrapefruit.platform.Terminal;
|
||||
|
||||
public abstract class AbstractTerminal implements Terminal {
|
||||
public final void init() {
|
||||
doInit();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract void doInit();
|
||||
protected abstract void init();
|
||||
}
|
||||
|
||||
@@ -14,8 +14,14 @@ public abstract class AbstractTerminals implements Terminals {
|
||||
}
|
||||
|
||||
if (current == null) {
|
||||
AbstractTerminal terminal = createTerminal(output);
|
||||
final AbstractTerminal terminal = createTerminal(output);
|
||||
terminal.init();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
terminal.reset();
|
||||
}
|
||||
});
|
||||
currentlyOpen = output;
|
||||
current = terminal;
|
||||
}
|
||||
|
||||
@@ -2,22 +2,18 @@ package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.NativeException;
|
||||
import net.rubygrapefruit.platform.Terminal;
|
||||
import net.rubygrapefruit.platform.Terminals;
|
||||
import net.rubygrapefruit.platform.TerminalSize;
|
||||
import net.rubygrapefruit.platform.Terminals;
|
||||
import net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions;
|
||||
import net.rubygrapefruit.platform.internal.jni.TerminfoFunctions;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class TerminfoTerminal extends AbstractTerminal {
|
||||
private final Terminals.Output output;
|
||||
private final PrintStream stream;
|
||||
private final TerminalCapabilities capabilities = new TerminalCapabilities();
|
||||
private Color foreground;
|
||||
|
||||
public TerminfoTerminal(Terminals.Output output) {
|
||||
this.output = output;
|
||||
stream = output == Terminals.Output.Stdout ? System.out : System.err;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,8 +22,7 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInit() {
|
||||
stream.flush();
|
||||
protected void init() {
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.initTerminal(output.ordinal(), capabilities, result);
|
||||
if (result.isFailed()) {
|
||||
@@ -62,7 +57,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
return this;
|
||||
}
|
||||
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.foreground(color.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
@@ -78,7 +72,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
return this;
|
||||
}
|
||||
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.bold(result);
|
||||
if (result.isFailed()) {
|
||||
@@ -97,7 +90,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal reset() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.reset(result);
|
||||
if (result.isFailed()) {
|
||||
@@ -107,7 +99,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal cursorDown(int count) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.down(count, result);
|
||||
if (result.isFailed()) {
|
||||
@@ -117,7 +108,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal cursorUp(int count) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.up(count, result);
|
||||
if (result.isFailed()) {
|
||||
@@ -127,7 +117,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal cursorLeft(int count) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.left(count, result);
|
||||
if (result.isFailed()) {
|
||||
@@ -137,7 +126,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal cursorRight(int count) {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.right(count, result);
|
||||
if (result.isFailed()) {
|
||||
@@ -147,7 +135,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal cursorStartOfLine() throws NativeException {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.startLine(result);
|
||||
if (result.isFailed()) {
|
||||
@@ -157,7 +144,6 @@ public class TerminfoTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
public Terminal clearToEndOfLine() throws NativeException {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.clearToEndOfLine(result);
|
||||
if (result.isFailed()) {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.Terminals;
|
||||
import net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class TerminfoTerminals extends AbstractTerminals {
|
||||
public boolean isTerminal(Output output) {
|
||||
return PosixTerminalFunctions.isatty(output.ordinal());
|
||||
@@ -9,6 +12,7 @@ public class TerminfoTerminals extends AbstractTerminals {
|
||||
|
||||
@Override
|
||||
protected AbstractTerminal createTerminal(Output output) {
|
||||
return new TerminfoTerminal(output);
|
||||
PrintStream stream = output == Terminals.Output.Stdout ? System.out : System.err;
|
||||
return new WrapperTerminal(stream, new TerminfoTerminal(output));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class WindowsTerminal extends AbstractTerminal {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doInit() {
|
||||
protected void init() {
|
||||
FunctionResult result = new FunctionResult();
|
||||
WindowsConsoleFunctions.initConsole(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.NativeException;
|
||||
import net.rubygrapefruit.platform.Terminals;
|
||||
import net.rubygrapefruit.platform.internal.jni.WindowsConsoleFunctions;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public class WindowsTerminals extends AbstractTerminals {
|
||||
public boolean isTerminal(Output output) {
|
||||
FunctionResult result = new FunctionResult();
|
||||
@@ -16,6 +19,7 @@ public class WindowsTerminals extends AbstractTerminals {
|
||||
|
||||
@Override
|
||||
protected AbstractTerminal createTerminal(Output output) {
|
||||
return new WindowsTerminal(output);
|
||||
PrintStream stream = output == Terminals.Output.Stdout ? System.out : System.err;
|
||||
return new WrapperTerminal(stream, new WindowsTerminal(output));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
import net.rubygrapefruit.platform.NativeException;
|
||||
import net.rubygrapefruit.platform.Terminal;
|
||||
import net.rubygrapefruit.platform.TerminalSize;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* A {@link Terminal} implementation that wraps another to add thread safety.
|
||||
*/
|
||||
public class WrapperTerminal extends AbstractTerminal {
|
||||
private final AbstractTerminal terminal;
|
||||
private final PrintStream stream;
|
||||
private final Object lock = new Object();
|
||||
|
||||
public WrapperTerminal(PrintStream stream, AbstractTerminal terminal) {
|
||||
this.stream = stream;
|
||||
this.terminal = terminal;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
stream.flush();
|
||||
terminal.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TerminalSize getTerminalSize() throws NativeException {
|
||||
return terminal.getTerminalSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsColor() {
|
||||
return terminal.supportsColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsCursorMotion() {
|
||||
return terminal.supportsCursorMotion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsTextAttributes() {
|
||||
return terminal.supportsTextAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal normal() throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.normal();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal bold() throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.bold();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal reset() throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.reset();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal foreground(Color color) throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.foreground(color);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal cursorLeft(int count) throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.cursorLeft(count);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal cursorRight(int count) throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.cursorRight(count);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal cursorUp(int count) throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.cursorUp(count);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal cursorDown(int count) throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.cursorDown(count);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal cursorStartOfLine() throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.cursorStartOfLine();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal clearToEndOfLine() throws NativeException {
|
||||
stream.flush();
|
||||
synchronized (lock) {
|
||||
terminal.clearToEndOfLine();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user