Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func Run(opts *Options) (int, error) {
eventBox.Set(EvtHeader, header)
return false
}
if opts.AnsiPreview {
rawCopy := make([]byte, len(data))
copy(rawCopy, data)
item.rawOrigText = &rawCopy
}
item.text, item.colors = ansiProcessor(data)
item.text.Index = itemIndex
itemIndex++
Expand Down Expand Up @@ -154,6 +159,11 @@ func Run(opts *Options) (int, error) {
item.text.TrimTrailingWhitespaces(int(maxColorOffset))
item.text.Index = itemIndex
item.origText = &data
if opts.AnsiPreview {
rawCopy := make([]byte, len(data))
copy(rawCopy, data)
item.rawOrigText = &rawCopy
}
itemIndex++
return true
})
Expand Down
10 changes: 7 additions & 3 deletions src/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ type transformed struct {
tokens []Token
}

// Item represents each input line. 56 bytes.
// Item represents each input line. 64 bytes.
type Item struct {
text util.Chars // 32 = 24 + 1 + 1 + 2 + 4
transformed *transformed // 8
origText *[]byte // 8
rawOrigText *[]byte // 8
colors *[]ansiOffset // 8
}

// Index returns ordinal index of the Item
// Index returns ordinal index of the ItemAdding rawText changes the struct size; the comment
func (item *Item) Index() int32 {
return item.text.Index
}
Expand All @@ -40,8 +41,11 @@ func (item *Item) Colors() []ansiOffset {
return *item.colors
}

// AsString returns the original string
// AsString returns the original string (prefers the raw field when not stripping)
func (item *Item) AsString(stripAnsi bool) string {
if item.rawOrigText != nil && !stripAnsi {
return string(*item.rawOrigText) // true raw ANSI
}
if item.origText != nil {
if stripAnsi {
trimmed, _, _ := extractColor(string(*item.origText), nil, nil)
Expand Down
9 changes: 8 additions & 1 deletion src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Usage: fzf [options]
--read0 Read input delimited by ASCII NUL characters
--print0 Print output delimited by ASCII NUL characters
--ansi Enable processing of ANSI color codes
--ansi-preview Pass ANSI color codes to preview command
--sync Synchronous search for multi-staged filtering

GLOBAL STYLE
Expand Down Expand Up @@ -660,6 +661,7 @@ type Options struct {
BlockProfile string
MutexProfile string
TtyDefault string
AnsiPreview bool
}

func filterNonEmpty(input []string) []string {
Expand Down Expand Up @@ -766,7 +768,8 @@ func defaultOptions() *Options {
WalkerSkip: []string{".git", "node_modules"},
TtyDefault: tui.DefaultTtyDevice,
Help: false,
Version: false}
Version: false,
AnsiPreview: false,}
}

func isDir(path string) bool {
Expand Down Expand Up @@ -2775,8 +2778,12 @@ func parseOptions(index *int, opts *Options, allArgs []string) error {
opts.Multi = 0
case "--ansi":
opts.Ansi = true
case "--ansi-preview":
opts.AnsiPreview = true
case "--no-ansi":
opts.Ansi = false
case "--no-ansi-preview":
opts.AnsiPreview = false
case "--no-mouse":
opts.Mouse = false
case "+c", "--no-color":
Expand Down
14 changes: 12 additions & 2 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ type Terminal struct {
scrollbar string
previewScrollbar string
ansi bool
ansiPreview bool
freezeLeft int
freezeRight int
nthAttr tui.Attr
Expand Down Expand Up @@ -1060,6 +1061,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
footer: opts.Footer,
header0: opts.Header,
ansi: opts.Ansi,
ansiPreview: opts.AnsiPreview,
freezeLeft: opts.FreezeLeft,
freezeRight: opts.FreezeRight,
nthAttr: opts.Theme.Nth.Attr,
Expand Down Expand Up @@ -4472,9 +4474,17 @@ func (t *Terminal) replacePlaceholderInInitialCommand(template string) (string,
}

func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list [3][]*Item) (string, []string) {
return t.replacePlaceholderWithPreview(template, forcePlus, input, list, false)
}

func (t *Terminal) replacePlaceholderWithPreview(template string, forcePlus bool, input string, list [3][]*Item, isPreview bool) (string, []string) {
stripAnsi := t.ansi
if isPreview && t.ansiPreview {
stripAnsi = false
}
return replacePlaceholder(replacePlaceholderParams{
template: template,
stripAnsi: t.ansi,
stripAnsi: stripAnsi,
delimiter: t.delimiter,
printsep: t.printsep,
forcePlus: forcePlus,
Expand Down Expand Up @@ -5268,7 +5278,7 @@ func (t *Terminal) Loop() error {
version++
// We don't display preview window if no match
if items[0] != nil {
command, tempFiles := t.replacePlaceholder(commandTemplate, false, query, items)
command, tempFiles := t.replacePlaceholderWithPreview(commandTemplate, false, query, items, true)
cmd := t.executor.ExecCommand(command, true)
cmd.Env = env

Expand Down