Dev a few more operations
* Archive * Config * Pull out filter generation (from list) for reuse
This commit is contained in:
@@ -312,3 +312,73 @@ func BeginningOfMonth() time.Time {
|
||||
now := time.Now()
|
||||
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
}
|
||||
|
||||
func BuildFilterFromArgs(args []string) func(*timertxt.Timer) bool {
|
||||
start := time.Time{}
|
||||
end := time.Now()
|
||||
var contextFilters []string
|
||||
var projectFilters []string
|
||||
var allFilters []func(timertxt.Timer) bool
|
||||
if len(args) > 0 {
|
||||
contextFilters, args = GetContextsFromSlice(args)
|
||||
projectFilters, args = GetProjectsFromSlice(args)
|
||||
}
|
||||
if len(args) > 0 {
|
||||
var err error
|
||||
if start, err = ParseFuzzyTime(args[0]); err != nil {
|
||||
y, m, d := time.Now().Date()
|
||||
start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())
|
||||
} else {
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) > 0 {
|
||||
if end, err = ParseFuzzyTime(args[0]); err != nil {
|
||||
y, m, d := time.Now().Date()
|
||||
end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location())
|
||||
} else {
|
||||
args = args[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
allFilters = append(allFilters, func(t timertxt.Timer) bool {
|
||||
if t.StartDate.Before(end) && t.StartDate.After(start) {
|
||||
return true
|
||||
}
|
||||
if t.FinishDate.Before(end) && t.FinishDate.After(start) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
if len(contextFilters) > 0 {
|
||||
allFilters = append(allFilters, func(t timertxt.Timer) bool {
|
||||
for _, v := range contextFilters {
|
||||
v = strings.TrimPrefix(v, "@")
|
||||
if !t.HasContext(v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
if len(projectFilters) > 0 {
|
||||
allFilters = append(allFilters, func(t timertxt.Timer) bool {
|
||||
for _, v := range projectFilters {
|
||||
v = strings.TrimPrefix(v, "+")
|
||||
if !t.HasProject(v) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
doFilters := func(t *timertxt.Timer) bool {
|
||||
for _, v := range allFilters {
|
||||
if !v(*t) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
// If we made it all the way down here, it matches
|
||||
return true
|
||||
}
|
||||
return doFilters
|
||||
}
|
||||
|
Reference in New Issue
Block a user