Skip to content

Commit

Permalink
Refactor Curses#tput methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 26, 2018
1 parent 81c428c commit eee2e70
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 91 deletions.
12 changes: 1 addition & 11 deletions reader/src/main/java/org/jline/keymap/KeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,7 @@ public static String ctrl(char key) {
}

public static String key(Terminal terminal, Capability capability) {
try {
String str = terminal.getStringCapability(capability);
if (str != null) {
StringWriter sw = new StringWriter();
Curses.tputs(sw, str);
return sw.toString();
}
} catch (IOException e) {
// Ignore
}
return null;
return Curses.tputs(terminal.getStringCapability(capability));
}

public static final Comparator<String> KEYSEQ_COMPARATOR = (s1, s2) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,7 @@ protected boolean freshLine() {
// a clr_eol first if possible.
String el = terminal.getStringCapability(Capability.clr_eol);
if (el != null) {
StringWriter sw = new StringWriter();
try {
Curses.tputs(sw, el);
} catch (IOException e) {
// nothing
}
sb.append(sw.toString());
Curses.tputs(sb, el);
}
for (int i = 0; i < size.getColumns() - 2; i++) {
sb.append(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ protected void assertConsoleOutputContains(String s) {
assertTrue(output.contains(s));
}

protected void assertBeeped() throws IOException {
protected void assertBeeped() {
String bellCap = terminal.getStringCapability(Capability.bell);
StringWriter sw = new StringWriter();
Curses.tputs(sw, bellCap);
assertConsoleOutputContains(sw.toString());
String s = Curses.tputs(bellCap);
assertConsoleOutputContains(s);
}

protected void assertBuffer(final String expected, final TestBuffer buffer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,8 @@ public void testBell() throws Exception {

reader.setVariable(LineReader.BELL_STYLE, "audible");
reader.beep();
String bellCap = terminal.getStringCapability(Capability.bell);
StringWriter sw = new StringWriter();
Curses.tputs(sw, bellCap);
assertEquals("out should have received bell", sw.toString(), out.toString());
String bell = Curses.tputs(terminal.getStringCapability(Capability.bell));
assertEquals("out should have received bell", bell, out.toString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ public boolean puts(Capability capability, Object... params) {
if (str == null) {
return false;
}
try {
Curses.tputs(writer(), str, params);
} catch (IOException e) {
throw new IOError(e);
}
Curses.tputs(writer(), str, params);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,30 +381,13 @@ protected String getEscapeSequence(short keyCode, int keyState) {
default:
return null;
}
return translate(escapeSequence, keyState + 1);
return Curses.tputs(escapeSequence, keyState + 1);
}

protected String getRawSequence(InfoCmp.Capability cap) {
return strings.get(cap);
}

protected String getSequence(InfoCmp.Capability cap) {
return translate(getRawSequence(cap));
}

private String translate(String str, Object... params) {
if (str != null) {
StringWriter sw = new StringWriter();
try {
Curses.tputs(sw, str, params);
} catch (IOException e) {
throw new IOError(e);
}
return sw.toString();
}
return null;
}

@Override
public boolean hasFocusSupport() {
return true;
Expand Down
60 changes: 43 additions & 17 deletions terminal/src/main/java/org/jline/utils/Curses.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
*/
package org.jline.utils;

import java.io.Flushable;
import java.io.IOError;
import java.io.IOException;
import java.io.Writer;
import java.io.StringWriter;
import java.util.Stack;

/**
Expand All @@ -30,15 +32,37 @@ public final class Curses {
private Curses() {
}

/**
* Print the given terminal capabilities
*
* @param cap the capability to output
* @param params optional parameters
*/
public static String tputs(String cap, Object... params) {
if (cap != null) {
StringWriter sw = new StringWriter();
tputs(sw, cap, params);
return sw.toString();
}
return null;
}

/**
* Print the given terminal capabilities
*
* @param out the output stream
* @param str the capability to output
* @param params optional parameters
* @throws IOException if an error occurs
*/
public static void tputs(Writer out, String str, Object... params) throws IOException {
public static void tputs(Appendable out, String str, Object... params) {
try {
doTputs(out, str, params);
} catch (Exception e) {
throw new IOError(e);
}
}

private static void doTputs(Appendable out, String str, Object... params) throws IOException {
int index = 0;
int length = str.length();
int ifte = IFTE_NONE;
Expand All @@ -56,45 +80,45 @@ public static void tputs(Writer out, String str, Object... params) throws IOExce
case 'e':
case 'E':
if (exec) {
out.write(27); // escape
out.append((char) 27); // escape
}
break;
case 'n':
out.write('\n');
out.append('\n');
break;
// case 'l':
// rawPrint('\l');
// break;
case 'r':
if (exec) {
out.write('\r');
out.append('\r');
}
break;
case 't':
if (exec) {
out.write('\t');
out.append('\t');
}
break;
case 'b':
if (exec) {
out.write('\b');
out.append('\b');
}
break;
case 'f':
if (exec) {
out.write('\f');
out.append('\f');
}
break;
case 's':
if (exec) {
out.write(' ');
out.append(' ');
}
break;
case ':':
case '^':
case '\\':
if (exec) {
out.write(ch);
out.append(ch);
}
break;
default:
Expand All @@ -105,15 +129,15 @@ public static void tputs(Writer out, String str, Object... params) throws IOExce
case '^':
ch = str.charAt(index++);
if (exec) {
out.write(ch - '@');
out.append((char)(ch - '@'));
}
break;
case '%':
ch = str.charAt(index++);
switch (ch) {
case '%':
if (exec) {
out.write('%');
out.append('%');
}
break;
case 'p':
Expand Down Expand Up @@ -316,7 +340,7 @@ public static void tputs(Writer out, String str, Object... params) throws IOExce
}
break;
case 'd':
out.write(Integer.toString(toInteger(stack.pop())));
out.append(Integer.toString(toInteger(stack.pop())));
break;
default:
throw new UnsupportedOperationException();
Expand All @@ -339,19 +363,21 @@ public static void tputs(Writer out, String str, Object... params) throws IOExce
}
index++;
try {
out.flush();
if (out instanceof Flushable) {
((Flushable) out).flush();
}
Thread.sleep(nb);
} catch (InterruptedException e) {
}
} else {
if (exec) {
out.write(ch);
out.append(ch);
}
}
break;
default:
if (exec) {
out.write(ch);
out.append(ch);
}
break;
}
Expand Down
21 changes: 2 additions & 19 deletions terminal/src/main/java/org/jline/utils/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
*/
package org.jline.utils;

import java.io.IOError;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -53,7 +50,7 @@ public Display(Terminal terminal, boolean fullscreen) {
this.wrapAtEol = terminal.getBooleanCapability(Capability.auto_right_margin);
this.delayedWrapAtEol = this.wrapAtEol
&& terminal.getBooleanCapability(Capability.eat_newline_glitch);
this.cursorDownIsNewLine = "\n".equals(tput(Capability.cursor_down));
this.cursorDownIsNewLine = "\n".equals(Curses.tputs(terminal.getStringCapability(Capability.cursor_down)));
}

/** If cursor is at right margin, don't wrap immediately.
Expand Down Expand Up @@ -373,24 +370,10 @@ private int cost(Capability cap) {
}

private int computeCost(Capability cap) {
String s = tput(cap, 0);
String s = Curses.tputs(terminal.getStringCapability(cap), 0);
return s != null ? s.length() : Integer.MAX_VALUE;
}

private String tput(Capability cap, Object... params) {
try {
StringWriter sw = new StringWriter();
String d = terminal.getStringCapability(cap);
if (d != null) {
Curses.tputs(sw, d, params);
return sw.toString();
}
return null;
} catch (IOException e) {
throw new IOError(e);
}
}

private static int[] longestCommon(List<AttributedString> l1, List<AttributedString> l2) {
int start1 = 0;
int start2 = 0;
Expand Down
7 changes: 1 addition & 6 deletions terminal/src/test/java/org/jline/utils/CursesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ public class CursesTest {
@Test
public void testTputs() throws Exception {

assertEquals("\033[3;4r", tputs("\\E[%i%p1%d;%p2%dr", 2, 3));
assertEquals("\033[3;4r", Curses.tputs("\\E[%i%p1%d;%p2%dr", 2, 3));

}

private String tputs(String cap, Object... params) throws Exception {
StringWriter sw = new StringWriter();
Curses.tputs(sw, cap, params);
return sw.toString();
}
}

0 comments on commit eee2e70

Please sign in to comment.