ScrollText now respects ANSI escape codes.

This commit is contained in:
Jonathan Bernard 2016-03-12 20:49:20 -06:00
parent 6d08caab80
commit 7101535412
2 changed files with 49 additions and 11 deletions

View File

@ -11,7 +11,7 @@ dependencies {
compile 'ch.qos.logback:logback-core:1.1.3'
compile 'org.slf4j:slf4j-api:1.7.14'
compile 'com.offbytwo:docopt:0.6.0.20150202'
compile 'com.jdbernard:jdb-util:4.3'
compile 'com.jdbernard:jdb-util:4.4'
compile 'jline:jline:2.12'
compile project(":wdiwtlt-core")
compile 'uk.co.caprica:vlcj:3.10.1'

View File

@ -1,11 +1,14 @@
package com.jdbernard.wdiwtlt.cli
import com.jdbernard.util.AnsiEscapeCodeSequence as ANSI
public class ScrollText {
public String text = ""
public int maxWidth
private int scrollIdx
private String curAnsiPrefix = ""
public void setMaxWidth(int max) {
this.maxWidth = Math.max(max, 1) }
@ -15,17 +18,52 @@ public class ScrollText {
this.scrollIdx = Math.max(0, text.size() - 10) }
public String getNextScroll() {
if (text.size() < maxWidth) return text
else {
scrollIdx = (scrollIdx + 1) % text.size()
int endIdx = Math.min(scrollIdx + maxWidth, text.size())
int wrapIdx = (scrollIdx + maxWidth) % text.size()
if (ANSI.strip(text).size() < maxWidth) return text
// don't need to wrap past the end
if (wrapIdx == endIdx) return text[scrollIdx..<endIdx]
scrollIdx = (scrollIdx + 1) % text.size()
// If we're on the start of an ANSI escape sequence, skip past the end
// of it.
while (text[scrollIdx] == '\u001b') {
def endIdx = text.findIndexOf(scrollIdx) {
Character.isLetter(it.charAt(0)) }
curAnsiPrefix = text[scrollIdx..endIdx]
scrollIdx = (endIdx + 1) % text.size() }
int toWalk = maxWidth
int endIdx = scrollIdx + 1
int wrapIdx = 0
boolean inAnsiSeq = false
while (toWalk > 0 && wrapIdx < scrollIdx) {
def cur
if (endIdx == text.size()) {
cur = text[wrapIdx]
if (cur == '\u001b') inAnsiSeq = true
if (inAnsiSeq && Character.isLetter(cur.charAt(0)))
inAnsiSeq = false
if (!inAnsiSeq) toWalk--
wrapIdx++ }
else {
cur = text[endIdx]
if (cur == '\u001b') inAnsiSeq = true
if (inAnsiSeq && Character.isLetter(cur.charAt(0)))
inAnsiSeq = false
if (!inAnsiSeq) toWalk--
endIdx++ } }
if (wrapIdx == 0) return curAnsiPrefix + text[scrollIdx..<endIdx]
return new StringBuilder()
.append(curAnsiPrefix)
.append(text[scrollIdx..<endIdx])
.append(" ").append(text[0..<wrapIdx]) } }
.append(" ").append(text[0..<wrapIdx]) }
public String toString() { return getNextScroll() }
}