Added support for Terminal.cursorStartOfLine() and clearToEndOfLine().

This commit is contained in:
Adam Murdoch
2012-08-04 17:18:16 +10:00
parent ec9d8d7bf8
commit e0c31aa176
7 changed files with 69 additions and 6 deletions

View File

@@ -17,7 +17,8 @@ These bindings work for both the UNIX terminal and Windows console:
* Query the terminal size.
* Switch between bold and normal mode on the terminal.
* Change foreground color on the terminal.
* Move terminal cursor up, down, left, right.
* Move terminal cursor up, down, left, right, start of line.
* Clear to end of line.
Currently ported to OS X, Linux and Windows. Tested on:
@@ -37,7 +38,7 @@ You need to install Visual studio, and build from a Visual studio command prompt
# TODO
* Fix TERM=dumb on linux
* Fix TERM=dumb
* Split out separate native library for terminal handling.
* String names for errno values.
* Split into multiple projects.

View File

@@ -190,5 +190,15 @@ Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_right(JNIEnv *en
}
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_startLine(JNIEnv *env, jclass target, jobject result) {
write_capability(env, "cr", result);
}
JNIEXPORT void JNICALL
Java_net_rubygrapefruit_platform_internal_jni_TerminfoFunctions_clearToEndOfLine(JNIEnv *env, jclass target, jobject result) {
write_capability(env, "ce", result);
}
#endif

View File

@@ -25,7 +25,8 @@ public class Main {
terminal.normal();
System.out.println(" [normal]");
System.out.println("here are the colors:");
System.out.println();
System.out.println("COLORS");
for (Terminal.Color color : Terminal.Color.values()) {
terminal.foreground(color);
System.out.print(String.format("[%s] ", color.toString().toLowerCase()));
@@ -42,9 +43,9 @@ public class Main {
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.print("draw ");
System.out.print("[delete me]");
terminal.cursorLeft(10);
terminal.cursorLeft(11);
terminal.cursorUp(1);
terminal.cursorRight(10);
System.out.print("[4]");
@@ -57,7 +58,8 @@ public class Main {
terminal.cursorDown(1);
System.out.print("[3]");
terminal.cursorDown(1);
terminal.cursorRight(10);
terminal.cursorStartOfLine();
terminal.clearToEndOfLine();
System.out.println("done!");
}

View File

@@ -72,4 +72,18 @@ public interface Terminal {
* @throws NativeException On failure.
*/
Terminal cursorDown(int count) throws NativeException;
/**
* Moves the cursor to the start of the current line.
*
* @throws NativeException On failure.
*/
Terminal cursorStartOfLine() throws NativeException;
/**
* Clears characters from the cursor position to the end of the current line.
*
* @throws NativeException On failure.
*/
Terminal clearToEndOfLine() throws NativeException;
}

View File

@@ -131,4 +131,26 @@ public class TerminfoTerminal extends AbstractTerminal {
}
return this;
}
@Override
public Terminal cursorStartOfLine() throws NativeException {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.startLine(result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not move cursor to start of line for %s: %s", this, result.getMessage()));
}
return this;
}
@Override
public Terminal clearToEndOfLine() throws NativeException {
stream.flush();
FunctionResult result = new FunctionResult();
TerminfoFunctions.clearToEndOfLine(result);
if (result.isFailed()) {
throw new NativeException(String.format("Could not clear to end of line for %s: %s", this, result.getMessage()));
}
return this;
}
}

View File

@@ -97,4 +97,14 @@ public class WindowsTerminal extends AbstractTerminal {
public Terminal cursorRight(int count) throws NativeException {
throw new UnsupportedOperationException();
}
@Override
public Terminal cursorStartOfLine() throws NativeException {
throw new UnsupportedOperationException();
}
@Override
public Terminal clearToEndOfLine() throws NativeException {
throw new UnsupportedOperationException();
}
}

View File

@@ -21,4 +21,8 @@ public class TerminfoFunctions {
public static native void up(int count, FunctionResult result);
public static native void down(int count, FunctionResult result);
public static native void startLine(FunctionResult result);
public static native void clearToEndOfLine(FunctionResult result);
}