Skip to content

Commit

Permalink
Support alternate charset for box-drawing operations
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 26, 2018
1 parent 0e135e5 commit adb1d94
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class TerminalBuilder {

public static final String PROP_NON_BLOCKING_READS = "org.jline.terminal.pty.nonBlockingReads";
public static final String PROP_COLOR_DISTANCE = "org.jline.utils.colorDistance";
public static final String PROP_DISABLE_ALTERNATE_CHARSET = "org.jline.utils.disableAlternateCharset";

/**
* Returns the default system terminal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import static org.jline.utils.AttributedStyle.F_UNDERLINE;
import static org.jline.utils.AttributedStyle.F_HIDDEN;
import static org.jline.utils.AttributedStyle.MASK;
import static org.jline.terminal.TerminalBuilder.PROP_DISABLE_ALTERNATE_CHARSET;

public abstract class AttributedCharSequence implements CharSequence {

// cache the value here as we can't afford to get it each time
static final boolean DISABLE_ALTERNATE_CHARSET = Boolean.getBoolean(PROP_DISABLE_ALTERNATE_CHARSET);

public String toAnsi() {
return toAnsi(null);
}
Expand All @@ -44,23 +48,54 @@ public String toAnsi(Terminal terminal) {
}
int colors = 256;
boolean force256colors = false;
String alternateIn = null, alternateOut = null;
if (terminal != null) {
Integer max_colors = terminal.getNumericCapability(Capability.max_colors);
if (max_colors != null) {
colors = max_colors;
}
force256colors = AbstractWindowsTerminal.TYPE_WINDOWS_256_COLOR.equals(terminal.getType());
if (!DISABLE_ALTERNATE_CHARSET) {
alternateIn = Curses.tputs(terminal.getStringCapability(Capability.enter_alt_charset_mode));
alternateOut = Curses.tputs(terminal.getStringCapability(Capability.exit_alt_charset_mode));
}
}
return toAnsi(colors, force256colors);
return toAnsi(colors, force256colors, alternateIn, alternateOut);
}

public String toAnsi(int colors, boolean force256colors) {
return toAnsi(colors, force256colors, null, null);
}

public String toAnsi(int colors, boolean force256colors, String altIn, String altOut) {
StringBuilder sb = new StringBuilder();
int style = 0;
int foreground = -1;
int background = -1;
boolean alt = false;
for (int i = 0; i < length(); i++) {
char c = charAt(i);
if (altIn != null && altOut != null) {
char pc = c;
switch (c) {
case '┘': c = 'j'; break;
case '┐': c = 'k'; break;
case '┌': c = 'l'; break;
case '└': c = 'm'; break;
case '┼': c = 'n'; break;
case '─': c = 'q'; break;
case '├': c = 't'; break;
case '┤': c = 'u'; break;
case '┴': c = 'v'; break;
case '┬': c = 'w'; break;
case '│': c = 'x'; break;
}
boolean oldalt = alt;
alt = c != pc;
if (oldalt ^ alt) {
sb.append(alt ? altIn : altOut);
}
}
int s = styleCodeAt(i) & ~F_HIDDEN; // The hidden flag does not change the ansi styles
if (style != s) {
int d = (style ^ s) & MASK;
Expand Down Expand Up @@ -142,6 +177,9 @@ public String toAnsi(int colors, boolean force256colors) {
}
sb.append(c);
}
if (alt) {
sb.append(altOut);
}
if (style != 0) {
sb.append("\033[0m");
}
Expand Down

0 comments on commit adb1d94

Please sign in to comment.