Skip to content

Commit

Permalink
Fix rendering problems on windows, fixes #114
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 24, 2017
1 parent aa22442 commit c0ce9c5
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 416 deletions.
41 changes: 37 additions & 4 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.StringWriter;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.Curses;
import org.jline.utils.Display;
import org.jline.utils.InfoCmp.Capability;
import org.jline.utils.Levenshtein;
Expand Down Expand Up @@ -489,6 +491,11 @@ public String readLine(String prompt, String rightPrompt, Character mask, String
callWidget(FRESH_LINE);
if (isSet(Option.MOUSE))
terminal.trackMouse(Terminal.MouseTracking.Normal);
} else {
// For dumb terminals, we need to make sure that CR are ignored
Attributes attr = new Attributes(originalAttributes);
attr.setInputFlag(Attributes.InputFlag.IGNCR, true);
terminal.setAttributes(attr);
}

callWidget(CALLBACK_INIT);
Expand Down Expand Up @@ -583,16 +590,42 @@ public String readLine(String prompt, String rightPrompt, Character mask, String

/** Make sure we position the cursor on column 0 */
protected boolean freshLine() {
boolean wrapAtEol = terminal.getBooleanCapability(Capability.auto_right_margin);
boolean delayedWrapAtEol = wrapAtEol && terminal.getBooleanCapability(Capability.eat_newline_glitch);
AttributedStringBuilder sb = new AttributedStringBuilder();
sb.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.BLACK + AttributedStyle.BRIGHT));
sb.append("~");
sb.style(AttributedStyle.DEFAULT);
for (int i = 0; i < size.getColumns() - 1; i++) {
if (!wrapAtEol || delayedWrapAtEol) {
for (int i = 0; i < size.getColumns() - 1; i++) {
sb.append(" ");
}
sb.append(KeyMap.key(terminal, Capability.carriage_return));
sb.append(" ");
sb.append(KeyMap.key(terminal, Capability.carriage_return));
} else {
// Given the terminal will wrap automatically,
// we need to print one less than needed.
// This means that the last character will not
// be overwritten, and that's why we're using
// 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());
}
for (int i = 0; i < size.getColumns() - 2; i++) {
sb.append(" ");
}
sb.append(KeyMap.key(terminal, Capability.carriage_return));
sb.append(" ");
sb.append(KeyMap.key(terminal, Capability.carriage_return));
}
sb.append(KeyMap.key(terminal, Capability.carriage_return));
sb.append(" ");
sb.append(KeyMap.key(terminal, Capability.carriage_return));
print(sb.toAnsi(terminal));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;

/**
* A ANSI output stream extracts ANSI escape codes written to
Expand Down Expand Up @@ -203,6 +204,21 @@ private void reset(boolean skipBuffer) throws IOException {
state = LOOKING_FOR_FIRST_ESC_CHAR;
}

/**
* Helper for processEscapeCommand() to iterate over integer options
* @param optionsIterator the underlying iterator
* @throws IOException if no more non-null values left
*/
private int getNextOptionInt(Iterator<Object> optionsIterator) throws IOException {
for (;;) {
if (!optionsIterator.hasNext())
throw new IllegalArgumentException();
Object arg = optionsIterator.next();
if (arg != null)
return (Integer) arg;
}
}

/**
* @param options
* @param command
Expand Down Expand Up @@ -257,14 +273,52 @@ private boolean processEscapeCommand(ArrayList<Object> options, int command) thr
}

int count = 0;
for (Object next : options) {
Iterator<Object> optionsIterator = options.iterator();
while (optionsIterator.hasNext()) {
Object next = optionsIterator.next();
if (next != null) {
count++;
int value = (Integer) next;
if (30 <= value && value <= 37) {
processSetForegroundColor(value - 30);
} else if (40 <= value && value <= 47) {
processSetBackgroundColor(value - 40);
} else if (90 <= value && value <= 97) {
processSetForegroundColor(value - 90, true);
} else if (100 <= value && value <= 107) {
processSetBackgroundColor(value - 100, true);
} else if (value == 38 || value == 48) {
// extended color like `esc[38;5;<index>m` or `esc[38;2;<r>;<g>;<b>m`
int arg2or5 = getNextOptionInt(optionsIterator);
if (arg2or5 == 2) {
// 24 bit color style like `esc[38;2;<r>;<g>;<b>m`
int r = getNextOptionInt(optionsIterator);
int g = getNextOptionInt(optionsIterator);
int b = getNextOptionInt(optionsIterator);
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) {
if (value == 38)
processSetForegroundColorExt(r, g, b);
else
processSetBackgroundColorExt(r, g, b);
} else {
throw new IllegalArgumentException();
}
}
else if (arg2or5 == 5) {
// 256 color style like `esc[38;5;<index>m`
int paletteIndex = getNextOptionInt(optionsIterator);
if (paletteIndex >= 0 && paletteIndex <= 255) {
if (value == 38)
processSetForegroundColorExt(paletteIndex);
else
processSetBackgroundColorExt(paletteIndex);
} else {
throw new IllegalArgumentException();
}
}
else {
throw new IllegalArgumentException();
}
} else {
switch (value) {
case 39:
Expand Down Expand Up @@ -377,7 +431,7 @@ protected void processEraseLine(int eraseOption) throws IOException {
protected static final int ATTRIBUTE_INTENSITY_NORMAL = 22; // Intensity; Normal not bold and not faint
protected static final int ATTRIBUTE_UNDERLINE_OFF = 24; // Underline; None
protected static final int ATTRIBUTE_BLINK_OFF = 25; // Blink; off
protected static final int ATTRIBUTE_NEGATIVE_Off = 27; // Image; Positive
protected static final int ATTRIBUTE_NEGATIVE_OFF = 27; // Image; Positive
protected static final int ATTRIBUTE_CONCEAL_OFF = 28; // Reveal conceal off

protected void processSetAttribute(int attribute) throws IOException {
Expand All @@ -393,9 +447,29 @@ protected void processSetAttribute(int attribute) throws IOException {
protected static final int WHITE = 7;

protected void processSetForegroundColor(int color) throws IOException {
processSetForegroundColor(color, false);
}

protected void processSetForegroundColor(int color, boolean bright) throws IOException {
}

protected void processSetForegroundColorExt(int paletteIndex) throws IOException {
}

protected void processSetForegroundColorExt(int r, int g, int b) throws IOException {
}

protected void processSetBackgroundColor(int color) throws IOException {
processSetBackgroundColor(color, false);
}

protected void processSetBackgroundColor(int color, boolean bright) throws IOException {
}

protected void processSetBackgroundColorExt(int paletteIndex) throws IOException {
}

protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException {
}

protected void processDefaultTextColor() throws IOException {
Expand Down
Loading

0 comments on commit c0ce9c5

Please sign in to comment.