Sort By Access Time and Change Time (#226)
- Add access time and change time as sort by types. This is powered by github.com/djherbis/times. - Fall back to modification time if access time and change time cannot be determined. - Add `sa` and `sc` as default bindings for sort by access time and sort by change time respectively. - Add access time and change time to info types allowing them to be displayed by the file list in the ui
This commit is contained in:
1 parent
d13cc79bc4
commit
9515bd73d0
7 files changed
+56
-13
No files matched your search
@@ -13,6 +13,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
times "gopkg.in/djherbis/times.v1"
|
||||
)
|
||||
|
||||
type linkState byte
|
||||
@@ -25,9 +27,11 @@ const (
|
||||
|
||||
type file struct {
|
||||
os.FileInfo
|
||||
linkState linkState
|
||||
path string
|
||||
dirCount int
|
||||
linkState linkState
|
||||
path string
|
||||
dirCount int
|
||||
accessTime time.Time
|
||||
changeTime time.Time
|
||||
}
|
||||
|
||||
func readdir(path string) ([]*file, error) {
|
||||
@@ -62,11 +66,24 @@ func readdir(path string) ([]*file, error) {
|
||||
}
|
||||
}
|
||||
|
||||
ts := times.Get(lstat)
|
||||
at := ts.AccessTime()
|
||||
var ct time.Time
|
||||
// from times docs: ChangeTime() panics unless HasChangeTime() is true
|
||||
if ts.HasChangeTime() {
|
||||
ct = ts.ChangeTime()
|
||||
} else {
|
||||
// fall back to ModTime if ChangeTime cannot be determined
|
||||
ct = lstat.ModTime()
|
||||
}
|
||||
|
||||
files = append(files, &file{
|
||||
FileInfo: lstat,
|
||||
linkState: linkState,
|
||||
path: fpath,
|
||||
dirCount: -1,
|
||||
FileInfo: lstat,
|
||||
linkState: linkState,
|
||||
path: fpath,
|
||||
dirCount: -1,
|
||||
accessTime: at,
|
||||
changeTime: ct,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -124,6 +141,14 @@ func (dir *dir) sort() {
|
||||
sort.SliceStable(dir.files, func(i, j int) bool {
|
||||
return dir.files[i].ModTime().Before(dir.files[j].ModTime())
|
||||
})
|
||||
case atimeSort:
|
||||
sort.SliceStable(dir.files, func(i, j int) bool {
|
||||
return dir.files[i].accessTime.Before(dir.files[j].accessTime)
|
||||
})
|
||||
case ctimeSort:
|
||||
sort.SliceStable(dir.files, func(i, j int) bool {
|
||||
return dir.files[i].changeTime.Before(dir.files[j].changeTime)
|
||||
})
|
||||
}
|
||||
|
||||
if gOpts.sortType.option&reverseSort != 0 {
|
||||
|
||||
Reference in new issue
Block a user