Skip to content

Commit

Permalink
do not store path in every file item
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Milde committed Jan 3, 2021
1 parent fb9b383 commit 8a9d603
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
7 changes: 1 addition & 6 deletions analyze/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func ProcessDir(path string, progress *CurrentProgress, ignore ShouldDirBeIgnore
concurrencyLimitChannel := make(chan bool, 2*runtime.NumCPU())
var wait sync.WaitGroup
dir := processDir(path, progress, concurrencyLimitChannel, &wait, ignore)
dir.BasePath = filepath.Dir(path)
wait.Wait()
dir.UpdateStats()
return dir
Expand All @@ -33,10 +34,6 @@ func ProcessDir(path string, progress *CurrentProgress, ignore ShouldDirBeIgnore
func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel chan bool, wait *sync.WaitGroup, ignoreDir ShouldDirBeIgnored) *File {
var file *File
var err error
path, err = filepath.Abs(path)
if err != nil {
log.Print(err.Error())
}

files, err := ioutil.ReadDir(path)
if err != nil {
Expand All @@ -45,7 +42,6 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel

dir := File{
Name: filepath.Base(path),
Path: path,
IsDir: true,
ItemCount: 1,
Files: make([]*File, 0, len(files)),
Expand Down Expand Up @@ -76,7 +72,6 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel
} else {
file = &File{
Name: f.Name(),
Path: entryPath,
Size: f.Size(),
ItemCount: 1,
Parent: &dir,
Expand Down
51 changes: 30 additions & 21 deletions analyze/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,31 @@ package analyze

import (
"os"
"path/filepath"
)

// File struct
type File struct {
Name string
Path string
BasePath string
Size int64
ItemCount int
IsDir bool
Files Files
Parent *File
}

// Files - slice of pointers to File
type Files []*File

// Find searches File in Files and returns its index, or -1
func (s Files) Find(file *File) int {
for i, item := range s {
if item == file {
return i
}
// Path retruns absolute path of the file
func (f *File) Path() string {
if f.BasePath != "" {
return filepath.Join(f.BasePath, f.Name)
}
return -1
}

// Remove removes File from Files
func (s Files) Remove(file *File) Files {
index := s.Find(file)
if index == -1 {
return s
}
return append(s[:index], s[index+1:]...)
return filepath.Join(f.Parent.Path(), f.Name)
}

// RemoveFile removes file from dir
func (f *File) RemoveFile(file *File) {
error := os.RemoveAll(file.Path)
error := os.RemoveAll(file.Path())
if error != nil {
panic(error)
}
Expand Down Expand Up @@ -73,3 +60,25 @@ func (f *File) UpdateStats() {
f.ItemCount = itemCount + 1
f.Size = totalSize
}

// Files - slice of pointers to File
type Files []*File

// Find searches File in Files and returns its index, or -1
func (s Files) Find(file *File) int {
for i, item := range s {
if item == file {
return i
}
}
return -1
}

// Remove removes File from Files
func (s Files) Remove(file *File) Files {
index := s.Find(file)
if index == -1 {
return s
}
return append(s[:index], s[index+1:]...)
}
1 change: 1 addition & 0 deletions analyze/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestRemove(t *testing.T) {
func TestRemoveFile(t *testing.T) {
dir := &File{
Name: "xxx",
BasePath: ".",
Size: 5,
ItemCount: 3,
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (ui *UI) ShouldDirBeIgnored(path string) bool {
}

func (ui *UI) showDir() {
ui.currentDirPath = ui.currentDir.Path
ui.currentDirPath = ui.currentDir.Path()
ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---")

ui.table.Clear()
Expand Down
1 change: 1 addition & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestFooter(t *testing.T) {

dir := analyze.File{
Name: "xxx",
BasePath: ".",
Size: 5,
ItemCount: 2,
}
Expand Down

0 comments on commit 8a9d603

Please sign in to comment.