-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Strip function to strip ANSI escape sequences from a byte slice.
Fix up some documentation.
- Loading branch information
Showing
4 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ansi | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// An errorList is simply a list of errors. | ||
type errorList []error | ||
|
||
func (e errorList) Error() string { | ||
if len(e) == 0 { | ||
return "" | ||
} | ||
parts := make([]string, len(e)) | ||
for x, err := range e { | ||
parts[x] = err.Error() | ||
} | ||
return strings.Join(parts, "\n") | ||
} | ||
|
||
func (e errorList) err() error { | ||
switch len(e) { | ||
case 0: | ||
return nil | ||
case 1: | ||
return e[0] | ||
default: | ||
return e | ||
} | ||
} | ||
|
||
// Strip returns in with all ANSI escape sequences stripped. An error is | ||
// also returned if one or more of the stripped escape sequences are invalid. | ||
func Strip(in []byte) ([]byte, error) { | ||
var errs errorList | ||
var out []string | ||
var s *S | ||
var err error | ||
for len(in) > 0 { | ||
in, s, err = Decode(in) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("%q: %v", s, err)) | ||
} | ||
// If s.Type is "" then s represents plain text and not | ||
// an escape sequence. We are only interested in plain | ||
// text. | ||
if s.Type == "" { | ||
out = append(out, string(s.Code)) | ||
} | ||
} | ||
if len(out) > 0 { | ||
return []byte(strings.Join(out, "")), errs.err() | ||
} | ||
return nil, errs.err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package ansi | ||
|
||
import "testing" | ||
|
||
func TestStrip(t *testing.T) { | ||
for _, tt := range []struct { | ||
in, out string | ||
errors bool | ||
}{ | ||
{}, | ||
|
||
// Make sure control characters are not stripped. | ||
{in: "abc", out: "abc"}, | ||
{in: "abc\r\n", out: "abc\r\n"}, | ||
{in: "\t", out: "\t"}, | ||
{in: "abc\t", out: "abc\t"}, | ||
{in: "\tabc", out: "\tabc"}, | ||
{in: "abc\tabc", out: "abc\tabc"}, | ||
|
||
// Lone escape | ||
{in: "\033", errors: true}, | ||
{in: "abc\033", out: "abc", errors: true}, | ||
|
||
// invalid escape sequence | ||
{in: "abc\033\r\n", out: "abc\n", errors: true}, | ||
{in: "ab\033\tc", out: "abc", errors: true}, | ||
{in: "abc\033\033\n", out: "abc\n", errors: true}, | ||
|
||
// Strip simple escape sequences | ||
{in: "\033B"}, | ||
{in: "abc\033B", out: "abc"}, | ||
{in: "\033Babc", out: "abc"}, | ||
{in: "a\033Bbc", out: "abc"}, | ||
{in: "a\033Bb\033Bc", out: "abc"}, | ||
{in: "a\033B\033Bbc", out: "abc"}, | ||
|
||
// Strip multi-byte CSI escape sequences with no parameters | ||
{in: "\033[A"}, | ||
{in: "abc\033[A", out: "abc"}, | ||
{in: "\033[Aabc", out: "abc"}, | ||
{in: "a\033[Abc", out: "abc"}, | ||
{in: "a\033[Ab\033[Ac", out: "abc"}, | ||
{in: "a\033[A\033[Abc", out: "abc"}, | ||
|
||
// Strip single byte CSI escape sequences | ||
{in: "\233A"}, | ||
{in: "abc\233A", out: "abc"}, | ||
{in: "\233Aabc", out: "abc"}, | ||
{in: "a\233Abc", out: "abc"}, | ||
{in: "a\233Ab\233Ac", out: "abc"}, | ||
{in: "a\233A\233Abc", out: "abc"}, | ||
|
||
// Strip CSI escape sequences with parameters | ||
{in: "\033[4A"}, | ||
{in: "abc\033[4A", out: "abc"}, | ||
{in: "\033[4Aabc", out: "abc"}, | ||
{in: "a\033[4Abc", out: "abc"}, | ||
{in: "a\033[4Ab\033[4Ac", out: "abc"}, | ||
{in: "a\033[4A\033[4Abc", out: "abc"}, | ||
{in: "a\033[4A\033[4Abc", out: "abc"}, | ||
|
||
// Strip CSI escape sequenes with intermediate bytes | ||
// Strip CSI escape sequence with multiple parameters | ||
{in: "\033[1;2 Tabc", out: "abc"}, | ||
|
||
// Strip SOS escape sequences (start of string) | ||
{in: "\033_This is an APC string\033\\abc", out: "abc"}, | ||
|
||
// SOS without ST | ||
{in: "\033_This is an incomplete APC string", out: "", errors: true}, | ||
|
||
// too many parameters | ||
{in: "\033[1;2Aabc", out: "abc", errors: true}, | ||
|
||
// two few parameters (" T" requires 2) | ||
{in: "\033[ Tabc", out: "abc", errors: true}, | ||
{in: "\033[1 Tabc", out: "abc", errors: true}, | ||
} { | ||
bout, err := Strip([]byte(tt.in)) | ||
switch { | ||
case tt.errors && err == nil: | ||
t.Errorf("%q: did not get expected error", tt.in) | ||
case !tt.errors && err != nil: | ||
t.Errorf("%q: got unexpected error %v", tt.in, err) | ||
} | ||
out := string(bout) | ||
if out != tt.out { | ||
t.Errorf("%q: got %q, want %q", tt.in, out, tt.out) | ||
} | ||
} | ||
} |