Skip to content

Commit

Permalink
Fix some tput limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 10, 2020
1 parent b77a0a8 commit 99e130d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
73 changes: 72 additions & 1 deletion terminal/src/main/java/org/jline/utils/Curses.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,78 @@ private static void doTputs(Appendable out, String str, Object... params) throws
out.append(Integer.toString(toInteger(stack.pop())));
break;
default:
throw new UnsupportedOperationException();
if (ch == ':') {
ch = str.charAt(index++);
}
boolean alternate = false;
boolean left = false;
boolean space = false;
boolean plus = false;
int width = 0;
int prec = -1;
int cnv;
while ("-+# ".indexOf(ch) >= 0) {
switch (ch) {
case '-': left = true; break;
case '+': plus = true; break;
case '#': alternate = true; break;
case ' ': space = true; break;
}
ch = str.charAt(index++);
}
if ("123456789".indexOf(ch) >= 0) {
do {
width = width * 10 + (ch - '0');
ch = str.charAt(index++);
} while ("0123456789".indexOf(ch) >= 0);
}
if (ch == '.') {
prec = 0;
ch = str.charAt(index++);
}
if ("0123456789".indexOf(ch) >= 0) {
do {
prec = prec * 10 + (ch - '0');
ch = str.charAt(index++);
} while ("0123456789".indexOf(ch) >= 0);
}
if ("cdoxXs".indexOf(ch) < 0) {
throw new IllegalArgumentException();
}
cnv = ch;
if (exec) {
String res;
if (cnv == 's') {
res = (String) stack.pop();
if (prec >= 0) {
res = res.substring(0, prec);
}
} else {
int p = toInteger(stack.pop());
StringBuilder fmt = new StringBuilder(16);
fmt.append('%');
if (alternate) {
fmt.append('#');
}
if (plus) {
fmt.append('+');
}
if (space) {
fmt.append(' ');
}
if (prec >= 0) {
fmt.append('0');
fmt.append(prec);
}
fmt.append((char) cnv);
res = String.format(fmt.toString(), p);
}
if (width > res.length()) {
res = String.format("%" + (left ? "-" : "") + width + "s", res);
}
out.append(res);
}
break;
}
break;
case '$':
Expand Down
7 changes: 7 additions & 0 deletions terminal/src/test/java/org/jline/utils/CursesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public void testTputs() throws Exception {

}

@Test
public void testInitc() throws Exception {

assertEquals("\033]4;123;rgb:3F/00/22\033\\",
Curses.tputs("\\E]4;%p1%d;rgb\\:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*%{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\\E\\\\", 123, 0xfa, 0x00, 0x89));
}

}

0 comments on commit 99e130d

Please sign in to comment.