From 90b0cd44ac8fb9a6965c3dcf050f0e6aa2e90121 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 28 Aug 2015 21:23:10 +0900 Subject: [PATCH] Should not strip ANSI codes when --ansi is not set --- src/core.go | 4 ++-- src/item.go | 14 +++++++++----- src/terminal.go | 8 +++++--- test/test_go.rb | 6 ++++++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/core.go b/src/core.go index fdd1e06..4f07215 100644 --- a/src/core.go +++ b/src/core.go @@ -174,7 +174,7 @@ func Run(opts *Options) { chunks: snapshot, pattern: pattern}) for i := 0; i < merger.Length(); i++ { - fmt.Println(merger.Get(i).AsString()) + fmt.Println(merger.Get(i).AsString(opts.Ansi)) } } os.Exit(0) @@ -250,7 +250,7 @@ func Run(opts *Options) { fmt.Println() } for i := 0; i < count; i++ { - fmt.Println(val.Get(i).AsString()) + fmt.Println(val.Get(i).AsString(opts.Ansi)) } os.Exit(0) } diff --git a/src/item.go b/src/item.go index 2ab8a78..12ca3df 100644 --- a/src/item.go +++ b/src/item.go @@ -94,15 +94,19 @@ func (item *Item) Rank(cache bool) Rank { } // AsString returns the original string -func (item *Item) AsString() string { - return *item.StringPtr() +func (item *Item) AsString(stripAnsi bool) string { + return *item.StringPtr(stripAnsi) } // StringPtr returns the pointer to the original string -func (item *Item) StringPtr() *string { +func (item *Item) StringPtr(stripAnsi bool) *string { if item.origText != nil { - trimmed, _, _ := extractColor(string(*item.origText), nil) - return &trimmed + if stripAnsi { + trimmed, _, _ := extractColor(string(*item.origText), nil) + return &trimmed + } + orig := string(*item.origText) + return &orig } str := string(item.text) return &str diff --git a/src/terminal.go b/src/terminal.go index aca319a..053ed78 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -42,6 +42,7 @@ type Terminal struct { history *History cycle bool header []string + ansi bool margin [4]string marginInt [4]int count int @@ -207,6 +208,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal { marginInt: [4]int{0, 0, 0, 0}, cycle: opts.Cycle, header: opts.Header, + ansi: opts.Ansi, reading: true, merger: EmptyMerger, selected: make(map[uint32]selectedItem), @@ -288,7 +290,7 @@ func (t *Terminal) output() { if len(t.selected) == 0 { cnt := t.merger.Length() if cnt > 0 && cnt > t.cy { - fmt.Println(t.merger.Get(t.cy).AsString()) + fmt.Println(t.merger.Get(t.cy).AsString(t.ansi)) } } else { sels := make([]selectedItem, 0, len(t.selected)) @@ -805,7 +807,7 @@ func (t *Terminal) Loop() { } selectItem := func(item *Item) bool { if _, found := t.selected[item.index]; !found { - t.selected[item.index] = selectedItem{time.Now(), item.StringPtr()} + t.selected[item.index] = selectedItem{time.Now(), item.StringPtr(t.ansi)} return true } return false @@ -843,7 +845,7 @@ func (t *Terminal) Loop() { case actExecute: if t.cy >= 0 && t.cy < t.merger.Length() { item := t.merger.Get(t.cy) - executeCommand(t.execmap[mapkey], item.AsString()) + executeCommand(t.execmap[mapkey], item.AsString(t.ansi)) } case actInvalid: t.mutex.Unlock() diff --git a/test/test_go.rb b/test/test_go.rb index ba229bf..377af22 100644 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -784,6 +784,12 @@ class TestGoFZF < TestBase assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi`.chomp end + def test_with_nth_no_ansi + src = "\x1b[33mhello \x1b[34;1mworld\x1b[m " + writelines tempname, [src, 'byebye'] + assert_equal src, `cat #{tempname} | #{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi`.chomp + end + private def writelines path, lines File.unlink path while File.exists? path