Skip to content

Commit

Permalink
update size on deletion of children
Browse files Browse the repository at this point in the history
  • Loading branch information
frogfreg committed Oct 16, 2024
1 parent 5691d6e commit d10e37a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 4 additions & 0 deletions fileinfo/fileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,13 @@ func GetSortedDirs(m map[string]FileInfo, root string) []FileInfo {
}

func CleanChildren(m map[string]FileInfo, dir string) {
size := m[dir].Size
delete(m, dir)

for path, fi := range m {
if slices.Contains(fi.Children, dir) {
fi.Size -= size
}
fi.Children = slices.DeleteFunc(fi.Children, func(item string) bool {
return item == dir
})
Expand Down
3 changes: 1 addition & 2 deletions fileinfo/fileInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ func TestCleanChildren(t *testing.T) {
}

expected := map[string]FileInfo{
"testfiles": {Name: "testfiles", FileType: "dir", Size: 13, Children: []string{"testfiles/file1.txt", "testfiles/file2.txt"}, Checked: true},
"testfiles": {Name: "testfiles", FileType: "dir", Size: 3, Children: []string{"testfiles/file1.txt", "testfiles/file2.txt"}, Checked: true},
}

CleanChildren(m, "testfiles/dir1")
t.Log(m)

if !reflect.DeepEqual(expected, m) {
t.Errorf("expected %v, got %v", expected, m)
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ func (m model) updateCurrentDir(dir string, replace bool) model {
return m
}

func deleteCmd(m map[string]fileinfo.FileInfo, dir string) tea.Cmd {
func deleteCmd(m map[string]fileinfo.FileInfo, path string) tea.Cmd {
f := func() tea.Msg {
var res deleteResponse

err := os.RemoveAll(dir)
err := os.RemoveAll(path)
if err != nil {
res.err = err
}

fileinfo.CleanChildren(m, dir)
fileinfo.CleanChildren(m, path)

return res
}
Expand Down

0 comments on commit d10e37a

Please sign in to comment.