-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
94 lines (78 loc) · 2 KB
/
line.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package peco
import (
"strings"
"github.com/google/btree"
"github.com/peco/peco/internal/util"
)
// NewRawLine creates a new RawLine. The `enableSep` flag tells
// it if we should search for a null character to split the
// string to display and the string to emit upon selection of
// of said line
func NewRawLine(id uint64, v string, enableSep bool) *RawLine {
rl := &RawLine{
id: id,
buf: v,
sepLoc: -1,
displayString: "",
dirty: false,
}
if !enableSep {
return rl
}
if i := strings.IndexByte(rl.buf, '\000'); i != -1 {
rl.sepLoc = i
}
return rl
}
// Less implements the btree.Item interface
func (rl *RawLine) Less(b btree.Item) bool {
return rl.id < b.(Line).ID()
}
// ID returns the unique ID of this line
func (rl *RawLine) ID() uint64 {
return rl.id
}
// IsDirty returns true if this line must be redrawn on the terminal
func (rl RawLine) IsDirty() bool {
return rl.dirty
}
// SetDirty sets the dirty flag
func (rl *RawLine) SetDirty(b bool) {
rl.dirty = b
}
// Buffer returns the raw buffer. May contain null
func (rl RawLine) Buffer() string {
return rl.buf
}
// DisplayString returns the string to be displayed
func (rl RawLine) DisplayString() string {
if rl.displayString != "" {
return rl.displayString
}
if i := rl.sepLoc; i > -1 {
rl.displayString = util.StripANSISequence(rl.buf[:i])
} else {
rl.displayString = util.StripANSISequence(rl.buf)
}
return rl.displayString
}
// Output returns the string to be displayed *after peco is done
func (rl RawLine) Output() string {
if i := rl.sepLoc; i > -1 {
return rl.buf[i+1:]
}
return rl.buf
}
// Indices fulfills the Line interface, but for RawLine it always
// returns nil
func (rl RawLine) Indices() [][]int {
return nil
}
// NewMatchedLine creates a new MatchedLine
func NewMatchedLine(rl Line, matches [][]int) *MatchedLine {
return &MatchedLine{rl, matches}
}
// Indices returns the indices in the buffer that matched
func (ml MatchedLine) Indices() [][]int {
return ml.indices
}