From b3f19bef7326da5fad747d471a4a1bf7a5aca793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Sun, 21 Jul 2024 20:54:22 -0700 Subject: [PATCH] show size of selected files in header --- internal/files/disk.go | 2 +- internal/files/files.go | 16 ++++++++++++++-- internal/files/trash.go | 2 +- internal/tables/tables.go | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/internal/files/disk.go b/internal/files/disk.go index c274d1a..f1a7747 100644 --- a/internal/files/disk.go +++ b/internal/files/disk.go @@ -28,7 +28,7 @@ func (f DiskFile) IsDir() bool { return f.isdir } func (f DiskFile) Mode() fs.FileMode { return f.mode } func (f DiskFile) Filesize() int64 { if f.isdir { - return -1 + return 0 } return f.filesize } diff --git a/internal/files/files.go b/internal/files/files.go index 2ceee52..48d12ec 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -42,11 +42,15 @@ func SortByModifiedReverse(a, b File) int { } func SortBySize(a, b File) int { - return cmp.Compare(b.Filesize(), a.Filesize()) + as := getSortingSize(a) + bs := getSortingSize(b) + return cmp.Compare(bs, as) } func SortBySizeReverse(a, b File) int { - return cmp.Compare(a.Filesize(), b.Filesize()) + as := getSortingSize(a) + bs := getSortingSize(b) + return cmp.Compare(as, bs) } func SortByName(a, b File) int { @@ -98,3 +102,11 @@ func SortDirectoriesLast(a, b File) int { return 0 } } + +func getSortingSize(f File) int64 { + if f.IsDir() { + return -1 + } else { + return f.Filesize() + } +} diff --git a/internal/files/trash.go b/internal/files/trash.go index d1bca2f..17cea16 100644 --- a/internal/files/trash.go +++ b/internal/files/trash.go @@ -51,7 +51,7 @@ func (t TrashInfo) IsDir() bool { return t.isdir } func (t TrashInfo) Mode() fs.FileMode { return t.mode } func (t TrashInfo) Filesize() int64 { if t.isdir { - return -1 + return 0 } return t.filesize } diff --git a/internal/tables/tables.go b/internal/tables/tables.go index 335a5c1..d220f2b 100644 --- a/internal/tables/tables.go +++ b/internal/tables/tables.go @@ -53,6 +53,7 @@ type model struct { table table.Model keys keyMap selected map[string]bool + selectsize int64 readonly bool once bool termheight int @@ -79,6 +80,7 @@ func newModel(fs []files.File, width, height int, readonly, preselected, once bo termheight: height, mode: mode, selected: map[string]bool{}, + selectsize: 0, files: fs, } ) @@ -283,12 +285,12 @@ func (m model) header() string { default: mode = m.mode.String() if m.workdir != "" { - mode += fmt.Sprintf(" in %s ", dirs.UnExpand(m.workdir, "")) + mode += fmt.Sprintf(" in %s", dirs.UnExpand(m.workdir, "")) } } mode += fmt.Sprintf(" %s %s", dot, strings.Join(select_keys, wide_dot)) - return fmt.Sprintf(" %s %s %d/%d", mode, dot, len(m.selected), len(m.table.Rows())) + return fmt.Sprintf(" %s %s %d/%d %s %s", mode, dot, len(m.selected), len(m.table.Rows()), dot, humanize.Bytes(uint64(m.selectsize))) } func (m model) footer() string { @@ -348,6 +350,7 @@ func (m *model) freshRows(preselected bool) (rows []table.Row) { } if preselected { m.selected[f.String()] = true + m.selectsize += f.Filesize() } rows = append(rows, r) } @@ -403,16 +406,19 @@ func (m *model) toggleItem(index int) (selected bool) { } name := m.files[index].String() + size := m.files[index].Filesize() // select the thing if v, ok := m.selected[name]; v && ok { // already selected delete(m.selected, name) selected = false + m.selectsize -= size } else { // not selected m.selected[name] = true selected = true + m.selectsize += size } // update the rows with the state @@ -426,8 +432,10 @@ func (m *model) selectAll() { } m.selected = map[string]bool{} + m.selectsize = 0 for i := range m.table.Rows() { m.selected[m.files[i].String()] = true + m.selectsize += m.files[i].Filesize() } m.updateRows(true) } @@ -438,6 +446,7 @@ func (m *model) unselectAll() { } m.selected = map[string]bool{} + m.selectsize = 0 m.updateRows(false) } @@ -450,8 +459,10 @@ func (m *model) invertSelection() { for index, row := range m.table.Rows() { name := m.files[index].String() + size := m.files[index].Filesize() if v, ok := m.selected[name]; v && ok { delete(m.selected, name) + m.selectsize -= size newrows = append(newrows, table.Row{ row[0], row[1], @@ -461,6 +472,7 @@ func (m *model) invertSelection() { }) } else { m.selected[name] = true + m.selectsize += size newrows = append(newrows, table.Row{ row[0], row[1],