From d10e37a964a11d80eff53f915b7f8100ca6b9b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Fern=C3=A1ndez?= Date: Wed, 16 Oct 2024 15:11:52 -0600 Subject: [PATCH] update size on deletion of children --- fileinfo/fileInfo.go | 4 ++++ fileinfo/fileInfo_test.go | 3 +-- main.go | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fileinfo/fileInfo.go b/fileinfo/fileInfo.go index a5baad9..9094c9c 100644 --- a/fileinfo/fileInfo.go +++ b/fileinfo/fileInfo.go @@ -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 }) diff --git a/fileinfo/fileInfo_test.go b/fileinfo/fileInfo_test.go index 837deb7..458bd1c 100644 --- a/fileinfo/fileInfo_test.go +++ b/fileinfo/fileInfo_test.go @@ -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) diff --git a/main.go b/main.go index 2a30458..013c38d 100644 --- a/main.go +++ b/main.go @@ -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 }