-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.go
More file actions
70 lines (64 loc) · 1.19 KB
/
list.go
File metadata and controls
70 lines (64 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
func doList() {
if err := ensureCacheDir(); err != nil {
fatal(err.Error())
}
entries, err := os.ReadDir(cacheDir())
if err != nil {
fatal(err.Error())
}
// Sort by name (which is timestamp-based)
sort.Slice(entries, func(i, j int) bool {
return entries[i].Name() < entries[j].Name()
})
found := false
now := time.Now()
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".txt") {
continue
}
found = true
id := strings.TrimSuffix(e.Name(), ".txt")
path := filepath.Join(cacheDir(), e.Name())
lines := countLines(path)
info, err := e.Info()
ageStr := "unknown"
if err == nil {
secs := int64(now.Sub(info.ModTime()).Seconds())
ageStr = formatAge(secs)
}
fmt.Printf("%s\t%d lines\t%s\n", id, lines, ageStr)
}
if !found {
fmt.Println("No stored captures.")
}
}
func countLines(path string) int {
f, err := os.Open(path)
if err != nil {
return 0
}
defer f.Close()
count := 0
buf := make([]byte, 32*1024)
for {
n, err := f.Read(buf)
for i := 0; i < n; i++ {
if buf[i] == '\n' {
count++
}
}
if err != nil {
break
}
}
return count
}