-
Notifications
You must be signed in to change notification settings - Fork 5
/
textfilters.go
88 lines (75 loc) · 1.74 KB
/
textfilters.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
package imgui
import (
"strings"
)
// Helper: Growable text buffer for logging/accumulating text
// (this could be called 'ImGuiTextBuilder' / 'ImGuiStringBuilder')
type ImGuiTextBuffer []byte
// Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
type ImGuiTextFilter struct {
InputBuf []byte
Filters []string
CountGrep int
}
func NewImGuiTextFilter(default_filter string) ImGuiTextFilter {
if default_filter != "" {
filter := ImGuiTextFilter{InputBuf: []byte(default_filter)}
filter.Build()
return filter
}
return ImGuiTextFilter{}
}
func (this *ImGuiTextFilter) Draw(label string /*= "Filter (inc,-exc)"*/, width float) bool {
if width != 0.0 {
SetNextItemWidth(width)
}
var value_changed = InputText(label, &this.InputBuf, 0, nil, nil)
if value_changed {
this.Build()
}
return value_changed
}
// Helper calling InputText+Build
func (this *ImGuiTextFilter) PassFilter(text string) bool {
if len(this.Filters) == 0 {
return true
}
for _, f := range this.Filters {
if len(f) == 0 {
continue
}
if f[0] == '-' {
if strings.Contains(text, f[1:]) {
return false
}
} else {
// Grep
if strings.Contains(text, f) {
return false
}
}
}
// Implicit * grep
if this.CountGrep == 0 {
return true
}
return false
}
func (this *ImGuiTextFilter) Build() {
this.Filters = strings.Split(string(this.InputBuf), ",")
this.CountGrep = 0
for i := range this.Filters {
this.Filters[i] = strings.TrimSpace(this.Filters[i])
if len(this.Filters[i]) == 0 {
continue
}
if this.Filters[i][0] != '-' {
this.CountGrep += 1
}
}
}
func (this *ImGuiTextFilter) Clear() {
this.InputBuf[0] = 0
this.Build()
}
func (this *ImGuiTextFilter) IsActive() bool { return len(this.Filters) > 0 }