Added support for Terminal.up(), down(), left(), right() for the terminal.

This commit is contained in:
Adam Murdoch
2012-08-04 16:18:30 +10:00
parent 7ee843612a
commit ec9d8d7bf8
12 changed files with 686 additions and 467 deletions

View File

@@ -1,33 +1,48 @@
Provides Java bindings for various native APIs.
Provides Java bindings for various native APIs.
* Get and set UNIX file mode.
* Get PID of current process. # Available bindings
* Determine if stdout/stderr are attached to a terminal.
* Query the terminal size. ## Generic
* Switch between bold and normal mode on the terminal.
* Change foreground color on the terminal. * Get and set UNIX file mode.
* Get PID of current process.
Currently ported to OS X, Linux and Windows. Tested on:
## Terminal and console
* OS X 10.7.4
* Ubunutu 12.04 (amd64) These bindings work for both the UNIX terminal and Windows console:
* Windows 7 (amd64)
* Determine if stdout/stderr are attached to a terminal.
## Building * Query the terminal size.
* Switch between bold and normal mode on the terminal.
### Ubuntu * Change foreground color on the terminal.
* Move terminal cursor up, down, left, right.
You need to install the `libncurses5-dev` package to pick up the ncurses header files. Also worth installing the `ncurses-doc` package too.
Currently ported to OS X, Linux and Windows. Tested on:
## TODO
* OS X 10.7.4
* Fix TERM=dumb on linux * Ubunutu 12.04 (amd64)
* Split out separate native library for terminal handling. * Windows 7 (amd64)
* String names for errno values.
* Split into multiple projects. # Building
* Handle multiple architectures.
* IBM JVM. ## Ubuntu
* Convert to c.
* Thread safety. You need to install the `libncurses5-dev` package to pick up the ncurses header files. Also worth installing the `ncurses-doc` package too.
* Windows: flush System.out or System.err on attribute change.
## Windows
You need to install Visual studio, and build from a Visual studio command prompt.
# TODO
* Fix TERM=dumb on linux
* Split out separate native library for terminal handling.
* String names for errno values.
* Split into multiple projects.
* Handle multiple architectures.
* IBM JVM.
* Convert to c.
* Thread safety.
* Windows: flush System.out or System.err on attribute change.

View File

@@ -1,21 +1,21 @@
/* /*
* Generic functions * Generic functions
*/ */
#include "native.h" #include "native.h"
#include "generic.h" #include "generic.h"
void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) { void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) {
mark_failed_with_code(env, message, 0, result); mark_failed_with_code(env, message, 0, result);
} }
void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result) { void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result) {
jclass destClass = env->GetObjectClass(result); jclass destClass = env->GetObjectClass(result);
jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;I)V"); jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;I)V");
jstring message_str = env->NewStringUTF(message); jstring message_str = env->NewStringUTF(message);
env->CallVoidMethod(result, method, message_str, error_code); env->CallVoidMethod(result, method, message_str, error_code);
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) { Java_net_rubygrapefruit_platform_internal_jni_NativeLibraryFunctions_getVersion(JNIEnv *env, jclass target) {
return 2; return 3;
} }

View File

@@ -1,161 +1,194 @@
#ifndef WIN32 #ifndef WIN32
#include "native.h" #include "native.h"
#include "generic.h" #include "generic.h"
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <curses.h> #include <curses.h>
#include <term.h> #include <term.h>
/* /*
* Marks the given result as failed, using the current value of errno * Marks the given result as failed, using the current value of errno
*/ */
void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) {
mark_failed_with_code(env, message, errno, result); mark_failed_with_code(env, message, errno, result);
} }
/* /*
* File functions * File functions
*/ */
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_chmod(JNIEnv *env, jclass target, jstring path, jint mode, jobject result) { Java_net_rubygrapefruit_platform_internal_jni_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); env->ReleaseStringUTFChars(path, pathUtf8);
if (retval != 0) { if (retval != 0) {
mark_failed_with_errno(env, "could not chmod file", result); mark_failed_with_errno(env, "could not chmod file", result);
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_stat(JNIEnv *env, jclass target, jstring path, jobject dest, jobject result) { Java_net_rubygrapefruit_platform_internal_jni_PosixFileFunctions_stat(JNIEnv *env, jclass target, jstring path, jobject dest, jobject result) {
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); env->ReleaseStringUTFChars(path, pathUtf8);
if (retval != 0) { if (retval != 0) {
mark_failed_with_errno(env, "could not stat file", result); mark_failed_with_errno(env, "could not stat file", result);
return; return;
} }
jclass destClass = env->GetObjectClass(dest); jclass destClass = env->GetObjectClass(dest);
jfieldID modeField = env->GetFieldID(destClass, "mode", "I"); jfieldID modeField = env->GetFieldID(destClass, "mode", "I");
env->SetIntField(dest, modeField, 0777 & fileInfo.st_mode); env->SetIntField(dest, modeField, 0777 & fileInfo.st_mode);
} }
/* /*
* Process functions * Process functions
*/ */
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getPid(JNIEnv *env, jclass target) { Java_net_rubygrapefruit_platform_internal_jni_PosixProcessFunctions_getPid(JNIEnv *env, jclass target) {
return getpid(); return getpid();
} }
/* /*
* Terminal functions * Terminal functions
*/ */
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixTerminalFunctions_isatty(JNIEnv *env, jclass target, jint output) { Java_net_rubygrapefruit_platform_internal_jni_PosixTerminalFunctions_isatty(JNIEnv *env, jclass target, jint output) {
switch (output) { switch (output) {
case 0: case 0:
case 1: case 1:
return isatty(output+1) ? JNI_TRUE : JNI_FALSE; return isatty(output+1) ? JNI_TRUE : JNI_FALSE;
default: default:
return JNI_FALSE; return JNI_FALSE;
} }
} }
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_PosixTerminalFunctions_getTerminalSize(JNIEnv *env, jclass target, jint output, jobject dimension, jobject result) { Java_net_rubygrapefruit_platform_internal_jni_PosixTerminalFunctions_getTerminalSize(JNIEnv *env, jclass target, jint output, jobject dimension, jobject result) {
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) {
mark_failed_with_errno(env, "could not fetch terminal size", result); mark_failed_with_errno(env, "could not fetch terminal size", result);
return; return;
} }
jclass dimensionClass = env->GetObjectClass(dimension); jclass dimensionClass = env->GetObjectClass(dimension);
jfieldID widthField = env->GetFieldID(dimensionClass, "cols", "I"); jfieldID widthField = env->GetFieldID(dimensionClass, "cols", "I");
env->SetIntField(dimension, widthField, screen_size.ws_col); env->SetIntField(dimension, widthField, screen_size.ws_col);
jfieldID heightField = env->GetFieldID(dimensionClass, "rows", "I"); jfieldID heightField = env->GetFieldID(dimensionClass, "rows", "I");
env->SetIntField(dimension, heightField, screen_size.ws_row); env->SetIntField(dimension, heightField, screen_size.ws_row);
} }
/* /*
* Terminfo functions * Terminfo functions
*/ */
int current_terminal = -1; int current_terminal = -1;
int write_to_terminal(int ch) { int write_to_terminal(int ch) {
write(current_terminal, &ch, 1); write(current_terminal, &ch, 1);
} }
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) {
mark_failed_with_message(env, "unknown terminal capability", 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) {
mark_failed_with_message(env, "could not write to terminal", result); mark_failed_with_message(env, "could not write to terminal", result);
return; return;
} }
} }
JNIEXPORT void JNICALL void write_param_capability(JNIEnv *env, const char* capability, int count, jobject result) {
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_initTerminal(JNIEnv *env, jclass target, jint output, jobject result) { char* cap = tgetstr((char*)capability, NULL);
if (!isatty(output+1)) { if (cap == NULL) {
mark_failed_with_message(env, "not a terminal", result); mark_failed_with_message(env, "unknown terminal capability", result);
return; return;
} }
char* termType = getenv("TERM");
if (termType == NULL) { cap = tparm(cap, count, 0, 0, 0, 0, 0, 0, 0, 0);
mark_failed_with_message(env, "$TERM not set", result); if (cap == NULL) {
return; mark_failed_with_message(env, "could not format terminal capability string", result);
} return;
int retval = tgetent(NULL, termType); }
if (retval != 1) {
mark_failed_with_message(env, "could not get termcap entry", result); if (tputs(cap, 1, write_to_terminal) == ERR) {
return; mark_failed_with_message(env, "could not write to terminal", result);
} return;
current_terminal = output + 1; }
write_capability(env, "me", result); }
}
JNIEXPORT void JNICALL
JNIEXPORT void JNICALL Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_initTerminal(JNIEnv *env, jclass target, jint output, jobject result) {
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_bold(JNIEnv *env, jclass target, jobject result) { if (!isatty(output+1)) {
write_capability(env, "md", result); mark_failed_with_message(env, "not a terminal", result);
} return;
}
JNIEXPORT void JNICALL char* termType = getenv("TERM");
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_reset(JNIEnv *env, jclass target, jobject result) { if (termType == NULL) {
write_capability(env, "me", result); mark_failed_with_message(env, "$TERM not set", result);
} return;
}
JNIEXPORT void JNICALL int retval = tgetent(NULL, termType);
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_foreground(JNIEnv *env, jclass target, jint color, jobject result) { if (retval != 1) {
char* capability = tgetstr((char*)"AF", NULL); mark_failed_with_message(env, "could not get termcap entry", result);
if (capability == NULL) { return;
mark_failed_with_message(env, "unknown terminal capability", result); }
return; current_terminal = output + 1;
} write_capability(env, "me", result);
}
capability = tparm(capability, color, 0, 0, 0, 0, 0, 0, 0, 0);
if (capability == NULL) { JNIEXPORT void JNICALL
mark_failed_with_message(env, "could not format terminal capability string", result); Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_bold(JNIEnv *env, jclass target, jobject result) {
return; write_capability(env, "md", result);
} }
if (tputs(capability, 1, write_to_terminal) == ERR) { JNIEXPORT void JNICALL
mark_failed_with_message(env, "could not write to terminal", result); Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_reset(JNIEnv *env, jclass target, jobject result) {
return; write_capability(env, "me", result);
} }
}
JNIEXPORT void JNICALL
#endif Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_foreground(JNIEnv *env, jclass target, jint color, jobject result) {
write_param_capability(env, "AF", color, result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_up(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, "up", result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_down(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, "do", result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_left(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, "le", result);
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_right(JNIEnv *env, jclass target, jint count, jobject result) {
for (jint i = 0; i < count; i++) {
write_capability(env, "nd", result);
}
}
#endif

View File

@@ -1,41 +1,66 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(); System.out.println();
System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch")); System.out.println("* OS: " + System.getProperty("os.name") + ' ' + System.getProperty("os.version") + ' ' + System.getProperty("os.arch"));
Process process = Native.get(Process.class); Process process = Native.get(Process.class);
System.out.println("* PID: " + process.getProcessId()); System.out.println("* PID: " + process.getProcessId());
TerminalAccess terminalAccess = Native.get(TerminalAccess.class); TerminalAccess terminalAccess = Native.get(TerminalAccess.class);
boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout); boolean stdoutIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stdout);
boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr); boolean stderrIsTerminal = terminalAccess.isTerminal(TerminalAccess.Output.Stderr);
System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal")); System.out.println("* stdout: " + (stdoutIsTerminal ? "terminal" : "not a terminal"));
System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal")); System.out.println("* stderr: " + (stderrIsTerminal ? "terminal" : "not a terminal"));
if (stdoutIsTerminal) { if (stdoutIsTerminal) {
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();
System.out.println("TERMINAL OUTPUT"); 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:"); System.out.println("here are the colors:");
for (Terminal.Color color : Terminal.Color.values()) { for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color); terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase())); System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
terminal.bold(); terminal.bold();
System.out.print(String.format("[%s]", color.toString().toLowerCase())); System.out.print(String.format("[%s]", color.toString().toLowerCase()));
terminal.normal(); terminal.normal();
System.out.println(); System.out.println();
} }
}
System.out.println();
System.out.println(); terminal.reset();
}
} System.out.println("CURSOR MOVEMENT");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.print("draw ");
terminal.cursorLeft(10);
terminal.cursorUp(1);
terminal.cursorRight(10);
System.out.print("[4]");
terminal.cursorUp(1);
terminal.cursorLeft(3);
System.out.print("[2]");
terminal.cursorLeft(13);
System.out.print("[1]");
terminal.cursorLeft(3);
terminal.cursorDown(1);
System.out.print("[3]");
terminal.cursorDown(1);
terminal.cursorRight(10);
System.out.println("done!");
}
System.out.println();
}
}

View File

@@ -4,9 +4,21 @@ import java.io.File;
/** /**
* Functions to query and modify a file's POSIX meta-data. * Functions to query and modify a file's POSIX meta-data.
*
* Supported on Linux, OS X
*/ */
public interface PosixFile extends NativeIntegration { public interface PosixFile extends NativeIntegration {
/**
* Sets the mode for the given file.
*
* @throws NativeException On failure.
*/
void setMode(File path, int perms) throws NativeException; void setMode(File path, int perms) throws NativeException;
/**
* Gets the mode for the given file.
*
* @throws NativeException On failure.
*/
int getMode(File path) throws NativeException; int getMode(File path) throws NativeException;
} }

View File

@@ -1,8 +1,15 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
/** /**
* Functions to query and modify a process' meta-data * Functions to query and modify a process' meta-data
*/ *
public interface Process extends NativeIntegration { * Supported on Linux, OS X, Windows.
int getProcessId() throws NativeException; */
} public interface Process extends NativeIntegration {
/**
* Returns the process identifier.
*
* @throws NativeException On failure.
*/
int getProcessId() throws NativeException;
}

View File

@@ -1,5 +1,10 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
/**
* Allows the terminal/console to be manipulated.
*
* Supported on Linux, OS X, Windows.
*/
public interface Terminal { public interface Terminal {
enum Color { enum Color {
Black, Red, Green, Yellow, Blue, Magenta, Cyan, White Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
@@ -7,26 +12,64 @@ public interface Terminal {
/** /**
* Returns the size of the terminal. * Returns the size of the terminal.
*
* @throws NativeException On failure.
*/ */
TerminalSize getTerminalSize(); TerminalSize getTerminalSize() throws NativeException;
/** /**
* Sets the terminal foreground color. * Sets the terminal foreground color.
*
* @throws NativeException On failure.
*/ */
Terminal foreground(Color color); Terminal foreground(Color color) throws NativeException;
/** /**
* Switches the terminal to bold mode. * Switches the terminal to bold mode.
*
* @throws NativeException On failure.
*/ */
Terminal bold(); Terminal bold() throws NativeException;
/** /**
* Switches the terminal to normal mode. * Switches the terminal to normal mode.
*
* @throws NativeException On failure.
*/ */
Terminal normal(); Terminal normal() throws NativeException;
/** /**
* Switches the terminal to normal mode and restores default colors. * Switches the terminal to normal mode and restores default colors.
*
* @throws NativeException On failure.
*/ */
Terminal reset(); Terminal reset() throws NativeException;
/**
* Moves the cursor the given number of characters to the left.
*
* @throws NativeException On failure.
*/
Terminal cursorLeft(int count) throws NativeException;
/**
* Moves the cursor the given number of characters to the right.
*
* @throws NativeException On failure.
*/
Terminal cursorRight(int count) throws NativeException;
/**
* Moves the cursor the given number of characters up.
*
* @throws NativeException On failure.
*/
Terminal cursorUp(int count) throws NativeException;
/**
* Moves the cursor the given number of characters down.
*
* @throws NativeException On failure.
*/
Terminal cursorDown(int count) throws NativeException;
} }

View File

@@ -1,9 +1,24 @@
package net.rubygrapefruit.platform; package net.rubygrapefruit.platform;
/**
* Provides access to the terminal/console.
*
* Supported on Linux, OS X, Windows.
*/
public interface TerminalAccess extends NativeIntegration { public interface TerminalAccess extends NativeIntegration {
enum Output {Stdout, Stderr} enum Output {Stdout, Stderr}
boolean isTerminal(Output output); /**
* Returns true if the given output is attached to a terminal.
*
* @throws NativeException On failure.
*/
boolean isTerminal(Output output) throws NativeException;
Terminal getTerminal(Output output); /**
* Returns the terminal attached to the given output.
*
* @throws NativeException When the output is not attached to a terminal.
*/
Terminal getTerminal(Output output) throws NativeException;
} }

View File

@@ -1,90 +1,134 @@
package net.rubygrapefruit.platform.internal; package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.NativeException; import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.Terminal; import net.rubygrapefruit.platform.Terminal;
import net.rubygrapefruit.platform.TerminalAccess; import net.rubygrapefruit.platform.TerminalAccess;
import net.rubygrapefruit.platform.TerminalSize; import net.rubygrapefruit.platform.TerminalSize;
import net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions; import net.rubygrapefruit.platform.internal.jni.PosixTerminalFunctions;
import net.rubygrapefruit.platform.internal.jni.TerminfoFunctions; import net.rubygrapefruit.platform.internal.jni.TerminfoFunctions;
import java.io.PrintStream; import java.io.PrintStream;
public class TerminfoTerminal extends AbstractTerminal { public class TerminfoTerminal extends AbstractTerminal {
private final TerminalAccess.Output output; private final TerminalAccess.Output output;
private final PrintStream stream; private final PrintStream stream;
private Color foreground; private Color foreground;
public TerminfoTerminal(TerminalAccess.Output output) { public TerminfoTerminal(TerminalAccess.Output output) {
this.output = output; this.output = output;
stream = output == TerminalAccess.Output.Stdout ? System.out : System.err; stream = output == TerminalAccess.Output.Stdout ? System.out : System.err;
} }
@Override @Override
public String toString() { public String toString() {
return output.toString().toLowerCase(); return output.toString().toLowerCase();
} }
@Override @Override
protected void doInit() { protected void doInit() {
stream.flush(); stream.flush();
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 for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not open terminal for %s: %s", this, result.getMessage()));
} }
} }
@Override @Override
public TerminalSize getTerminalSize() { public TerminalSize getTerminalSize() {
MutableTerminalSize terminalSize = new MutableTerminalSize(); MutableTerminalSize terminalSize = new MutableTerminalSize();
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 for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not get terminal size for %s: %s", this, result.getMessage()));
} }
return terminalSize; return terminalSize;
} }
@Override @Override
public Terminal foreground(Color color) { public Terminal foreground(Color color) {
stream.flush(); stream.flush();
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.foreground(color.ordinal(), result); TerminfoFunctions.foreground(color.ordinal(), result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch foreground color for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not switch foreground color for %s: %s", this, result.getMessage()));
} }
foreground = color; foreground = color;
return this; return this;
} }
@Override @Override
public Terminal bold() { public Terminal bold() {
stream.flush(); stream.flush();
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.bold(result); TerminfoFunctions.bold(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch to bold mode for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not switch to bold mode for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
@Override @Override
public Terminal normal() { public Terminal normal() {
reset(); reset();
if (foreground != null) { if (foreground != null) {
foreground(foreground); foreground(foreground);
} }
return this; return this;
} }
@Override @Override
public Terminal reset() { public Terminal reset() {
stream.flush(); stream.flush();
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
TerminfoFunctions.reset(result); TerminfoFunctions.reset(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not reset terminal for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not reset terminal for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
}
@Override
public Terminal cursorDown(int count) {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.down(count, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not move cursor down for %s: %s", this, result.getMessage()));
}
return this;
}
@Override
public Terminal cursorUp(int count) {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.up(count, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not move cursor up for %s: %s", this, result.getMessage()));
}
return this;
}
@Override
public Terminal cursorLeft(int count) {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.left(count, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not move cursor left for %s: %s", this, result.getMessage()));
}
return this;
}
@Override
public Terminal cursorRight(int count) {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.right(count, result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not move cursor right for %s: %s", this, result.getMessage()));
}
return this;
}
}

View File

@@ -1,80 +1,100 @@
package net.rubygrapefruit.platform.internal; package net.rubygrapefruit.platform.internal;
import net.rubygrapefruit.platform.NativeException; import net.rubygrapefruit.platform.NativeException;
import net.rubygrapefruit.platform.Terminal; import net.rubygrapefruit.platform.Terminal;
import net.rubygrapefruit.platform.TerminalAccess; import net.rubygrapefruit.platform.TerminalAccess;
import net.rubygrapefruit.platform.TerminalSize; import net.rubygrapefruit.platform.TerminalSize;
import net.rubygrapefruit.platform.internal.jni.WindowsConsoleFunctions; import net.rubygrapefruit.platform.internal.jni.WindowsConsoleFunctions;
public class WindowsTerminal extends AbstractTerminal { public class WindowsTerminal extends AbstractTerminal {
private final TerminalAccess.Output output; private final TerminalAccess.Output output;
public WindowsTerminal(TerminalAccess.Output output) { public WindowsTerminal(TerminalAccess.Output output) {
this.output = output; this.output = output;
} }
@Override @Override
public String toString() { public String toString() {
return output.toString().toLowerCase(); return output.toString().toLowerCase();
} }
@Override @Override
protected void doInit() { protected void doInit() {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
WindowsConsoleFunctions.initConsole(output.ordinal(), result); WindowsConsoleFunctions.initConsole(output.ordinal(), result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not open console for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not open console for %s: %s", this, result.getMessage()));
} }
} }
@Override @Override
public TerminalSize getTerminalSize() { public TerminalSize getTerminalSize() {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
MutableTerminalSize size = new MutableTerminalSize(); MutableTerminalSize size = new MutableTerminalSize();
WindowsConsoleFunctions.getConsoleSize(output.ordinal(), size, result); WindowsConsoleFunctions.getConsoleSize(output.ordinal(), size, result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not determine console size for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not determine console size for %s: %s", this, result.getMessage()));
} }
return size; return size;
} }
@Override @Override
public Terminal bold() { public Terminal bold() {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
WindowsConsoleFunctions.bold(result); WindowsConsoleFunctions.bold(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch console to bold mode for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not switch console to bold mode for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
@Override @Override
public Terminal foreground(Color color) { public Terminal foreground(Color color) {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
WindowsConsoleFunctions.foreground(color.ordinal(), result); WindowsConsoleFunctions.foreground(color.ordinal(), result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not change console foreground color for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not change console foreground color for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
@Override @Override
public Terminal normal() { public Terminal normal() {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
WindowsConsoleFunctions.normal(result); WindowsConsoleFunctions.normal(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not switch console to normal mode for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not switch console to normal mode for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
@Override @Override
public Terminal reset() { public Terminal reset() {
FunctionResult result = new FunctionResult(); FunctionResult result = new FunctionResult();
WindowsConsoleFunctions.reset(result); WindowsConsoleFunctions.reset(result);
if (result.isFailed()) { if (result.isFailed()) {
throw new NativeException(String.format("Could not reset console for %s: %s", this, result.getMessage())); throw new NativeException(String.format("Could not reset console for %s: %s", this, result.getMessage()));
} }
return this; return this;
} }
}
@Override
public Terminal cursorDown(int count) throws NativeException {
throw new UnsupportedOperationException();
}
@Override
public Terminal cursorUp(int count) throws NativeException {
throw new UnsupportedOperationException();
}
@Override
public Terminal cursorLeft(int count) throws NativeException {
throw new UnsupportedOperationException();
}
@Override
public Terminal cursorRight(int count) throws NativeException {
throw new UnsupportedOperationException();
}
}

View File

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

View File

@@ -1,19 +1,24 @@
package net.rubygrapefruit.platform.internal.jni; package net.rubygrapefruit.platform.internal.jni;
import net.rubygrapefruit.platform.internal.FunctionResult; import net.rubygrapefruit.platform.internal.FunctionResult;
public class TerminfoFunctions { public class TerminfoFunctions {
/** /**
* Sets up terminal info and switches output to normal mode. * Sets up terminal info and switches output to normal mode.
*/ */
public static native void initTerminal(int filedes, FunctionResult result); public static native void initTerminal(int filedes, FunctionResult result);
public static native void bold(FunctionResult result); public static native void bold(FunctionResult result);
public static native void reset(FunctionResult result); public static native void reset(FunctionResult result);
/** public static native void foreground(int ansiColor, FunctionResult result);
* Set the foreground color to the given ansi color.
*/ public static native void left(int count, FunctionResult result);
public static native void foreground(int ansiColor, FunctionResult result);
} public static native void right(int count, FunctionResult result);
public static native void up(int count, FunctionResult result);
public static native void down(int count, FunctionResult result);
}