Skip to content

Commit

Permalink
feat: 🚧 add before/after option
Browse files Browse the repository at this point in the history
  • Loading branch information
Equationzhao committed Sep 23, 2023
1 parent 5b39814 commit 7a3f385
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/filtering.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"time"

"github.com/Equationzhao/g/filter"
"github.com/Equationzhao/g/filter/content"
"github.com/gabriel-vasile/mimetype"
Expand Down Expand Up @@ -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
},
},
}
34 changes: 34 additions & 0 deletions filter/itemfliter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 7a3f385

Please sign in to comment.