- Added support for setting the terminal foreground color.

- Some improvements to error handling.
This commit is contained in:
Adam Murdoch
2012-08-04 10:00:32 +10:00
parent 31e9d2b41c
commit 335065887e
10 changed files with 145 additions and 49 deletions

View File

@@ -15,11 +15,24 @@ 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.println();
System.out.println("TERMINAL OUTPUT");
System.out.print("[normal] ");
terminal.bold();
System.out.print("[bold]");
terminal.normal();
System.out.println(" [normal]");
System.out.println("here are the colors:");
for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
terminal.bold();
System.out.print(String.format("[%s]", color.toString().toLowerCase()));
terminal.normal();
System.out.println();
}
System.out.println();
}
}
}

View File

@@ -52,7 +52,7 @@ public class Platform {
FunctionResult result = new FunctionResult();
PosixFileFunctions.chmod(file.getPath(), perms, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not set UNIX mode on %s. Errno is %d.", file, result.getErrno()));
throw new NativeException(String.format("Could not set UNIX mode on %s: %s", file, result.getMessage()));
}
}
@@ -62,7 +62,7 @@ public class Platform {
FileStat stat = new FileStat();
PosixFileFunctions.stat(file.getPath(), stat, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not get UNIX mode on %s. Errno is %d.", file, result.getErrno()));
throw new NativeException(String.format("Could not get UNIX mode on %s: %s", file, result.getMessage()));
}
return stat.mode;
}
@@ -100,6 +100,7 @@ public class Platform {
private static class DefaultTerminal implements Terminal {
private final TerminalAccess.Output output;
private final PrintStream stream;
private Color foreground;
public DefaultTerminal(TerminalAccess.Output output) {
this.output = output;
@@ -111,9 +112,14 @@ public class Platform {
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()));
throw new NativeException(String.format("Could not open terminal: %s", result.getMessage()));
}
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
reset();
}
});
}
@Override
@@ -122,40 +128,50 @@ public class Platform {
FunctionResult result = new FunctionResult();
PosixTerminalFunctions.getTerminalSize(output.ordinal(), terminalSize, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not get terminal size. Errno is %d.",
result.getErrno()));
throw new NativeException(String.format("Could not get terminal size: %s", result.getMessage()));
}
return terminalSize;
}
@Override
public Terminal foreground(Color color) {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.foreground(color.ordinal(), result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not switch foreground color: %s", result.getMessage()));
}
foreground = color;
return this;
}
@Override
public Terminal bold() {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.bold(output.ordinal(), result);
TerminfoFunctions.bold(result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not switch to bold mode. Errno is %d.",
result.getErrno()));
throw new NativeException(String.format("Could not switch to bold mode: %s", result.getMessage()));
}
return this;
}
@Override
public Terminal bold(String output) {
bold();
stream.print(output);
normal();
return this;
}
@Override
public Terminal normal() {
reset();
if (foreground != null) {
foreground(foreground);
}
return this;
}
@Override
public Terminal reset() {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.normal(output.ordinal(), result);
TerminfoFunctions.reset(result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not switch to normal mode. Errno is %d.",
result.getErrno()));
throw new NativeException(String.format("Could not reset terminal: %s", result.getMessage()));
}
return this;
}

View File

@@ -1,20 +1,32 @@
package net.rubygrapefruit.platform;
public interface Terminal {
enum Color {
Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
}
/**
* Returns the size of the terminal.
*/
TerminalSize getTerminalSize();
/**
* Sets the terminal foreground color.
*/
Terminal foreground(Color color);
/**
* 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();
/**
* Switches the terminal to normal mode and restores default colors.
*/
Terminal reset();
}

View File

@@ -1,17 +1,26 @@
package net.rubygrapefruit.platform.internal;
public class FunctionResult {
String message;
int errno;
void failed(int errno) {
void failed(String message, int errno) {
this.message = message;
this.errno = errno;
}
public boolean isFailed() {
return errno != 0;
void failed(String message) {
this.message = message;
}
public int getErrno() {
return errno;
public boolean isFailed() {
return message != null;
}
public String getMessage() {
if (errno != 0) {
return String.format("%s (errno %d)", message, errno);
}
return message;
}
}

View File

@@ -1,7 +1,7 @@
package net.rubygrapefruit.platform.internal;
public class NativeLibraryFunctions {
public static final int VERSION = 1;
public static final int VERSION = 2;
public static native int getVersion();
}

View File

@@ -6,7 +6,12 @@ public class TerminfoFunctions {
*/
public static native void initTerminal(int filedes, FunctionResult result);
public static native void bold(int filedes, FunctionResult result);
public static native void bold(FunctionResult result);
public static native void normal(int filedes, FunctionResult result);
public static native void reset(FunctionResult result);
/**
* Set the foreground color to the given ansi color.
*/
public static native void foreground(int ansiColor, FunctionResult result);
}