Add a but: glob qualifier to exclude paths.

This commit is contained in:
Qi Xiao 2017-02-16 04:10:31 +00:00
parent 79350d7b9c
commit 8db13b85ec
2 changed files with 14 additions and 3 deletions

View File

@ -101,7 +101,7 @@ func cat(lhs, rhs Value) Value {
segs := stringToSegments(string(lhs))
// We know rhs contains exactly one segment.
segs = append(segs, rhs.Segments[0])
return GlobPattern{glob.Pattern{segs, ""}, rhs.Flags}
return GlobPattern{glob.Pattern{segs, ""}, rhs.Flags, rhs.Buts}
}
case GlobPattern:
// NOTE Modifies lhs in place.
@ -113,6 +113,7 @@ func cat(lhs, rhs Value) Value {
// We know rhs contains exactly one segment.
lhs.append(rhs.Segments[0])
lhs.Flags |= rhs.Flags
lhs.Buts = append(lhs.Buts, rhs.Buts...)
return lhs
}
}
@ -260,7 +261,7 @@ func (cp *compiler) primary(n *parse.Primary) ValuesOpFunc {
case parse.Wildcard:
vs := []Value{GlobPattern{
glob.Pattern{[]glob.Segment{wildcardToSegment(n.SourceText())}, ""},
0}}
0, nil}}
return func(ec *EvalCtx) []Value {
return vs
}

View File

@ -15,6 +15,7 @@ import (
type GlobPattern struct {
glob.Pattern
Flags GlobFlag
Buts []string
}
type GlobFlag uint
@ -72,6 +73,8 @@ func (gp GlobPattern) Index(modifiers []Value) []Value {
switch {
case modifier == "nomatch-ok":
gp.Flags |= NoMatchOK
case strings.HasPrefix(modifier, "but:"):
gp.Buts = append(gp.Buts, modifier[len("but:"):])
case modifier == "match-hidden":
lastSeg := gp.mustGetLastWildSeg()
gp.Segments[len(gp.Segments)-1] = glob.Wild{
@ -171,6 +174,11 @@ func stringToSegments(s string) []glob.Segment {
}
func doGlob(gp GlobPattern, abort <-chan struct{}) []Value {
but := make(map[string]struct{})
for _, s := range gp.Buts {
but[s] = struct{}{}
}
vs := make([]Value, 0)
if !gp.Glob(func(name string) bool {
select {
@ -179,7 +187,9 @@ func doGlob(gp GlobPattern, abort <-chan struct{}) []Value {
return false
default:
}
vs = append(vs, String(name))
if _, b := but[name]; !b {
vs = append(vs, String(name))
}
return true
}) {
throw(ErrInterrupted)