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,
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)
}

View File

@ -94,16 +94,20 @@ 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 {
if stripAnsi {
trimmed, _, _ := extractColor(string(*item.origText), nil)
return &trimmed
}
orig := string(*item.origText)
return &orig
}
str := string(item.text)
return &str
}

View File

@ -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()

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
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