Added support for switching the terminal to bold mode.
This commit is contained in:
@@ -15,6 +15,11 @@ public class Main {
|
||||
Terminal terminal = terminalAccess.getTerminal(TerminalAccess.Output.Stdout);
|
||||
TerminalSize terminalSize = terminal.getTerminalSize();
|
||||
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
|
||||
System.out.print("[normal] ");
|
||||
terminal.bold();
|
||||
System.out.print("[bold]");
|
||||
terminal.normal();
|
||||
System.out.println(" [normal]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import net.rubygrapefruit.platform.internal.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Provides access to the native integrations. Use {@link #get(Class)} to load a particular integration.
|
||||
@@ -25,6 +26,10 @@ public class Platform {
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
int nativeVersion = NativeLibraryFunctions.getVersion();
|
||||
if (nativeVersion != NativeLibraryFunctions.VERSION) {
|
||||
throw new NativeException(String.format("Unexpected native library version loaded. Expected %s, was %s.", nativeVersion, NativeLibraryFunctions.VERSION));
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
}
|
||||
@@ -71,6 +76,8 @@ public class Platform {
|
||||
}
|
||||
|
||||
private static class DefaultTerminalAccess implements TerminalAccess {
|
||||
private static Output currentlyOpen;
|
||||
|
||||
@Override
|
||||
public boolean isTerminal(Output output) {
|
||||
return PosixTerminalFunctions.isatty(output.ordinal());
|
||||
@@ -78,18 +85,35 @@ public class Platform {
|
||||
|
||||
@Override
|
||||
public Terminal getTerminal(Output output) {
|
||||
if (!isTerminal(output)) {
|
||||
throw new NativeException(String.format("%s is not attached to a terminal.", output));
|
||||
if (currentlyOpen != null) {
|
||||
throw new UnsupportedOperationException("Currently only one output can be used as a terminal.");
|
||||
}
|
||||
return new DefaultTerminal(output);
|
||||
|
||||
DefaultTerminal terminal = new DefaultTerminal(output);
|
||||
terminal.init();
|
||||
|
||||
currentlyOpen = output;
|
||||
return terminal;
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultTerminal implements Terminal {
|
||||
private final TerminalAccess.Output output;
|
||||
private final PrintStream stream;
|
||||
|
||||
public DefaultTerminal(TerminalAccess.Output output) {
|
||||
this.output = output;
|
||||
stream = output == TerminalAccess.Output.Stdout ? System.out : System.err;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.initTerminal(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not open terminal. Errno is %d.",
|
||||
result.getErrno()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -103,5 +127,37 @@ public class Platform {
|
||||
}
|
||||
return terminalSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal bold() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.bold(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch to bold mode. Errno is %d.",
|
||||
result.getErrno()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal bold(String output) {
|
||||
bold();
|
||||
stream.print(output);
|
||||
normal();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Terminal normal() {
|
||||
stream.flush();
|
||||
FunctionResult result = new FunctionResult();
|
||||
TerminfoFunctions.normal(output.ordinal(), result);
|
||||
if (result.isFailed()) {
|
||||
throw new NativeException(String.format("Could not switch to normal mode. Errno is %d.",
|
||||
result.getErrno()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,19 @@ package net.rubygrapefruit.platform;
|
||||
|
||||
public interface Terminal {
|
||||
TerminalSize getTerminalSize();
|
||||
|
||||
/**
|
||||
* Switches the terminal to bold mode.
|
||||
*/
|
||||
Terminal bold();
|
||||
|
||||
/**
|
||||
* Switches the terminal to bold mode, outputs the given text, then switches to normal mode.
|
||||
*/
|
||||
Terminal bold(String output);
|
||||
|
||||
/**
|
||||
* Switches the terminal to normal mode.
|
||||
*/
|
||||
Terminal normal();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
public class NativeLibraryFunctions {
|
||||
public static final int VERSION = 1;
|
||||
|
||||
public static native int getVersion();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.rubygrapefruit.platform.internal;
|
||||
|
||||
public class TerminfoFunctions {
|
||||
/**
|
||||
* Sets up terminal info and switches output to normal mode.
|
||||
*/
|
||||
public static native void initTerminal(int filedes, FunctionResult result);
|
||||
|
||||
public static native void bold(int filedes, FunctionResult result);
|
||||
|
||||
public static native void normal(int filedes, FunctionResult result);
|
||||
}
|
||||
Reference in New Issue
Block a user