Skip to content

Commit

Permalink
refactor(filecount)!: Move size and file count algorithm into filecount
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom5521 committed May 31, 2024
1 parent b532d13 commit e5239da
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 49 deletions.
60 changes: 11 additions & 49 deletions file.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"fmt"
"io/fs"
"os"
"os/user"
"path/filepath"
"time"

msg "github.com/Tom5521/GoNotes/pkg/messages"
"github.com/Tom5521/fsize/checkos"
"github.com/Tom5521/fsize/filecount"
"github.com/Tom5521/fsize/stat"
"github.com/schollz/progressbar/v3"
)

type File struct {
Expand Down Expand Up @@ -46,53 +45,12 @@ func Read(path string) (f File, err error) {
}

if Progress && finfo.IsDir() && !NoWalk {
msg.Info("Counting the amount of files...")
var (
fileNumber int64
files []os.FileInfo
)
err = filepath.Walk(path, func(_ string, info fs.FileInfo, err error) error {
if err != nil {
Warning(err)
return nil
}
fileNumber++
files = append(files, info)
fmt.Printf("%v files found...\r", fileNumber)
return nil
})
fmt.Print("\n")
if err != nil {
return f, err
}
msg.Info("Calculating total size...")
f.FilesNumber = fileNumber
bar := progressbar.Default(fileNumber)
for _, file := range files {
f.Size += file.Size()
bar.Add(1)
}
err = filecount.Progress(&f.FilesNumber, &f.Size, path)
} else if finfo.IsDir() && !NoWalk {
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)
}

f.Size += info.Size()
f.FilesNumber++

return nil
})
if err != nil {
return f, err
}
err = filecount.Print(&f.FilesNumber, &f.Size, path)
}

return f, nil
return
}

func BasicFile(finfo os.FileInfo, absPath string) (f File, err error) {
Expand All @@ -106,9 +64,13 @@ func BasicFile(finfo os.FileInfo, absPath string) (f File, err error) {
// Values which do not work on some systems.

// Only on windows systems.
f.CreationDate = stat.CreationDate(finfo)
if checkos.Windows {
f.CreationDate = stat.CreationDate(finfo)
}
// Only on unix systems.
f.User, f.Group, err = stat.UsrAndGroup(finfo)
if checkos.Unix {
f.User, f.Group, err = stat.UsrAndGroup(finfo)
}

return
}
Expand Down
32 changes: 32 additions & 0 deletions filecount/print.go
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
}
42 changes: 42 additions & 0 deletions filecount/progress.go
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
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package main

import (
msg "github.com/Tom5521/GoNotes/pkg/messages"
"github.com/Tom5521/fsize/filecount"
"github.com/gookit/color"
"github.com/spf13/cobra"
)

func main() {
// Initialize variables
filecount.Warning = Warning
filecount.PrintOnWalk = &PrintOnWalk
err := LoadSettings()
if err != nil {
return
Expand Down

0 comments on commit e5239da

Please sign in to comment.