From f0516b860041a1f413c946bdc2cdf1d52f57b4d0 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 16 Jun 2019 20:59:45 +0100 Subject: [PATCH] Fix indexing progress bar, improve question --- core/index.go | 72 +++++++++++++++++++++++++-------------------------- main.go | 2 +- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/core/index.go b/core/index.go index 21a7f29..4872b9e 100644 --- a/core/index.go +++ b/core/index.go @@ -158,15 +158,41 @@ func (in *Index) Refresh() error { // TODO: If needed, multithreaded hashing // for i := 0; i < runtime.NumCPU(); i++ {} - // Get fileinfos of pack.toml and index to compare them - // Case-sensitivity? + // Is case-sensitivity a problem? pathPF, _ := filepath.Abs(in.flags.PackFile) pathIndex, _ := filepath.Abs(in.indexFile) - progressLength := len(in.Files) - progressCurrent := 0 progressContainer := mpb.New() - progress := progressContainer.AddBar(int64(progressLength), + + // TODO: A method of specifying pack root directory? + // TODO: A method of excluding files + packRoot := filepath.Dir(in.flags.PackFile) + var fileList []string + + err := filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error { + if err != nil { + // TODO: Handle errors on individual files properly + return err + } + + // Exit if the files are the same as the pack/index files + absPath, _ := filepath.Abs(path) + if absPath == pathPF || absPath == pathIndex { + return nil + } + // Exit if this is a directory + if info.IsDir() { + return nil + } + + fileList = append(fileList, path) + return nil + }) + if err != nil { + return err + } + + progress := progressContainer.AddBar(int64(len(fileList)), mpb.PrependDecorators( // simple name decorator decor.Name("Refreshing index..."), @@ -182,43 +208,17 @@ func (in *Index) Refresh() error { ), ) - // TODO: A method of specifying pack root directory? - // TODO: A method of excluding files - packRoot := filepath.Dir(in.flags.PackFile) - err := filepath.Walk(packRoot, func(path string, info os.FileInfo, err error) error { + for _, v := range fileList { + start := time.Now() + + err := in.updateFile(v) if err != nil { - // TODO: Handle errors on individual files properly return err } - start := time.Now() - defer func() { - // TODO: this is stupid, traverse the file tree first *then* read all the files - if progressCurrent >= progressLength { - progressLength++ - progress.SetTotal(int64(progressLength), false) - } - progressCurrent++ - progress.Increment(time.Since(start)) - }() - // Exit if the files are the same as the pack/index files - absPath, _ := filepath.Abs(path) - if absPath == pathPF || absPath == pathIndex { - return nil - } - // Exit if this is a directory - if info.IsDir() { - return nil - } - - return in.updateFile(path) - }) - if err != nil { - return err + progress.Increment(time.Since(start)) } - progress.SetTotal(int64(progressLength), true) - // Check all the files exist, remove them if they don't i := 0 for _, file := range in.Files { diff --git a/main.go b/main.go index 4fc3065..5e2e2a3 100644 --- a/main.go +++ b/main.go @@ -207,7 +207,7 @@ func cmdUpdate(flags core.Flags, mod string) error { return nil } - fmt.Println("Do you want to update? [Y/n]: ") + fmt.Print("Do you want to update? [Y/n]: ") var answer string _, err := fmt.Scanln(&answer) if err != nil {