Skip to content
Myr

Trim unsupported OSC sequences (#4169)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #4169

authored by Junegunn Choi · committed by Junegunn Choi · parent d7b13f3 · Browse files · 2 files+28 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−12 removed

  • other:+28 added−12 removed

2 files+28 added−12 removed

modified:src/ansi_test.go+11 added-3 removed
335 unmodified lines
336 assert((*offsets)[1], 6, 11, 200, 100, false)337 })338339 state = nil340 var color24 tui.Color = (1 << 24) + (180 << 16) + (190 << 8) + 254341 src = "\x1b[1mhello \x1b[22;1;38:2:180:190:254mworld"342 check(func(offsets *[]ansiOffset, state *ansiState) {343 if len(*offsets) != 2 {344 t.Fail()345 }345 var color tui.Color = (1 << 24) + (180 << 16) + (190 << 8) + 254346 if state.fg != color || state.attr != 1 {346 if state.fg != color24 || state.attr != 1 {347 t.Fail()348 }349 assert((*offsets)[0], 0, 6, -1, -1, true)350 assert((*offsets)[1], 6, 11, color, -1, true)350 assert((*offsets)[1], 6, 11, color24, -1, true)351 })352353 src = "\x1b]133;A\x1b\\hello \x1b]133;C\x1b\\world"354 check(func(offsets *[]ansiOffset, state *ansiState) {355 if len(*offsets) != 1 {356 t.Fail()357 }358 assert((*offsets)[0], 0, 11, color24, -1, true)359 })360}361362func TestAnsiCodeStringConversion(t *testing.T) {363 assert := func(code string, prevState *ansiState, expected string) {
90 unmodified lines
modified:src/ansi.go+17 added-9 removed
96 unmodified lines
97func isPrint(c uint8) bool {98 return '\x20' <= c && c <= '\x7e'99}100101func matchOperatingSystemCommand(s string) int {101func matchOperatingSystemCommand(s string, start int) int {102 // `\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)`103 // ^ match starting here103 // ^ match starting here after the first printable character104 //105 i := 5 // prefix matched in nextAnsiEscapeSequence()105 i := start // prefix matched in nextAnsiEscapeSequence()106 for ; i < len(s) && isPrint(s[i]); i++ {107 }108 if i < len(s) {109 if s[i] == '\x07' {
45 unmodified lines
155156// nextAnsiEscapeSequence returns the ANSI escape sequence and is equivalent to157// calling FindStringIndex() on the below regex (which was originally used):158//159// "(?:\x1b[\\[()][0-9;:?]*[a-zA-Z@]|\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)"159// "(?:\x1b[\\[()][0-9;:?]*[a-zA-Z@]|\x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)"160func nextAnsiEscapeSequence(s string) (int, int) {161 // fast check for ANSI escape sequences162 i := 0163 for ; i < len(s); i++ {
26 unmodified lines
190 return i, i + j191 }192 }193194 // match: `\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)`195 if i+5 < len(s) && s[i+1] == ']' && isNumeric(s[i+2]) &&196 (s[i+3] == ';' || s[i+3] == ':') && isPrint(s[i+4]) {194 // match: `\x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)`195 if i+5 < len(s) && s[i+1] == ']' {196 j := 2197 // \x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)198 // ------199 for ; i+j < len(s) && isNumeric(s[i+j]); j++ {200 }201202 // \x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)203 // ---------------204 if j > 2 && i+j+1 < len(s) && (s[i+j] == ';' || s[i+j] == ':') && isPrint(s[i+j+1]) {198 if j := matchOperatingSystemCommand(s[i:]); j != -1 {199 return i, i + j205 if k := matchOperatingSystemCommand(s[i:], j+2); k != -1 {206 return i, i + k207 }208 }209 }210211 // match: `\x1b.`
270 unmodified lines