Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support carriage returns, tabs, and "[0;m" #7

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion regex.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
this is the source definitions for the scary escape code regex

# from tests in Terminal.app, this regex should cover all basic \e[ and \e] cases
^([\[\]]([\d\?]+)?(;[\d\?]+)*)?.
^([\[\]]([\d\?]+)?(;[\d\?]*)*)?[m@GCDPK;l]

# this catches any case the above does not
# make sure to not include any special characters the main regex finds (like ?)
Expand Down
6 changes: 4 additions & 2 deletions vtclean.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// see regex.txt for a slightly separated version of this regex
var vt100re = regexp.MustCompile(`^\033([\[\]]([\d\?]+)?(;[\d\?]+)*)?(.)`)
var vt100re = regexp.MustCompile(`^\033([\[\]]([\d\?]+)?(;[\d\?]*)*)?([m@GCDPK;l])`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not thrilled about ([m@GCDPK;l]), however leaving it as (.) broke this test as the r would get matched

"ccc \033]4;1;rgb:38/54/71test":       "ccc rgb:38/54/71test",

var vt100exc = regexp.MustCompile(`^\033(\[[^a-zA-Z0-9@\?]+|[\(\)]).`)

// this is to handle the RGB escape generated by `tput initc 1 500 500 500`
Expand All @@ -21,6 +21,8 @@ func Clean(line string, color bool) string {
for i := 0; i < len(lineb); {
c := lineb[i]
switch c {
case '\r':
edit.MoveAbs(0)
case '\b':
edit.Move(-1)
case '\033':
Expand Down Expand Up @@ -74,7 +76,7 @@ func Clean(line string, color bool) string {
}
continue
default:
if c == '\n' || c >= ' ' {
if c == '\n' || c == '\t' || c >= ' ' {
edit.Write([]byte{c})
}
}
Expand Down
12 changes: 12 additions & 0 deletions vtclean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ var tests = map[string]string{

"bbb \033]4;1;rgb:38/54/71\033\\test": "bbb test",
"ccc \033]4;1;rgb:38/54/71test": "ccc rgb:38/54/71test",

// tabs
"aa\tbb": "aa\tbb",

// carriage return
"aaa\rb": "baa",
}

var colorTests = map[string]string{
"aaa \033[25;25mtest": "aaa \033[25;25mtest\x1b[0m",

// [0;m cases
// TODO: trailing [0;m should probably not be overridden with [0m
// TODO: when [0;m seen, may not want to append [0m to the end
Copy link
Owner

@lunixbochs lunixbochs Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain these TODOs?

"\033[0;m aa": "\033[0;m aa\033[0m",
"\033[32;1m$ echo foobar\033[0;m": "\033[32;1m$ echo foobar\033[0m",
}

func TestMain(t *testing.T) {
Expand Down