summaryrefslogtreecommitdiffstats
path: root/src/core.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/core.go
parentd80a41bb6d1a507d65885d553a30d4e7dc7d0453 (diff)
Add support for ANSI color codes
Diffstat (limited to 'src/core.go')
-rw-r--r--src/core.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/core.go b/src/core.go
index 62190d08..d561ab40 100644
--- a/src/core.go
+++ b/src/core.go
@@ -63,14 +63,36 @@ func Run(options *Options) {
// Event channel
eventBox := util.NewEventBox()
+ // ANSI code processor
+ extractColors := func(data *string) (*string, []AnsiOffset) {
+ // By default, we do nothing
+ return data, nil
+ }
+ if opts.Ansi {
+ if opts.Color {
+ extractColors = func(data *string) (*string, []AnsiOffset) {
+ return ExtractColor(data)
+ }
+ } else {
+ // When color is disabled but ansi option is given,
+ // we simply strip out ANSI codes from the input
+ extractColors = func(data *string) (*string, []AnsiOffset) {
+ trimmed, _ := ExtractColor(data)
+ return trimmed, nil
+ }
+ }
+ }
+
// Chunk list
var chunkList *ChunkList
if len(opts.WithNth) == 0 {
chunkList = NewChunkList(func(data *string, index int) *Item {
+ data, colors := extractColors(data)
return &Item{
- text: data,
- index: uint32(index),
- rank: Rank{0, 0, uint32(index)}}
+ text: data,
+ index: uint32(index),
+ colors: colors,
+ rank: Rank{0, 0, uint32(index)}}
})
} else {
chunkList = NewChunkList(func(data *string, index int) *Item {
@@ -79,7 +101,12 @@ func Run(options *Options) {
text: Transform(tokens, opts.WithNth).whole,
origText: data,
index: uint32(index),
+ colors: nil,
rank: Rank{0, 0, uint32(index)}}
+
+ trimmed, colors := extractColors(item.text)
+ item.text = trimmed
+ item.colors = colors
return &item
})
}