summaryrefslogtreecommitdiff
path: root/src/ansi_test.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-03-19 01:59:14 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-03-19 01:59:14 +0900
commite70a2a5817586e4e7df0ee1446f609bbd859164a (patch)
tree24ea4cb8865233ec89e2e2828a66727cf0b129f4 /src/ansi_test.go
parentd80a41bb6d1a507d65885d553a30d4e7dc7d0453 (diff)
downloadfzf-e70a2a5817586e4e7df0ee1446f609bbd859164a.tar.gz
Add support for ANSI color codes
Diffstat (limited to 'src/ansi_test.go')
-rw-r--r--src/ansi_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/ansi_test.go b/src/ansi_test.go
new file mode 100644
index 00000000..37196dd8
--- /dev/null
+++ b/src/ansi_test.go
@@ -0,0 +1,91 @@
+package fzf
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestExtractColor(t *testing.T) {
+ assert := func(offset AnsiOffset, b int32, e int32, fg int, bg int, bold bool) {
+ if offset.offset[0] != b || offset.offset[1] != e ||
+ offset.color.fg != fg || offset.color.bg != bg || offset.color.bold != bold {
+ t.Error(offset, b, e, fg, bg, bold)
+ }
+ }
+
+ src := "hello world"
+ clean := "\x1b[0m"
+ check := func(assertion func(ansiOffsets []AnsiOffset)) {
+ output, ansiOffsets := ExtractColor(&src)
+ if *output != "hello world" {
+ t.Errorf("Invalid output: {}", output)
+ }
+ fmt.Println(src, ansiOffsets, clean)
+ assertion(ansiOffsets)
+ }
+
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) > 0 {
+ t.Fail()
+ }
+ })
+
+ src = "\x1b[0mhello world"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) > 0 {
+ t.Fail()
+ }
+ })
+
+ src = "\x1b[1mhello world"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 1 {
+ t.Fail()
+ }
+ assert(offsets[0], 0, 11, -1, -1, true)
+ })
+
+ src = "hello \x1b[34;45;1mworld"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 1 {
+ t.Fail()
+ }
+ assert(offsets[0], 6, 11, 4, 5, true)
+ })
+
+ src = "hello \x1b[34;45;1mwor\x1b[34;45;1mld"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 1 {
+ t.Fail()
+ }
+ assert(offsets[0], 6, 11, 4, 5, true)
+ })
+
+ src = "hello \x1b[34;45;1mwor\x1b[0mld"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 1 {
+ t.Fail()
+ }
+ assert(offsets[0], 6, 9, 4, 5, true)
+ })
+
+ src = "hello \x1b[34;48;5;233;1mwo\x1b[38;5;161mr\x1b[0ml\x1b[38;5;161md"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 3 {
+ t.Fail()
+ }
+ assert(offsets[0], 6, 8, 4, 233, true)
+ assert(offsets[1], 8, 9, 161, 233, true)
+ assert(offsets[2], 10, 11, 161, -1, false)
+ })
+
+ // {38,48};5;{38,48}
+ src = "hello \x1b[38;5;38;48;5;48;1mwor\x1b[38;5;48;48;5;38ml\x1b[0md"
+ check(func(offsets []AnsiOffset) {
+ if len(offsets) != 2 {
+ t.Fail()
+ }
+ assert(offsets[0], 6, 9, 38, 48, true)
+ assert(offsets[1], 9, 10, 48, 38, true)
+ })
+}