Browse Source

cli rewrite

Pierre Curto 6 years ago
parent
commit
58f5d507bd
4 changed files with 58 additions and 6 deletions
  1. 4 1
      go.mod
  2. 11 0
      go.sum
  3. 26 2
      internal/cmds/compress.go
  4. 17 3
      internal/cmds/uncompress.go

+ 4 - 1
go.mod

@@ -1,3 +1,6 @@
 module github.com/pierrec/lz4
 module github.com/pierrec/lz4
 
 
-require code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c
+require (
+	code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c
+	github.com/schollz/progressbar/v2 v2.12.1
+)

+ 11 - 0
go.sum

@@ -1,2 +1,13 @@
 code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c h1:VzwteSWGbW9mxXTEkH+kpnao5jbgLynw3hq742juQh8=
 code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c h1:VzwteSWGbW9mxXTEkH+kpnao5jbgLynw3hq742juQh8=
 code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c/go.mod h1:wN/zk7mhREp/oviagqUXY3EwuHhWyOvAdsn5Y4CzOrc=
 code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c/go.mod h1:wN/zk7mhREp/oviagqUXY3EwuHhWyOvAdsn5Y4CzOrc=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
+github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/schollz/progressbar v1.0.0/go.mod h1:/l9I7PC3L3erOuz54ghIRKUEFcosiWfLvJv+Eq26UMs=
+github.com/schollz/progressbar/v2 v2.12.1 h1:0Ce7IBClG+s3lxXN1Noqwh7aToKGL5a3mnMfPJqDlv4=
+github.com/schollz/progressbar/v2 v2.12.1/go.mod h1:fBI3onORwtNtwCWJHsrXtjE3QnJOtqIZrvr3rDaF7L0=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

+ 26 - 2
internal/cmds/compress.go

@@ -7,13 +7,12 @@ import (
 	"os"
 	"os"
 
 
 	"code.cloudfoundry.org/bytefmt"
 	"code.cloudfoundry.org/bytefmt"
+	"github.com/schollz/progressbar/v2"
 
 
 	"github.com/pierrec/lz4"
 	"github.com/pierrec/lz4"
 	"github.com/pierrec/lz4/internal/cmdflag"
 	"github.com/pierrec/lz4/internal/cmdflag"
 )
 )
 
 
-//TODO add progress bar and stats
-
 // Compress compresses a set of files or from stdin to stdout.
 // Compress compresses a set of files or from stdin to stdout.
 func Compress(fs *flag.FlagSet) cmdflag.Handler {
 func Compress(fs *flag.FlagSet) cmdflag.Handler {
 	var blockMaxSize string
 	var blockMaxSize string
@@ -61,6 +60,27 @@ func Compress(fs *flag.FlagSet) cmdflag.Handler {
 			}
 			}
 			mode := finfo.Mode() // use the same mode for the output file
 			mode := finfo.Mode() // use the same mode for the output file
 
 
+			// Accumulate compressed bytes num.
+			var (
+				zsize int
+				size  = finfo.Size()
+			)
+			if size > 0 {
+				// Progress bar setup.
+				numBlocks := int(size) / zw.Header.BlockMaxSize
+				bar := progressbar.NewOptions(numBlocks,
+					// File transfers are usually slow, make sure we display the bar at 0%.
+					progressbar.OptionSetRenderBlankState(true),
+					// Display the filename.
+					progressbar.OptionSetDescription(filename),
+					progressbar.OptionClearOnFinish(),
+				)
+				zw.OnBlockDone = func(n int) {
+					_ = bar.Add(1)
+					zsize += n
+				}
+			}
+
 			// Output file.
 			// Output file.
 			zfilename := fmt.Sprintf("%s%s", filename, lz4.Extension)
 			zfilename := fmt.Sprintf("%s%s", filename, lz4.Extension)
 			zfile, err := os.OpenFile(zfilename, os.O_CREATE|os.O_WRONLY, mode)
 			zfile, err := os.OpenFile(zfilename, os.O_CREATE|os.O_WRONLY, mode)
@@ -80,6 +100,10 @@ func Compress(fs *flag.FlagSet) cmdflag.Handler {
 					return err
 					return err
 				}
 				}
 			}
 			}
+
+			if size > 0 {
+				fmt.Printf("%s %.02f%%\n", zfilename, float64(zsize)*100/float64(size))
+			}
 		}
 		}
 
 
 		return nil
 		return nil

+ 17 - 3
internal/cmds/uncompress.go

@@ -2,6 +2,7 @@ package cmds
 
 
 import (
 import (
 	"flag"
 	"flag"
+	"github.com/schollz/progressbar/v2"
 	"io"
 	"io"
 	"os"
 	"os"
 	"strings"
 	"strings"
@@ -10,8 +11,6 @@ import (
 	"github.com/pierrec/lz4/internal/cmdflag"
 	"github.com/pierrec/lz4/internal/cmdflag"
 )
 )
 
 
-//TODO add progress bar and stats
-
 // Uncompress uncompresses a set of files or from stdin to stdout.
 // Uncompress uncompresses a set of files or from stdin to stdout.
 func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
 func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
 	return func(args ...string) error {
 	return func(args ...string) error {
@@ -44,8 +43,23 @@ func Uncompress(_ *flag.FlagSet) cmdflag.Handler {
 			}
 			}
 			zr.Reset(zfile)
 			zr.Reset(zfile)
 
 
+			zfinfo, err := zfile.Stat()
+			if err != nil {
+				return err
+			}
+			var out io.Writer = file
+			if size := zfinfo.Size(); size > 0 {
+				out = progressbar.NewOptions(0,
+					// File transfers are usually slow, make sure we display the bar at 0%.
+					progressbar.OptionSetRenderBlankState(true),
+					// Display the filename.
+					progressbar.OptionSetDescription(filename),
+					progressbar.OptionClearOnFinish(),
+				)
+			}
+
 			// Uncompress.
 			// Uncompress.
-			_, err = io.Copy(file, zr)
+			_, err = io.Copy(out, zr)
 			if err != nil {
 			if err != nil {
 				return err
 				return err
 			}
 			}