diff --git a/app/filtering.go b/app/filtering.go index 5b434d8..29e5cdb 100644 --- a/app/filtering.go +++ b/app/filtering.go @@ -1,6 +1,8 @@ package app import ( + "time" + "github.com/Equationzhao/g/filter" "github.com/Equationzhao/g/filter/content" "github.com/gabriel-vasile/mimetype" @@ -210,4 +212,32 @@ var filteringFlag = []cli.Flag{ DisableDefaultText: true, Category: "FILTERING", }, + &cli.StringFlag{ + Name: "before", + Usage: "show items which was modified/access/created before given time, the time field is determined by --time-style/--time-type", + Category: "FILTERING", + Action: func(ctx *cli.Context, s string) error { + t, err := time.Parse(timeFormat, s) + if err != nil { + return err + } + f := filter.BeforeTime(t.Unix(), filter.WhichTimeFiled(timeType[0])) + itemFilterFunc = append(itemFilterFunc, &f) + return nil + }, + }, + &cli.StringFlag{ + Name: "after", + Usage: "show items which was modified/access/created after given time, the time field is determined by --time-style/--time-type", + Category: "FILTERING", + Action: func(ctx *cli.Context, s string) error { + t, err := time.Parse(timeFormat, s) + if err != nil { + return err + } + f := filter.AfterTime(t.Unix(), filter.WhichTimeFiled(timeType[0])) + itemFilterFunc = append(itemFilterFunc, &f) + return nil + }, + }, } diff --git a/filter/itemfliter.go b/filter/itemfliter.go index 3b395f9..6617678 100644 --- a/filter/itemfliter.go +++ b/filter/itemfliter.go @@ -3,10 +3,13 @@ package filter import ( "os" "path/filepath" + "runtime" "strings" + "time" "github.com/Equationzhao/g/git" "github.com/Equationzhao/g/item" + "github.com/Equationzhao/g/osbased" "github.com/gabriel-vasile/mimetype" "github.com/gobwas/glob" ) @@ -253,3 +256,34 @@ func RemoveMimeType(fileTypes ...string) ItemFilterFunc { return keep } } + +func BeforeTime(t int64, timeFunc func(os.FileInfo) time.Time) ItemFilterFunc { + return func(e *item.FileInfo) bool { + return timeFunc(e).Unix() < t + } +} + +func AfterTime(t int64, timeFunc func(os.FileInfo) time.Time) ItemFilterFunc { + return func(e *item.FileInfo) bool { + return timeFunc(e).Unix() > t + } +} + +func WhichTimeFiled(mod string) (t func(os.FileInfo) time.Time) { + switch mod { + case "mod": + t = osbased.ModTime + case "create": + t = osbased.CreateTime + case "access": + t = osbased.AccessTime + case "birth": + // if darwin, check birth time + if runtime.GOOS == "darwin" { + t = osbased.BirthTime + } else { + t = osbased.CreateTime + } + } + return +}