Add CLI progress bars to the packer tool
Since packing can be quite slow, it is nice to display progress to the caller. We use the excellent github.com/vbauerster/mpb library to do so, and add a bit of colour with github.com/logrusru/aurora. Finally, augment the inspector and packer with a summary printer that displays the overall file size/count, and compression ratio for each compression type.
This commit is contained in:
parent
984639d475
commit
6f532296ef
6 changed files with 273 additions and 12 deletions
|
|
@ -32,7 +32,6 @@ var inspectCmd = &cobra.Command{
|
|||
|
||||
// Inspect a packfile.
|
||||
// TODO: verify etag; verify integrity of compressed data.
|
||||
// TODO: skip Gzip/Brotli if not present; print ratio.
|
||||
func Inspect(filename string) error {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
|
|
@ -65,10 +64,46 @@ func Inspect(filename string) error {
|
|||
printSize(info.Brotli.Length), info.Brotli.Offset)
|
||||
}
|
||||
}
|
||||
inspectSummary(dir)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func inspectSummary(dir *packed.Directory) {
|
||||
var (
|
||||
n, ngzip, nbrotli int
|
||||
s, sgzip, sbrotli uint64
|
||||
)
|
||||
|
||||
for _, f := range dir.Files {
|
||||
n++
|
||||
s += f.Uncompressed.Length
|
||||
if f.Gzip != nil {
|
||||
ngzip++
|
||||
sgzip += f.Gzip.Length
|
||||
}
|
||||
if f.Brotli != nil {
|
||||
nbrotli++
|
||||
sbrotli += f.Brotli.Length
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Uncompressed:\n\tFiles: %d\n\tSize: %s\n",
|
||||
n, printSize(s))
|
||||
if ngzip > 0 {
|
||||
fmt.Printf("gzip compressed:\n\tFiles: %d (%.1f%% of total)\n"+
|
||||
"\tSize: %s\n\tRatio: %.1f%%\n",
|
||||
ngzip, 100*float64(ngzip)/float64(n),
|
||||
printSize(sgzip), 100*float64(sgzip)/float64(s))
|
||||
}
|
||||
if nbrotli > 0 {
|
||||
fmt.Printf("brotli compressed:\n\tFiles: %d (%.1f%% of total)\n"+
|
||||
"\tSize: %s\n\tRatio: %.1f%%\n",
|
||||
nbrotli, 100*float64(nbrotli)/float64(n),
|
||||
printSize(sbrotli), 100*float64(sbrotli)/float64(s))
|
||||
}
|
||||
}
|
||||
|
||||
func printSize(size uint64) string {
|
||||
switch {
|
||||
case size < 1<<10:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue