Skip to content

Commit f80b464

Browse files
committed
Fixed: Improve handling of empty ';' SGR sequences
Currently the Termux terminal emulator prints "HI" in red with: ```sh printf "\e[31;m HI \e[0m" ``` This is not how other terminals (tested on xterm, gnome-terminal, alacritty and the mac built in terminal) handle it, since they parse ""\e[31;m" as "\e[31;0m", where the "0" resets the colors. This change aligns with other terminals, as well as improves performance by avoiding allocating a new int[] array for each byte processed by `parseArg()`, and most importantly simplifies things by removing the `mIsCSIStart` and `mLastCSIArg` state, preparing for supporting ':' separated sub parameters such as used in https://sw.kovidgoyal.net/kitty/underlines/
1 parent a5de3d3 commit f80b464

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

terminal-emulator/src/main/java/com/termux/terminal/TerminalEmulator.java

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ public final class TerminalEmulator {
126126
private String mTitle;
127127
private final Stack<String> mTitleStack = new Stack<>();
128128

129-
/** If processing first character of first parameter of {@link #ESC_CSI}. */
130-
private boolean mIsCSIStart;
131-
/** The last character processed of a parameter of {@link #ESC_CSI}. */
132-
private Integer mLastCSIArg;
133-
134129
/** The cursor position. Between (0,0) and (mRows-1, mColumns-1). */
135130
private int mCursorRow, mCursorCol;
136131

@@ -1393,8 +1388,6 @@ private void doEsc(int b) {
13931388
break;
13941389
case '[':
13951390
continueSequence(ESC_CSI);
1396-
mIsCSIStart = true;
1397-
mLastCSIArg = null;
13981391
break;
13991392
case '=': // DECKPAM
14001393
setDecsetinternalBit(DECSET_BIT_APPLICATION_KEYPAD, true);
@@ -1765,7 +1758,7 @@ private void doCsi(int b) {
17651758
private void selectGraphicRendition() {
17661759
if (mArgIndex >= mArgs.length) mArgIndex = mArgs.length - 1;
17671760
for (int i = 0; i <= mArgIndex; i++) {
1768-
int code = mArgs[i];
1761+
int code = getArg(i, 0, false);
17691762
if (code < 0) {
17701763
if (mArgIndex > 0) {
17711764
continue;
@@ -1823,7 +1816,10 @@ private void selectGraphicRendition() {
18231816
if (i + 4 > mArgIndex) {
18241817
Logger.logWarn(mClient, LOG_TAG, "Too few CSI" + code + ";2 RGB arguments");
18251818
} else {
1826-
int red = mArgs[i + 2], green = mArgs[i + 3], blue = mArgs[i + 4];
1819+
int red = getArg(i + 2, 0, false);
1820+
int green = getArg(i + 3, 0, false);
1821+
int blue = getArg(i + 4, 0, false);
1822+
18271823
if (red < 0 || green < 0 || blue < 0 || red > 255 || green > 255 || blue > 255) {
18281824
finishSequenceAndLogError("Invalid RGB: " + red + "," + green + "," + blue);
18291825
} else {
@@ -1837,7 +1833,7 @@ private void selectGraphicRendition() {
18371833
i += 4; // "2;P_r;P_g;P_r"
18381834
}
18391835
} else if (firstArg == 5) {
1840-
int color = mArgs[i + 2];
1836+
int color = getArg(i + 2, 0, false);
18411837
i += 2; // "5;P_s"
18421838
if (color >= 0 && color < TextStyle.NUM_INDEXED_COLORS) {
18431839
if (code == 38) {
@@ -2116,44 +2112,29 @@ private void scrollDownOneLine() {
21162112
*
21172113
* https://vt100.net/docs/vt510-rm/chapter4.html#S4.3.3
21182114
* */
2119-
private void parseArg(int inputByte) {
2120-
int[] bytes = new int[]{inputByte};
2121-
// Only doing this for ESC_CSI and not for other ESC_CSI_* since they seem to be using their
2122-
// own defaults with getArg*() calls, but there may be missed cases
2123-
if (mEscapeState == ESC_CSI) {
2124-
if ((mIsCSIStart && inputByte == ';') || // If sequence starts with a ; character, like \033[;m
2125-
(!mIsCSIStart && mLastCSIArg != null && mLastCSIArg == ';' && inputByte == ';')) { // If sequence contains sequential ; characters, like \033[;;m
2126-
bytes = new int[]{'0', ';'}; // Assume 0 was passed
2127-
}
2128-
}
2129-
2130-
mIsCSIStart = false;
2131-
2132-
for (int b : bytes) {
2133-
if (b >= '0' && b <= '9') {
2134-
if (mArgIndex < mArgs.length) {
2135-
int oldValue = mArgs[mArgIndex];
2136-
int thisDigit = b - '0';
2137-
int value;
2138-
if (oldValue >= 0) {
2139-
value = oldValue * 10 + thisDigit;
2140-
} else {
2141-
value = thisDigit;
2142-
}
2143-
if (value > 9999)
2144-
value = 9999;
2145-
mArgs[mArgIndex] = value;
2146-
}
2147-
continueSequence(mEscapeState);
2148-
} else if (b == ';') {
2149-
if (mArgIndex < mArgs.length) {
2150-
mArgIndex++;
2115+
private void parseArg(int b) {
2116+
if (b >= '0' && b <= '9') {
2117+
if (mArgIndex < mArgs.length) {
2118+
int oldValue = mArgs[mArgIndex];
2119+
int thisDigit = b - '0';
2120+
int value;
2121+
if (oldValue >= 0) {
2122+
value = oldValue * 10 + thisDigit;
2123+
} else {
2124+
value = thisDigit;
21512125
}
2152-
continueSequence(mEscapeState);
2153-
} else {
2154-
unknownSequence(b);
2126+
if (value > 9999)
2127+
value = 9999;
2128+
mArgs[mArgIndex] = value;
2129+
}
2130+
continueSequence(mEscapeState);
2131+
} else if (b == ';') {
2132+
if (mArgIndex < mArgs.length) {
2133+
mArgIndex++;
21552134
}
2156-
mLastCSIArg = b;
2135+
continueSequence(mEscapeState);
2136+
} else {
2137+
unknownSequence(b);
21572138
}
21582139
}
21592140

terminal-emulator/src/test/java/com/termux/terminal/TerminalTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ public void testSelectGraphics() {
163163
assertEquals(TextStyle.COLOR_INDEX_FOREGROUND, mTerminal.mForeColor);
164164
enterString("\033[31;;m");
165165
assertEquals(TextStyle.COLOR_INDEX_FOREGROUND, mTerminal.mForeColor);
166+
enterString("\033[31;m");
167+
assertEquals(TextStyle.COLOR_INDEX_FOREGROUND, mTerminal.mForeColor);
168+
enterString("\033[31;;41m");
169+
assertEquals(TextStyle.COLOR_INDEX_FOREGROUND, mTerminal.mForeColor);
170+
assertEquals(1, mTerminal.mBackColor);
171+
enterString("\033[0m");
172+
assertEquals(TextStyle.COLOR_INDEX_BACKGROUND, mTerminal.mBackColor);
166173

167174
// 256 colors:
168175
enterString("\033[38;5;119m");
@@ -178,10 +185,18 @@ public void testSelectGraphics() {
178185
assertEquals(129, mTerminal.mBackColor);
179186

180187
// Multiple parameters at once:
181-
enterString("\033[38;5;178;48;5;179;m");
188+
enterString("\033[38;5;178;48;5;179m");
182189
assertEquals(178, mTerminal.mForeColor);
183190
assertEquals(179, mTerminal.mBackColor);
184191

192+
// Omitted parameter means zero:
193+
enterString("\033[38;5;m");
194+
assertEquals(0, mTerminal.mForeColor);
195+
assertEquals(179, mTerminal.mBackColor);
196+
enterString("\033[48;5;m");
197+
assertEquals(0, mTerminal.mForeColor);
198+
assertEquals(0, mTerminal.mBackColor);
199+
185200
// 24 bit colors:
186201
enterString(("\033[0m")); // Reset fg and bg colors.
187202
enterString("\033[38;2;255;127;2m");
@@ -205,6 +220,16 @@ public void testSelectGraphics() {
205220
enterString("\033[38;2;300;127;2;48;2;1;300;254m");
206221
assertEquals(expectedForeground, mTerminal.mForeColor);
207222
assertEquals(expectedBackground, mTerminal.mBackColor);
223+
224+
// 24 bit colors, omitted parameter means zero:
225+
enterString("\033[38;2;255;127;m");
226+
expectedForeground = 0xff000000 | (255 << 16) | (127 << 8);
227+
assertEquals(expectedForeground, mTerminal.mForeColor);
228+
assertEquals(expectedBackground, mTerminal.mBackColor);
229+
enterString("\033[38;2;123;;77m");
230+
expectedForeground = 0xff000000 | (123 << 16) | 77;
231+
assertEquals(expectedForeground, mTerminal.mForeColor);
232+
assertEquals(expectedBackground, mTerminal.mBackColor);
208233
}
209234

210235
public void testBackgroundColorErase() {

0 commit comments

Comments
 (0)