Skip to content

Commit

Permalink
feat: ✨ new time-type 'birth' for macOS
Browse files Browse the repository at this point in the history
using Birthtimespec

fix: 🐛 fix panic in '--tree -d'
  • Loading branch information
Equationzhao committed Sep 11, 2023
1 parent 3e7f7fe commit 76b3e73
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ func init() {
// remove non-display items
infos = itemFilter.Filter(infos...)

if tree {
infos[0].Cache["level"] = []byte("0")
}
goto final
}

Expand Down
16 changes: 14 additions & 2 deletions app/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ var viewFlag = []cli.Flag{
},
&cli.StringSliceFlag{
Name: "time-type",
Usage: "time type, mod(default), create, access, all",
Usage: "time type, mod(default), create, access, all, birth[macOS only]",
EnvVars: []string{"TIME_TYPE"},
Action: func(context *cli.Context, ss []string) error {
timeType = make([]string, 0, len(ss))
accepts := []string{"mod", "modified", "create", "cr", "access", "ac"}
accepts := []string{"mod", "modified", "create", "cr", "access", "ac", "birth"}
for _, s := range ss {
if slices.Contains(accepts, strings.ToLower(s)) {
timeType = append(timeType, s)
Expand Down Expand Up @@ -111,6 +111,18 @@ var viewFlag = []cli.Flag{
},
Category: "VIEW",
},
&cli.BoolFlag{
Name: "birth",
Usage: "birth time[macOS only]",
DisableDefaultText: true,
Action: func(context *cli.Context, b bool) error {
if b {
timeType = append(timeType, "birth")
}
return nil
},
Category: "VIEW",
},
&cli.StringFlag{
Name: "size-unit",
Aliases: []string{"su", "block-size"},
Expand Down
20 changes: 19 additions & 1 deletion filter/content/time.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package content

import (
"runtime"
"time"

"github.com/Equationzhao/g/filter"
Expand Down Expand Up @@ -33,6 +34,14 @@ func (r *RelativeTimeEnabler) Enable(renderer *render.Renderer) filter.ContentOp
case "access":
t = osbased.AccessTime(info)
timeType = timeAccessed
case "birth":
timeType = timeBirth
// if darwin, check birth time
if runtime.GOOS == "darwin" {
t = osbased.BirthTime(info)
} else {
t = osbased.CreateTime(info)
}
default:
t = osbased.ModTime(info)
timeType = timeModified
Expand All @@ -46,10 +55,11 @@ const (
timeModified = "Modified"
timeCreated = "Created"
timeAccessed = "Accessed"
timeBirth = "Birth"
)

// EnableTime enables time
// accepts ['mod', 'modified', 'create', 'access']
// accepts ['mod', 'modified', 'create', 'access', 'birth']
func EnableTime(format string, mode string, renderer *render.Renderer) filter.ContentOption {
return func(info *item.FileInfo) (string, string) {
// get mod time/ create time/ access time
Expand All @@ -65,6 +75,14 @@ func EnableTime(format string, mode string, renderer *render.Renderer) filter.Co
case "access", "ac":
t = osbased.AccessTime(info)
timeType = timeAccessed
case "birth":
timeType = timeBirth
// if darwin, check birth time
if runtime.GOOS == "darwin" {
t = osbased.BirthTime(info)
} else {
t = osbased.CreateTime(info)
}
default:
t = osbased.ModTime(info)
timeType = timeModified
Expand Down
5 changes: 5 additions & 0 deletions osbased/time_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ func CreateTime(a os.FileInfo) time.Time {
ctim := a.Sys().(*syscall.Stat_t).Ctimespec
return time.Unix(ctim.Sec, ctim.Nsec)
}

func BirthTime(a os.FileInfo) time.Time {
btim := a.Sys().(*syscall.Stat_t).Birthtimespec
return time.Unix(btim.Sec, btim.Nsec)
}
4 changes: 4 additions & 0 deletions osbased/time_linux_32.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func CreateTime(a os.FileInfo) time.Time {
ctim := a.Sys().(*syscall.Stat_t).Ctim
return time.Unix(int64(ctim.Sec), int64(ctim.Nsec))
}

func BirthTime(a os.FileInfo) time.Time {
return CreateTime(a)
}
4 changes: 4 additions & 0 deletions osbased/time_linux_64.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func CreateTime(a os.FileInfo) time.Time {
ctim := a.Sys().(*syscall.Stat_t).Ctim
return time.Unix(ctim.Sec, ctim.Nsec)
}

func BirthTime(a os.FileInfo) time.Time {
return CreateTime(a)
}
4 changes: 4 additions & 0 deletions osbased/time_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func CreateTime(a os.FileInfo) time.Time {
atim := a.Sys().(*syscall.Win32FileAttributeData).CreationTime
return time.Unix(0, atim.Nanoseconds())
}

func BirthTime(a os.FileInfo) time.Time {
return CreateTime(a)
}

0 comments on commit 76b3e73

Please sign in to comment.