show size of selected files in header

This commit is contained in:
Lilian Jónsdóttir 2024-07-21 20:54:22 -07:00
parent ead10096d7
commit b3f19bef73
4 changed files with 30 additions and 6 deletions

View file

@ -28,7 +28,7 @@ func (f DiskFile) IsDir() bool { return f.isdir }
func (f DiskFile) Mode() fs.FileMode { return f.mode } func (f DiskFile) Mode() fs.FileMode { return f.mode }
func (f DiskFile) Filesize() int64 { func (f DiskFile) Filesize() int64 {
if f.isdir { if f.isdir {
return -1 return 0
} }
return f.filesize return f.filesize
} }

View file

@ -42,11 +42,15 @@ func SortByModifiedReverse(a, b File) int {
} }
func SortBySize(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 { 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 { func SortByName(a, b File) int {
@ -98,3 +102,11 @@ func SortDirectoriesLast(a, b File) int {
return 0 return 0
} }
} }
func getSortingSize(f File) int64 {
if f.IsDir() {
return -1
} else {
return f.Filesize()
}
}

View file

@ -51,7 +51,7 @@ func (t TrashInfo) IsDir() bool { return t.isdir }
func (t TrashInfo) Mode() fs.FileMode { return t.mode } func (t TrashInfo) Mode() fs.FileMode { return t.mode }
func (t TrashInfo) Filesize() int64 { func (t TrashInfo) Filesize() int64 {
if t.isdir { if t.isdir {
return -1 return 0
} }
return t.filesize return t.filesize
} }

View file

@ -53,6 +53,7 @@ type model struct {
table table.Model table table.Model
keys keyMap keys keyMap
selected map[string]bool selected map[string]bool
selectsize int64
readonly bool readonly bool
once bool once bool
termheight int termheight int
@ -79,6 +80,7 @@ func newModel(fs []files.File, width, height int, readonly, preselected, once bo
termheight: height, termheight: height,
mode: mode, mode: mode,
selected: map[string]bool{}, selected: map[string]bool{},
selectsize: 0,
files: fs, files: fs,
} }
) )
@ -288,7 +290,7 @@ func (m model) header() string {
} }
mode += fmt.Sprintf(" %s %s", dot, strings.Join(select_keys, wide_dot)) 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 { func (m model) footer() string {
@ -348,6 +350,7 @@ func (m *model) freshRows(preselected bool) (rows []table.Row) {
} }
if preselected { if preselected {
m.selected[f.String()] = true m.selected[f.String()] = true
m.selectsize += f.Filesize()
} }
rows = append(rows, r) rows = append(rows, r)
} }
@ -403,16 +406,19 @@ func (m *model) toggleItem(index int) (selected bool) {
} }
name := m.files[index].String() name := m.files[index].String()
size := m.files[index].Filesize()
// select the thing // select the thing
if v, ok := m.selected[name]; v && ok { if v, ok := m.selected[name]; v && ok {
// already selected // already selected
delete(m.selected, name) delete(m.selected, name)
selected = false selected = false
m.selectsize -= size
} else { } else {
// not selected // not selected
m.selected[name] = true m.selected[name] = true
selected = true selected = true
m.selectsize += size
} }
// update the rows with the state // update the rows with the state
@ -426,8 +432,10 @@ func (m *model) selectAll() {
} }
m.selected = map[string]bool{} m.selected = map[string]bool{}
m.selectsize = 0
for i := range m.table.Rows() { for i := range m.table.Rows() {
m.selected[m.files[i].String()] = true m.selected[m.files[i].String()] = true
m.selectsize += m.files[i].Filesize()
} }
m.updateRows(true) m.updateRows(true)
} }
@ -438,6 +446,7 @@ func (m *model) unselectAll() {
} }
m.selected = map[string]bool{} m.selected = map[string]bool{}
m.selectsize = 0
m.updateRows(false) m.updateRows(false)
} }
@ -450,8 +459,10 @@ func (m *model) invertSelection() {
for index, row := range m.table.Rows() { for index, row := range m.table.Rows() {
name := m.files[index].String() name := m.files[index].String()
size := m.files[index].Filesize()
if v, ok := m.selected[name]; v && ok { if v, ok := m.selected[name]; v && ok {
delete(m.selected, name) delete(m.selected, name)
m.selectsize -= size
newrows = append(newrows, table.Row{ newrows = append(newrows, table.Row{
row[0], row[0],
row[1], row[1],
@ -461,6 +472,7 @@ func (m *model) invertSelection() {
}) })
} else { } else {
m.selected[name] = true m.selected[name] = true
m.selectsize += size
newrows = append(newrows, table.Row{ newrows = append(newrows, table.Row{
row[0], row[0],
row[1], row[1],