diff --git a/fish_tests.cpp b/fish_tests.cpp index 6584380de..a415d08e7 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -1209,6 +1209,13 @@ static void test_escape_sequences(void) if (escape_code_length(L"\x1b[2J") != 4) err(L"test_escape_sequences failed on line %d\n", __LINE__); if (escape_code_length(L"\x1b[38;5;123mABC") != strlen("\x1b[38;5;123m")) err(L"test_escape_sequences failed on line %d\n", __LINE__); if (escape_code_length(L"\x1b@") != 2) err(L"test_escape_sequences failed on line %d\n", __LINE__); + if (escape_code_length(L"\x1b@") != 2) err(L"test_escape_sequences failed on line %d\n", __LINE__); + + // iTerm2 escape sequences + if (escape_code_length(L"\x1b]50;CurrentDir=/tmp/foo\x07NOT_PART_OF_SEQUENCE") != 24) err(L"test_escape_sequences failed on line %d\n", __LINE__); + if (escape_code_length(L"\x1b]50;SetMark\x07NOT_PART_OF_SEQUENCE") != 12) err(L"test_escape_sequences failed on line %d\n", __LINE__); + if (escape_code_length(L"\x1b" L"]6;1;bg;red;brightness;255\x07NOT_PART_OF_SEQUENCE") != 27) err(L"test_escape_sequences failed on line %d\n", __LINE__); + if (escape_code_length(L"\x1b]Pg4040ff\x1b\\NOT_PART_OF_SEQUENCE") != 10) err(L"test_escape_sequences failed on line %d\n", __LINE__); } class lru_node_test_t : public lru_node_t diff --git a/screen.cpp b/screen.cpp index 96fe0f2a5..7bb022263 100644 --- a/screen.cpp +++ b/screen.cpp @@ -252,6 +252,32 @@ size_t escape_code_length(const wchar_t *code) } } } + + if (! found) + { + /* iTerm2 escape codes: CSI followed by ], terminated by either BEL or - see https://code.google.com/p/iterm2/wiki/ProprietaryEscapeCodes */ + if (code[1] == ']') + { + /* A sequence of characters terminated by either 'ESC backslash' or BEL */ + const wchar_t * const end1_sentinel = L"\x1b\\"; + const wchar_t * const end2_sentinel = L"\a"; + const wchar_t *end1 = wcsstr(&code[2], end1_sentinel); + const wchar_t *end2 = wcsstr(&code[2], end2_sentinel); + + // Use the non-null end, or if both are null, use the earlier end + const wchar_t *end = end1; + if (end == NULL || (end2 != NULL && end2 < end)) + { + end = end2; + } + if (end != NULL) + { + assert(end > code); + resulting_length = (end - code); + found = true; + } + } + } if (! found) {