Tweak so that we save the control sequence in Match.Buffer()

Using #167 as a basis, we save the original control sequence, and
only strip it when we display to peco. This also has the added
benefit that Match.Line() is used for matching, so we can effectively
match the line w/o the control sequence
This commit is contained in:
Daisuke Maki 2014-07-24 20:36:17 +09:00
parent 3d581cedce
commit b22cb7b8f6
3 changed files with 26 additions and 20 deletions

View file

@ -8,6 +8,14 @@ import (
"strings"
)
// Global var used to strips ansi sequences
var ansiStrips = regexp.MustCompile("\x1B\\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)?[m|K]")
// Function who strips ansi sequences
func stripANSISequence(s string) string {
return ansiStrips.ReplaceAllString(s, "")
}
// Match defines the interface for matches. Note that to make drawing easier,
// we have a DidMatch and NoMatch types instead of using []Match and []string.
type Match interface {
@ -18,14 +26,16 @@ type Match interface {
}
type matchString struct {
buf string
sepLoc int
buf string
sepLoc int
displayLine string
}
func newMatchString(v string, enableSep bool) *matchString {
m := &matchString{
v,
-1,
"",
}
if !enableSep {
return m
@ -47,10 +57,16 @@ func (m matchString) Buffer() string {
}
func (m matchString) Line() string {
if i := m.sepLoc; i > -1 {
return m.buf[:i]
if m.displayLine != "" {
return m.displayLine
}
return m.buf
if i := m.sepLoc; i > -1 {
m.displayLine = stripANSISequence(m.buf[:i])
} else {
m.displayLine = stripANSISequence(m.buf)
}
return m.displayLine
}
func (m matchString) Output() string {

View file

@ -3,20 +3,20 @@ package peco
import "testing"
// some little test to validate ansi strips function
func TestAnsiStrips(t *testing.T) {
test := StripsAnsiSequences("this is not a pipe")
func TestANSIColorStrip(t *testing.T) {
test := stripANSISequence("this is not a pipe")
if test != "this is not a pipe" {
t.Errorf("expected String = 'this is not a pipe', got '%s'", test)
}
test = StripsAnsiSequences(" [01;34helloWorld [0m")
test = stripANSISequence(" [01;34helloWorld [0m")
if test != " [01;34helloWorld [0m" {
t.Errorf("expected String = ' [01;34mhelloWorld [0m', got '%s'", test)
}
test = StripsAnsiSequences("\x1b[01;34mthe answer to life is \x1b[0;42m42")
test = stripANSISequence("\x1b[01;34mthe answer to life is \x1b[0;42m42")
if test != "the answer to life is 42" {
t.Errorf("expected String = 'the answer to life is 42' , got '%s'", test)
}
test = StripsAnsiSequences("x1b[01;34mthe answer to life is x1b[0;42m42")
test = stripANSISequence("x1b[01;34mthe answer to life is x1b[0;42m42")
if test != "x1b[01;34mthe answer to life is x1b[0;42m42" {
t.Errorf("expected String = 'x1b[01;34mthe answer to life is x1b[0;42m42' , got '%s'", test)
}

View file

@ -5,14 +5,10 @@ import (
"fmt"
"io"
"os"
"regexp"
"sync"
"time"
)
// Global var used to strips ansi sequences
var ansiStrips = regexp.MustCompile("\x1B\\[(?:[0-9]{1,2}(?:;[0-9]{1,2})?)?[m|K]")
// BufferReader reads lines from the input, either Stdin or a file.
// If the incoming data is endless, it keeps reading and adding to
// the search buffer, as long as it can.
@ -66,7 +62,6 @@ func (b *BufferReader) Loop() {
}
if line != "" {
line = StripsAnsiSequences(line)
once.Do(func() { b.inputReadyCh <- struct{}{} })
m.Lock()
b.lines = append(b.lines, NewNoMatch(line, b.enableSep))
@ -100,8 +95,3 @@ func (b *BufferReader) Loop() {
fmt.Fprintf(os.Stderr, "No buffer to work with was available")
}
}
// Function who strips ansi sequences
func StripsAnsiSequences(s string) string {
return ansiStrips.ReplaceAllString(s, "")
}