106 lines
2.1 KiB
Go
106 lines
2.1 KiB
Go
package main
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"sort"
|
||
|
|
"strings"
|
||
|
|
"text/tabwriter"
|
||
|
|
|
||
|
|
"mcquay.me/arrange"
|
||
|
|
)
|
||
|
|
|
||
|
|
// stats tallies a run without recording any file names, so the summary is
|
||
|
|
// safe to share.
|
||
|
|
type stats struct {
|
||
|
|
total int
|
||
|
|
dupes int
|
||
|
|
moved int
|
||
|
|
errors int
|
||
|
|
bytes int64
|
||
|
|
|
||
|
|
ext map[string]*tally
|
||
|
|
year map[int]*tally
|
||
|
|
}
|
||
|
|
|
||
|
|
type tally struct {
|
||
|
|
files, dupes int
|
||
|
|
bytes int64
|
||
|
|
}
|
||
|
|
|
||
|
|
func newStats() *stats {
|
||
|
|
return &stats{ext: map[string]*tally{}, year: map[int]*tally{}}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *stats) add(m arrange.Media, dup bool) {
|
||
|
|
s.total++
|
||
|
|
s.bytes += m.Size
|
||
|
|
e := s.ext[m.Extension]
|
||
|
|
if e == nil {
|
||
|
|
e = &tally{}
|
||
|
|
s.ext[m.Extension] = e
|
||
|
|
}
|
||
|
|
y := s.year[m.Time.Year()]
|
||
|
|
if y == nil {
|
||
|
|
y = &tally{}
|
||
|
|
s.year[m.Time.Year()] = y
|
||
|
|
}
|
||
|
|
e.files++
|
||
|
|
e.bytes += m.Size
|
||
|
|
y.files++
|
||
|
|
y.bytes += m.Size
|
||
|
|
if dup {
|
||
|
|
s.dupes++
|
||
|
|
e.dupes++
|
||
|
|
y.dupes++
|
||
|
|
} else {
|
||
|
|
s.moved++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *stats) write(w io.Writer, verb string) {
|
||
|
|
fmt.Fprintf(w, "total: %d files, %s\n", s.total, human(s.bytes))
|
||
|
|
fmt.Fprintf(w, "%s: %d\n", verb, s.moved)
|
||
|
|
fmt.Fprintf(w, "dupes: %d\n", s.dupes)
|
||
|
|
fmt.Fprintf(w, "errors: %d\n", s.errors)
|
||
|
|
|
||
|
|
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)
|
||
|
|
fmt.Fprintln(tw, "\text\tfiles\tdupes\tsize\t")
|
||
|
|
exts := make([]string, 0, len(s.ext))
|
||
|
|
for k := range s.ext {
|
||
|
|
exts = append(exts, k)
|
||
|
|
}
|
||
|
|
sort.Strings(exts)
|
||
|
|
for _, k := range exts {
|
||
|
|
t := s.ext[k]
|
||
|
|
fmt.Fprintf(tw, "\t%s\t%d\t%d\t%s\t\n", strings.TrimPrefix(k, "."), t.files, t.dupes, human(t.bytes))
|
||
|
|
}
|
||
|
|
tw.Flush()
|
||
|
|
|
||
|
|
tw = tabwriter.NewWriter(w, 0, 0, 2, ' ', tabwriter.AlignRight)
|
||
|
|
fmt.Fprintln(tw, "\tyear\tfiles\tdupes\tsize\t")
|
||
|
|
years := make([]int, 0, len(s.year))
|
||
|
|
for k := range s.year {
|
||
|
|
years = append(years, k)
|
||
|
|
}
|
||
|
|
sort.Ints(years)
|
||
|
|
for _, k := range years {
|
||
|
|
t := s.year[k]
|
||
|
|
fmt.Fprintf(tw, "\t%d\t%d\t%d\t%s\t\n", k, t.files, t.dupes, human(t.bytes))
|
||
|
|
}
|
||
|
|
tw.Flush()
|
||
|
|
}
|
||
|
|
|
||
|
|
func human(n int64) string {
|
||
|
|
const unit = 1024
|
||
|
|
if n < unit {
|
||
|
|
return fmt.Sprintf("%dB", n)
|
||
|
|
}
|
||
|
|
div, exp := int64(unit), 0
|
||
|
|
for m := n / unit; m >= unit; m /= unit {
|
||
|
|
div *= unit
|
||
|
|
exp++
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("%.1f%ciB", float64(n)/float64(div), "KMGTPE"[exp])
|
||
|
|
}
|