diff options
| author | Přemysl Eric Janouch <p@janouch.name> | 2023-12-10 09:12:34 +0100 | 
|---|---|---|
| committer | Přemysl Eric Janouch <p@janouch.name> | 2023-12-10 09:12:34 +0100 | 
| commit | e20f93aa6714868141159d8d855c11ef704e5507 (patch) | |
| tree | f675b2d13653a41bafdc548cccc263d6a0da06cb | |
| parent | 32a4282a018be501628781088a1d7d2405e58770 (diff) | |
| download | gallery-e20f93aa6714868141159d8d855c11ef704e5507.tar.gz gallery-e20f93aa6714868141159d8d855c11ef704e5507.tar.xz gallery-e20f93aa6714868141159d8d855c11ef704e5507.zip  | |
Improve import progress information
| -rw-r--r-- | main.go | 65 | 
1 files changed, 33 insertions, 32 deletions
@@ -386,11 +386,7 @@ type importer struct {  	dmMutex sync.Mutex  } -func (i *importer) Import(path string, d fs.DirEntry, err error) error { -	if err != nil || d.IsDir() { -		return err -	} - +func (i *importer) Import(path string) error {  	// The input may be a relative path, and we want to remember it as such,  	// but symlinks for the images must be absolute.  	absPath, err := filepath.Abs(path) @@ -478,38 +474,43 @@ func cmdImport(args []string) error {  		return err  	} -	// TODO: It would be more straight-forward to collect all paths -	// per argument first, then filter them through the actual import. -	// We could show the actual progress, then, -	// only at the cost of remembering all paths before processing. -	pb := newProgressBar(len(args) - 1) +	// Make the first step collecting all the paths, +	// in order to show more useful progress information. +	paths := []string{} +	cb := func(path string, d fs.DirEntry, err error) error { +		if err != nil || d.IsDir() { +			return err +		} +		paths = append(paths, path) +		return nil +	} +	for _, name := range args[1:] { +		if err := filepath.WalkDir(name, cb); err != nil { +			return err +		} +	} + +	pb := newProgressBar(len(paths))  	defer pb.Stop() -	ctx, cancel := context.WithCancelCause(context.Background())  	i := importer{} +	ctx, cancel := context.WithCancelCause(context.Background())  	wg := sync.WaitGroup{} -	for _, name := range args[1:] { -		cb := func(path string, d fs.DirEntry, err error) error { -			if taskSemaphore.Acquire(ctx, 1) != nil { -				return context.Cause(ctx) -			} - -			wg.Add(1) -			go func() { -				defer taskSemaphore.Release(1) -				defer wg.Done() -				if err := i.Import(path, d, err); err != nil { -					cancel(err) -				} -			}() -			return nil -		} -		if err := filepath.WalkDir(name, cb); err != nil { -			cancel(err) +	for _, path := range paths { +		if taskSemaphore.Acquire(ctx, 1) != nil {  			break  		} -		pb.Step() +		wg.Add(1) +		go func(path string) { +			defer taskSemaphore.Release(1) +			defer wg.Done() +			if err := i.Import(path); err != nil { +				cancel(err) +			} else { +				pb.Step() +			} +		}(path)  	}  	wg.Wait()  	if ctx.Err() != nil { @@ -643,9 +644,9 @@ func cmdThumbnail(args []string) error {  			defer wg.Done()  			if err := makeThumbnailFor(sha1); err != nil {  				cancel(err) +			} else { +				pb.Step()  			} - -			pb.Step()  		}(sha1)  	}  	wg.Wait()  | 
