Should not strip ANSI codes when --ansi is not set
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 698e800 · Browse files · 4 files·+22 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+22 added−10 removed
- tests:+6 added−0 removed
- other:+16 added−10 removed
Provenance
- Commit
90b0cd4- Tree
2d1f492- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
698e800
4 files changed+22 added−10 removed
modified:src/core.go+2 added-2 removed
172 unmodified lines173 merger, _ := matcher.scan(MatchRequest{174 chunks: snapshot,175 pattern: pattern})176 for i := 0; i < merger.Length(); i++ {177- fmt.Println(merger.Get(i).AsString())177+ fmt.Println(merger.Get(i).AsString(opts.Ansi))178 }179 }180 os.Exit(0)181 }67 unmodified lines249 if len(opts.Expect) > 0 {250 fmt.Println()251 }252 for i := 0; i < count; i++ {253- fmt.Println(val.Get(i).AsString())253+ fmt.Println(val.Get(i).AsString(opts.Ansi))254 }255 os.Exit(0)256 }257 deferred = false16 unmodified linesmodified:src/item.go+9 added-5 removed
92 unmodified lines93 return rank94 }95 96 // AsString returns the original string97-func (item *Item) AsString() string {98- return *item.StringPtr()97+func (item *Item) AsString(stripAnsi bool) string {98+ return *item.StringPtr(stripAnsi)99 }100 101 // StringPtr returns the pointer to the original string102-func (item *Item) StringPtr() *string {102+func (item *Item) StringPtr(stripAnsi bool) *string {103 if item.origText != nil {104+ if stripAnsi {104- trimmed, _, _ := extractColor(string(*item.origText), nil)105- return &trimmed105+ trimmed, _, _ := extractColor(string(*item.origText), nil)106+ return &trimmed107+ }108+ orig := string(*item.origText)109+ return &orig110 }111 str := string(item.text)112 return &str113 }154 unmodified linesmodified:src/terminal.go+5 added-3 removed
40 unmodified lines41 printQuery bool42 history *History43 cycle bool44 header []string45+ ansi bool46 margin [4]string47 marginInt [4]int48 count int49 progress int157 unmodified lines207 margin: opts.Margin,208 marginInt: [4]int{0, 0, 0, 0},209 cycle: opts.Cycle,210 header: opts.Header,211+ ansi: opts.Ansi,212 reading: true,213 merger: EmptyMerger,214 selected: make(map[uint32]selectedItem),215 reqBox: util.NewEventBox(),73 unmodified lines289 }290 if len(t.selected) == 0 {291 cnt := t.merger.Length()292 if cnt > 0 && cnt > t.cy {291- fmt.Println(t.merger.Get(t.cy).AsString())293+ fmt.Println(t.merger.Get(t.cy).AsString(t.ansi))294 }295 } else {296 sels := make([]selectedItem, 0, len(t.selected))297 for _, sel := range t.selected {508 unmodified lines806 }807 }808 selectItem := func(item *Item) bool {809 if _, found := t.selected[item.index]; !found {808- t.selected[item.index] = selectedItem{time.Now(), item.StringPtr()}810+ t.selected[item.index] = selectedItem{time.Now(), item.StringPtr(t.ansi)}811 return true812 }813 return false814 }29 unmodified lines844 case actIgnore:845 case actExecute:846 if t.cy >= 0 && t.cy < t.merger.Length() {847 item := t.merger.Get(t.cy)846- executeCommand(t.execmap[mapkey], item.AsString())848+ executeCommand(t.execmap[mapkey], item.AsString(t.ansi))849 }850 case actInvalid:851 t.mutex.Unlock()852 continue256 unmodified linesmodified:test/test_go.rb+6 added
782 unmodified lines783 writelines tempname, ["\x1b[33mhello \x1b[34;1mworld\x1b[m ", 'byebye']784 assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi`.chomp785 end786 787+ def test_with_nth_no_ansi788+ src = "\x1b[33mhello \x1b[34;1mworld\x1b[m "789+ writelines tempname, [src, 'byebye']790+ assert_equal src, `cat #{tempname} | #{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi`.chomp791+ end792+793 private794 def writelines path, lines795 File.unlink path while File.exists? path796 File.open(path, 'w') { |f| f << lines.join($/) }237 unmodified lines