-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(filecount)!: Move size and file count algorithm into filecount
- Loading branch information
Showing
4 changed files
with
89 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package filecount | ||
|
||
import ( | ||
"io/fs" | ||
|
||
"path/filepath" | ||
|
||
msg "github.com/Tom5521/GoNotes/pkg/messages" | ||
) | ||
|
||
var PrintOnWalk *bool | ||
|
||
func Print(count, size *int64, path string) (err error) { | ||
err = filepath.Walk(path, func(name string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
Warning(err) | ||
return nil | ||
} | ||
if *PrintOnWalk { | ||
msg.Infof("Reading \"%s\"...", name) | ||
} | ||
|
||
*size += info.Size() | ||
*count++ | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package filecount | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"path/filepath" | ||
|
||
msg "github.com/Tom5521/GoNotes/pkg/messages" | ||
"github.com/schollz/progressbar/v3" | ||
) | ||
|
||
var Warning func(...any) | ||
|
||
func Progress(count, size *int64, path string) (err error) { | ||
msg.Info("Counting the amount of files...") | ||
var warnings []error | ||
countBar := progressbar.Default(-1) | ||
setbar := func(files, errors int) { | ||
countBar.Describe( | ||
fmt.Sprintf("%v files with %v errors", files, errors), | ||
) | ||
} | ||
err = filepath.Walk(path, func(_ string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
warnings = append(warnings, err) | ||
return nil | ||
} | ||
*count++ | ||
*size += info.Size() | ||
setbar(int(*count), len(warnings)) | ||
return nil | ||
}) | ||
countBar.Finish() | ||
for _, e := range warnings { | ||
Warning(e.Error()) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters