| 1 |
package fzf |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
|
| 6 |
"github.com/junegunn/fzf/src/util" |
| 7 |
) |
| 8 |
|
| 9 |
type transformed struct { |
| 10 |
// Because nth can be changed dynamically by change-nth action, we need to |
| 11 |
// keep the revision number at the time of transformation. |
| 12 |
revision revision |
| 13 |
tokens []Token |
| 14 |
} |
| 15 |
|
| 16 |
// Item represents each input line. 56 bytes. |
| 17 |
type Item struct { |
| 18 |
text util.Chars // 32 = 24 + 1 + 1 + 2 + 4 |
| 19 |
transformed *transformed // 8 |
| 20 |
origText *[]byte // 8 |
| 21 |
colors *[]ansiOffset // 8 |
| 22 |
} |
| 23 |
|
| 24 |
// Index returns ordinal index of the Item |
| 25 |
func (item *Item) Index() int32 { |
| 26 |
return item.text.Index |
| 27 |
} |
| 28 |
|
| 29 |
var minItem = Item{text: util.Chars{Index: math.MinInt32}} |
| 30 |
|
| 31 |
func (item *Item) TrimLength() uint16 { |
| 32 |
return item.text.TrimLength() |
| 33 |
} |
| 34 |
|
| 35 |
// Colors returns ansiOffsets of the Item |
| 36 |
func (item *Item) Colors() []ansiOffset { |
| 37 |
if item.colors == nil { |
| 38 |
return []ansiOffset{} |
| 39 |
} |
| 40 |
return *item.colors |
| 41 |
} |
| 42 |
|
| 43 |
// AsString returns the original string |
| 44 |
func (item *Item) AsString(stripAnsi bool) string { |
| 45 |
if item.origText != nil { |
| 46 |
if stripAnsi { |
| 47 |
trimmed, _, _ := extractColor(string(*item.origText), nil, nil) |
| 48 |
return trimmed |
| 49 |
} |
| 50 |
return string(*item.origText) |
| 51 |
} |
| 52 |
return item.text.ToString() |
| 53 |
} |
| 54 |
|
| 55 |
func (item *Item) acceptNth(stripAnsi bool, delimiter Delimiter, transformer func([]Token, int32) string) string { |
| 56 |
tokens := Tokenize(item.AsString(stripAnsi), delimiter) |
| 57 |
transformed := transformer(tokens, item.Index()) |
| 58 |
return StripLastDelimiter(transformed, delimiter) |
| 59 |
} |