Skip to content
This repository was archived by the owner on May 3, 2021. It is now read-only.

Commit 7d29ae2

Browse files
authored
Sort messages in order. (#20)
This closes #16, by sorting messages in the maildir based upon their filepath age.
1 parent 5efb2ec commit 7d29ae2

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

finder/finder.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,20 @@ func New(prefix string) *Finder {
3333
// We exclude non-files, and ignore $path/tmp/.
3434
func (f *Finder) Messages(path string) []string {
3535

36-
// TODO use a struct
37-
// filename string
38-
// info os.FileInfo
36+
// We want to sort the files by age.
3937
//
40-
//Then we can sort them like so:
41-
//
42-
// sort.Slice(files, func(i,j int) bool{
43-
// return files[i].ModTime().Unix() < files[j].ModTime().Unix()
44-
// })
45-
//
46-
// Doing this is better than getting all the info a second
47-
// time, once we've found the filenames
38+
// Calling stat is expensive, but the appropriate
39+
// details are already retrieved by our filesystem
40+
// walker - so we store them alongside the names
41+
// as we receive them.
4842
//
43+
type MessageInfo struct {
44+
path string
45+
info os.FileInfo
46+
}
4947

50-
// Build the list of message filenames here.
51-
var files []string
48+
// The holder for the messages we find.
49+
var files []MessageInfo
5250

5351
// Directories we examine beneath the maildir
5452
dirs := []string{"cur", "new"}
@@ -65,14 +63,26 @@ func (f *Finder) Messages(path string) []string {
6563
// We only care about files
6664
mode := f.Mode()
6765
if mode.IsRegular() {
68-
files = append(files, path)
66+
files = append(files, MessageInfo{path: path, info: f})
6967
}
7068

7169
return nil
7270
})
7371
}
7472

75-
return files
73+
// Sort the files
74+
sort.Slice(files, func(i, j int) bool {
75+
return files[i].info.ModTime().Unix() < files[j].info.ModTime().Unix()
76+
})
77+
78+
// Now build up a return value of just the filenames,
79+
// which have been sorted by modification time.
80+
ret := make([]string, len(files))
81+
for i, e := range files {
82+
ret[i] = e.path
83+
}
84+
85+
return ret
7686
}
7787

7888
// Maildirs returns the list of Maildir folders beneath our prefix.

0 commit comments

Comments
 (0)