/
summary.go
93 lines (77 loc) · 1.78 KB
/
summary.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cmd
import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/umlx5h/gtrash/internal/glog"
"github.com/umlx5h/gtrash/internal/trash"
)
type summaryCmd struct {
cmd *cobra.Command
opts summaryOptions
}
type summaryOptions struct{}
func newSummaryCmd() *summaryCmd {
root := &summaryCmd{}
cmd := &cobra.Command{
Use: "summary",
Short: "Show summary of all trash cans (s)",
Aliases: []string{"s"},
Long: `Description:
Displays statistics summarizing all trash cans.
Shows the count of files (and folders) and their total size.
When multiple trash cans are detected, the statistics for each and the total are displayed.`,
SilenceUsage: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, _ []string) error {
if err := summaryCmdRun(root.opts); err != nil {
return err
}
if glog.ExitCode() > 0 {
return errContinue
}
return nil
},
}
root.cmd = cmd
return root
}
func summaryCmdRun(_ summaryOptions) error {
box := trash.NewBox(
trash.WithGetSize(true),
)
if err := box.Open(); err != nil {
return err
}
var (
totalSize int64
totalItem int
)
for i, trashDir := range box.TrashDirs {
var (
size int64
item int
)
for _, f := range box.FilesByTrashDir[trashDir] {
item++
if f.Size != nil {
size += *f.Size
}
}
fmt.Printf("[%s]\n", trashDir)
fmt.Printf("item: %d\n", item)
fmt.Printf("size: %s\n", humanize.Bytes(uint64(size)))
if i != len(box.TrashDirs)-1 {
fmt.Println("")
}
totalSize += size
totalItem += item
}
if len(box.TrashDirs) > 1 {
fmt.Printf("\n[total]\n")
fmt.Printf("item: %d\n", totalItem)
fmt.Printf("size: %s\n", humanize.Bytes(uint64(totalSize)))
}
return nil
}