- 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

@@ -6,6 +6,7 @@ Provides Java bindings for various native APIs.
* Determine if stdout/stderr are attached to a terminal. * Determine if stdout/stderr are attached to a terminal.
* Query the terminal size. * Query the terminal size.
* Switch between bold and normal mode on the terminal. * Switch between bold and normal mode on the terminal.
* Change foreground color on the terminal.
Currently only ported to OS X (10.7.4) and Linux (Ubuntu 12.04). Currently only ported to OS X (10.7.4) and Linux (Ubuntu 12.04).

View File

@@ -8,10 +8,24 @@
#include <curses.h> #include <curses.h>
#include <term.h> #include <term.h>
void markFailed(JNIEnv *env, jobject result) { /*
* Marks the given result as failed, using the current value of errno
*/
void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) {
jclass destClass = env->GetObjectClass(result); jclass destClass = env->GetObjectClass(result);
jmethodID method = env->GetMethodID(destClass, "failed", "(I)V"); jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;I)V");
env->CallVoidMethod(result, method, errno); jstring message_str = env->NewStringUTF(message);
env->CallVoidMethod(result, method, message_str, errno);
}
/*
* Marks the given result as failed, using the given error message
*/
void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) {
jclass destClass = env->GetObjectClass(result);
jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;)V");
jstring message_str = env->NewStringUTF(message);
env->CallVoidMethod(result, method, message_str);
} }
/* /*
@@ -20,7 +34,7 @@ void markFailed(JNIEnv *env, jobject result) {
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) { Java_net_rubygrapefruit_platform_internal_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
return 1; return 2;
} }
/* /*
@@ -31,8 +45,9 @@ JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_PosixFileFunctions_chmod(JNIEnv *env, jclass target, jstring path, jint mode, jobject result) { Java_net_rubygrapefruit_platform_internal_PosixFileFunctions_chmod(JNIEnv *env, jclass target, jstring path, jint mode, jobject result) {
const char* pathUtf8 = env->GetStringUTFChars(path, NULL); const char* pathUtf8 = env->GetStringUTFChars(path, NULL);
int retval = chmod(pathUtf8, mode); int retval = chmod(pathUtf8, mode);
env->ReleaseStringUTFChars(path, pathUtf8);
if (retval != 0) { if (retval != 0) {
markFailed(env, result); mark_failed_with_errno(env, "could not chmod file", result);
} }
} }
@@ -41,8 +56,9 @@ Java_net_rubygrapefruit_platform_internal_PosixFileFunctions_stat(JNIEnv *env, j
struct stat fileInfo; struct stat fileInfo;
const char* pathUtf8 = env->GetStringUTFChars(path, NULL); const char* pathUtf8 = env->GetStringUTFChars(path, NULL);
int retval = stat(pathUtf8, &fileInfo); int retval = stat(pathUtf8, &fileInfo);
env->ReleaseStringUTFChars(path, pathUtf8);
if (retval != 0) { if (retval != 0) {
markFailed(env, result); mark_failed_with_errno(env, "could not stat file", result);
return; return;
} }
jclass destClass = env->GetObjectClass(dest); jclass destClass = env->GetObjectClass(dest);
@@ -79,7 +95,7 @@ Java_net_rubygrapefruit_platform_internal_PosixTerminalFunctions_getTerminalSize
struct winsize screen_size; struct winsize screen_size;
int retval = ioctl(output+1, TIOCGWINSZ, &screen_size); int retval = ioctl(output+1, TIOCGWINSZ, &screen_size);
if (retval != 0) { if (retval != 0) {
markFailed(env, result); mark_failed_with_errno(env, "could not fetch terminal size", result);
return; return;
} }
jclass dimensionClass = env->GetObjectClass(dimension); jclass dimensionClass = env->GetObjectClass(dimension);
@@ -102,25 +118,29 @@ int write_to_terminal(int ch) {
void write_capability(JNIEnv *env, const char* capability, jobject result) { void write_capability(JNIEnv *env, const char* capability, jobject result) {
char* cap = tgetstr((char*)capability, NULL); char* cap = tgetstr((char*)capability, NULL);
if (cap == NULL) { if (cap == NULL) {
markFailed(env, result); mark_failed_with_message(env, "unknown terminal capability", result);
return; return;
} }
if (tputs(cap, 1, write_to_terminal) == ERR) { if (tputs(cap, 1, write_to_terminal) == ERR) {
markFailed(env, result); mark_failed_with_message(env, "could not write to terminal", result);
return; return;
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_initTerminal(JNIEnv *env, jclass target, jint output, jobject result) { Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_initTerminal(JNIEnv *env, jclass target, jint output, jobject result) {
if (!isatty(output+1)) {
mark_failed_with_message(env, "not a terminal", result);
return;
}
char* termType = getenv("TERM"); char* termType = getenv("TERM");
if (termType == NULL) { if (termType == NULL) {
markFailed(env, result); mark_failed_with_message(env, "$TERM not set", result);
return; return;
} }
int retval = tgetent(NULL, termType); int retval = tgetent(NULL, termType);
if (retval != 1) { if (retval != 1) {
markFailed(env, result); mark_failed_with_message(env, "could not get termcap entry", result);
return; return;
} }
current_terminal = output + 1; current_terminal = output + 1;
@@ -128,11 +148,31 @@ Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_initTerminal(JNIEnv
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_bold(JNIEnv *env, jclass target, jint output, jobject result) { Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_bold(JNIEnv *env, jclass target, jobject result) {
write_capability(env, "md", result); write_capability(env, "md", result);
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_normal(JNIEnv *env, jclass target, jint output, jobject result) { Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_reset(JNIEnv *env, jclass target, jobject result) {
write_capability(env, "me", result); write_capability(env, "me", result);
} }
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_TerminfoFunctions_foreground(JNIEnv *env, jclass target, jint color, jobject result) {
char* capability = tgetstr((char*)"AF", NULL);
if (capability == NULL) {
mark_failed_with_message(env, "unknown terminal capability", result);
return;
}
capability = tparm(capability, color, 0, 0, 0, 0, 0, 0, 0, 0);
if (capability == NULL) {
mark_failed_with_message(env, "could not format terminal capability string", result);
return;
}
if (tputs(capability, 1, write_to_terminal) == ERR) {
mark_failed_with_message(env, "could not write to terminal", result);
return;
}
}

View File

@@ -15,11 +15,24 @@ public class Main {
Terminal terminal = terminalAccess.getTerminal(TerminalAccess.Output.Stdout); Terminal terminal = terminalAccess.getTerminal(TerminalAccess.Output.Stdout);
TerminalSize terminalSize = terminal.getTerminalSize(); TerminalSize terminalSize = terminal.getTerminalSize();
System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows"); System.out.println("* terminal size: " + terminalSize.getCols() + " cols x " + terminalSize.getRows() + " rows");
System.out.println();
System.out.println("TERMINAL OUTPUT");
System.out.print("[normal] "); System.out.print("[normal] ");
terminal.bold(); terminal.bold();
System.out.print("[bold]"); System.out.print("[bold]");
terminal.normal(); terminal.normal();
System.out.println(" [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(); FunctionResult result = new FunctionResult();
PosixFileFunctions.chmod(file.getPath(), perms, result); PosixFileFunctions.chmod(file.getPath(), perms, result);
if (result.isFailed()) { 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(); FileStat stat = new FileStat();
PosixFileFunctions.stat(file.getPath(), stat, result); PosixFileFunctions.stat(file.getPath(), stat, result);
if (result.isFailed()) { 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; return stat.mode;
} }
@@ -100,6 +100,7 @@ public class Platform {
private static class DefaultTerminal implements Terminal { private static class DefaultTerminal implements Terminal {
private final TerminalAccess.Output output; private final TerminalAccess.Output output;
private final PrintStream stream; private final PrintStream stream;
private Color foreground;
public DefaultTerminal(TerminalAccess.Output output) { public DefaultTerminal(TerminalAccess.Output output) {
this.output = output; this.output = output;
@@ -111,9 +112,14 @@ public class Platform {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.initTerminal(output.ordinal(), result); TerminfoFunctions.initTerminal(output.ordinal(), result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not open terminal. Errno is %d.", throw new NativeException(String.format("Could not open terminal: %s", result.getMessage()));
result.getErrno()));
} }
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
reset();
}
});
} }
@Override @Override
@@ -122,40 +128,50 @@ public class Platform {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
PosixTerminalFunctions.getTerminalSize(output.ordinal(), terminalSize, result); PosixTerminalFunctions.getTerminalSize(output.ordinal(), terminalSize, result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not get terminal size. Errno is %d.", throw new NativeException(String.format("Could not get terminal size: %s", result.getMessage()));
result.getErrno()));
} }
return terminalSize; 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 @Override
public Terminal bold() { public Terminal bold() {
stream.flush(); stream.flush();
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.bold(output.ordinal(), result); TerminfoFunctions.bold(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch to bold mode. Errno is %d.", throw new NativeException(String.format("Could not switch to bold mode: %s", result.getMessage()));
result.getErrno()));
} }
return this; return this;
} }
@Override
public Terminal bold(String output) {
bold();
stream.print(output);
normal();
return this;
}
@Override @Override
public Terminal normal() { public Terminal normal() {
reset();
if (foreground != null) {
foreground(foreground);
}
return this;
}
@Override
public Terminal reset() {
stream.flush(); stream.flush();
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.normal(output.ordinal(), result); TerminfoFunctions.reset(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch to normal mode. Errno is %d.", throw new NativeException(String.format("Could not reset terminal: %s", result.getMessage()));
result.getErrno()));
} }
return this; return this;
} }

View File

@@ -1,20 +1,32 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
public interface Terminal { public interface Terminal {
enum Color {
Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
}
/**
* Returns the size of the terminal.
*/
TerminalSize getTerminalSize(); TerminalSize getTerminalSize();
/**
* Sets the terminal foreground color.
*/
Terminal foreground(Color color);
/** /**
* Switches the terminal to bold mode. * Switches the terminal to bold mode.
*/ */
Terminal bold(); 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. * Switches the terminal to normal mode.
*/ */
Terminal normal(); 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; package net.rubygrapefruit.platform.internal;
public class FunctionResult { public class FunctionResult {
String message;
int errno; int errno;
void failed(int errno) { void failed(String message, int errno) {
this.message = message;
this.errno = errno; this.errno = errno;
} }
public boolean isFailed() { void failed(String message) {
return errno != 0; this.message = message;
} }
public int getErrno() { public boolean isFailed() {
return errno; 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; package net.rubygrapefruit.platform.internal;
public class NativeLibraryFunctions { public class NativeLibraryFunctions {
public static final int VERSION = 1; public static final int VERSION = 2;
public static native int getVersion(); 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 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);
} }

View File

@@ -36,7 +36,7 @@ class PosixFileTest extends Specification {
then: then:
NativeException e = thrown() NativeException e = thrown()
e.message == "Could not set UNIX mode on $file. Errno is 2." e.message == "Could not set UNIX mode on $file: could not chmod file (errno 2)"
} }
def "throws exception on failure to get mode"() { def "throws exception on failure to get mode"() {
@@ -47,6 +47,6 @@ class PosixFileTest extends Specification {
then: then:
NativeException e = thrown() NativeException e = thrown()
e.message == "Could not get UNIX mode on $file. Errno is 2." e.message == "Could not get UNIX mode on $file: could not stat file (errno 2)"
} }
} }

View File

@@ -20,6 +20,6 @@ class TerminalTest extends Specification {
then: then:
NativeException e = thrown() NativeException e = thrown()
e.message.startsWith('Could not open terminal. Errno is ') e.message == 'Could not open terminal: not a terminal'
} }
} }