Should not strip ANSI codes when --ansi is not set

This commit is contained in:
Junegunn Choi 2015-08-28 21:23:10 +09:00
parent 698e8008df
commit 90b0cd44ac
4 changed files with 22 additions and 10 deletions

View File

@ -174,7 +174,7 @@ func Run(opts *Options) {
chunks: snapshot, chunks: snapshot,
pattern: pattern}) pattern: pattern})
for i := 0; i < merger.Length(); i++ { for i := 0; i < merger.Length(); i++ {
fmt.Println(merger.Get(i).AsString()) fmt.Println(merger.Get(i).AsString(opts.Ansi))
} }
} }
os.Exit(0) os.Exit(0)
@ -250,7 +250,7 @@ func Run(opts *Options) {
fmt.Println() fmt.Println()
} }
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
fmt.Println(val.Get(i).AsString()) fmt.Println(val.Get(i).AsString(opts.Ansi))
} }
os.Exit(0) os.Exit(0)
} }

View File

@ -94,16 +94,20 @@ func (item *Item) Rank(cache bool) Rank {
} }
// AsString returns the original string // AsString returns the original string
func (item *Item) AsString() string { func (item *Item) AsString(stripAnsi bool) string {
return *item.StringPtr() return *item.StringPtr(stripAnsi)
} }
// StringPtr returns the pointer to the original string // StringPtr returns the pointer to the original string
func (item *Item) StringPtr() *string { func (item *Item) StringPtr(stripAnsi bool) *string {
if item.origText != nil { if item.origText != nil {
if stripAnsi {
trimmed, _, _ := extractColor(string(*item.origText), nil) trimmed, _, _ := extractColor(string(*item.origText), nil)
return &trimmed return &trimmed
} }
orig := string(*item.origText)
return &orig
}
str := string(item.text) str := string(item.text)
return &str return &str
} }

View File

@ -42,6 +42,7 @@ type Terminal struct {
history *History history *History
cycle bool cycle bool
header []string header []string
ansi bool
margin [4]string margin [4]string
marginInt [4]int marginInt [4]int
count int count int
@ -207,6 +208,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
marginInt: [4]int{0, 0, 0, 0}, marginInt: [4]int{0, 0, 0, 0},
cycle: opts.Cycle, cycle: opts.Cycle,
header: opts.Header, header: opts.Header,
ansi: opts.Ansi,
reading: true, reading: true,
merger: EmptyMerger, merger: EmptyMerger,
selected: make(map[uint32]selectedItem), selected: make(map[uint32]selectedItem),
@ -288,7 +290,7 @@ func (t *Terminal) output() {
if len(t.selected) == 0 { if len(t.selected) == 0 {
cnt := t.merger.Length() cnt := t.merger.Length()
if cnt > 0 && cnt > t.cy { 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 { } else {
sels := make([]selectedItem, 0, len(t.selected)) sels := make([]selectedItem, 0, len(t.selected))
@ -805,7 +807,7 @@ func (t *Terminal) Loop() {
} }
selectItem := func(item *Item) bool { selectItem := func(item *Item) bool {
if _, found := t.selected[item.index]; !found { 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 true
} }
return false return false
@ -843,7 +845,7 @@ func (t *Terminal) Loop() {
case actExecute: case actExecute:
if t.cy >= 0 && t.cy < t.merger.Length() { if t.cy >= 0 && t.cy < t.merger.Length() {
item := t.merger.Get(t.cy) item := t.merger.Get(t.cy)
executeCommand(t.execmap[mapkey], item.AsString()) executeCommand(t.execmap[mapkey], item.AsString(t.ansi))
} }
case actInvalid: case actInvalid:
t.mutex.Unlock() t.mutex.Unlock()

View File

@ -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 assert_equal 'hello world ', `cat #{tempname} | #{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi`.chomp
end 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 private
def writelines path, lines def writelines path, lines
File.unlink path while File.exists? path File.unlink path while File.exists? path