Repository navigation
Expand file tree
/
Copy pathpredicates.go
More file actions
127 lines (112 loc) · 3.75 KB
/
Copy pathpredicates.go
File metadata and controls
127 lines (112 loc) · 3.75 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package treeview
import (
"regexp"
"strings"
)
// Common predicate functions for use with DefaultNodeProvider rules.
// These functions return predicates that can be used with WithIconRule,
// WithStyleRule, and other provider options.
// PredHasExtension returns a predicate that checks if a node's name ends with any of the given extensions.
func PredHasExtension[T any](extensions ...string) func(*Node[T]) bool {
return func(n *Node[T]) bool {
name := strings.ToLower(n.Name())
for _, ext := range extensions {
if strings.HasSuffix(name, ext) {
return true
}
}
return false
}
}
// PredIsDir returns a predicate that checks if a node represents a directory.
// This works by checking if the node's data implements the IsDir() bool method.
func PredIsDir[T any]() func(*Node[T]) bool {
return func(n *Node[T]) bool {
if dirNode, ok := any(*n.Data()).(interface{ IsDir() bool }); ok {
return dirNode.IsDir()
}
return false
}
}
// PredIsFile returns a predicate that checks if a node represents a file.
// This is the inverse of PredIsDir.
func PredIsFile[T any]() func(*Node[T]) bool {
return PredNot(PredIsDir[T]())
}
// PredIsHidden returns a predicate that checks if a node's name starts with a dot,
// indicating it's a hidden file or directory.
func PredIsHidden[T any]() func(*Node[T]) bool {
return func(n *Node[T]) bool {
return strings.HasPrefix(n.Name(), ".")
}
}
// PredIsExpanded returns a predicate that checks if a node is currently expanded.
func PredIsExpanded[T any]() func(*Node[T]) bool {
return func(n *Node[T]) bool {
return n.IsExpanded()
}
}
// PredIsCollapsed returns a predicate that checks if a node is currently collapsed.
func PredIsCollapsed[T any]() func(*Node[T]) bool {
return func(n *Node[T]) bool {
return !n.IsExpanded()
}
}
// PredHasName returns a predicate that checks if a node's name equals the given name.
// The comparison is case-sensitive.
func PredHasName[T any](name string) func(*Node[T]) bool {
return func(n *Node[T]) bool {
return n.Name() == name
}
}
// PredHasNameIgnoreCase returns a predicate that checks if a node's name equals the given name.
// The comparison is case-insensitive.
func PredHasNameIgnoreCase[T any](name string) func(*Node[T]) bool {
return func(n *Node[T]) bool {
return strings.EqualFold(n.Name(), name)
}
}
// PredContainsText returns a predicate that checks if a node's name contains the given text.
// The comparison is case-insensitive.
func PredContainsText[T any](text string) func(*Node[T]) bool {
return func(n *Node[T]) bool {
return strings.Contains(strings.ToLower(n.Name()), strings.ToLower(text))
}
}
// PredMatchesRegex returns a predicate that checks if a node's name matches the compiled regex pattern.
// Pass a compiled regexp.Regexp for efficient repeated matching.
func PredMatchesRegex[T any](pattern *regexp.Regexp) func(*Node[T]) bool {
return func(n *Node[T]) bool {
return pattern.MatchString(n.Name())
}
}
// PredAny combines multiple predicates with logical OR.
// At least one predicate must return true for the combined predicate to return true.
func PredAny[T any](predicates ...func(*Node[T]) bool) func(*Node[T]) bool {
return func(n *Node[T]) bool {
for _, pred := range predicates {
if pred(n) {
return true
}
}
return false
}
}
// PredAll combines multiple predicates with logical AND.
// All predicates must return true for the combined predicate to return true.
func PredAll[T any](predicates ...func(*Node[T]) bool) func(*Node[T]) bool {
return func(n *Node[T]) bool {
for _, pred := range predicates {
if !pred(n) {
return false
}
}
return true
}
}
// PredNot negates a predicate.
func PredNot[T any](predicate func(*Node[T]) bool) func(*Node[T]) bool {
return func(n *Node[T]) bool {
return !predicate(n)
}
}