ncdu: add toggle option for average size in directory - key 'a'

Add toggle option to show average size in directory. This toggle
function is for ncdu and is binded to key 'a'.
This commit is contained in:
Adam Plánský 2020-10-26 21:44:01 +01:00 committed by Nick Craig-Wood
parent 127f0fc64c
commit f89ff3872d
1 changed files with 49 additions and 32 deletions

View File

@ -71,6 +71,7 @@ func helpText() (tr []string) {
" ←,h to return", " ←,h to return",
" c toggle counts", " c toggle counts",
" g toggle graph", " g toggle graph",
" a toggle average size in directory",
" n,s,C,A sort by name,size,count,average size", " n,s,C,A sort by name,size,count,average size",
" d delete file/directory", " d delete file/directory",
} }
@ -88,28 +89,29 @@ func helpText() (tr []string) {
// UI contains the state of the user interface // UI contains the state of the user interface
type UI struct { type UI struct {
f fs.Fs // fs being displayed f fs.Fs // fs being displayed
fsName string // human name of Fs fsName string // human name of Fs
root *scan.Dir // root directory root *scan.Dir // root directory
d *scan.Dir // current directory being displayed d *scan.Dir // current directory being displayed
path string // path of current directory path string // path of current directory
showBox bool // whether to show a box showBox bool // whether to show a box
boxText []string // text to show in box boxText []string // text to show in box
boxMenu []string // box menu options boxMenu []string // box menu options
boxMenuButton int boxMenuButton int
boxMenuHandler func(fs fs.Fs, path string, option int) (string, error) boxMenuHandler func(fs fs.Fs, path string, option int) (string, error)
entries fs.DirEntries // entries of current directory entries fs.DirEntries // entries of current directory
sortPerm []int // order to display entries in after sorting sortPerm []int // order to display entries in after sorting
invSortPerm []int // inverse order invSortPerm []int // inverse order
dirListHeight int // height of listing dirListHeight int // height of listing
listing bool // whether listing is in progress listing bool // whether listing is in progress
showGraph bool // toggle showing graph showGraph bool // toggle showing graph
showCounts bool // toggle showing counts showCounts bool // toggle showing counts
sortByName int8 // +1 for normal, 0 for off, -1 for reverse showDirAverageSize bool // toggle average size
sortBySize int8 sortByName int8 // +1 for normal, 0 for off, -1 for reverse
sortByCount int8 sortBySize int8
sortByAverageSize int8 sortByCount int8
dirPosMap map[string]dirPos // store for directory positions sortByAverageSize int8
dirPosMap map[string]dirPos // store for directory positions
} }
// Where we have got to in the directory listing // Where we have got to in the directory listing
@ -346,6 +348,18 @@ func (u *UI) Draw() error {
extras += " " extras += " "
} }
}
var averageSize float64
if count > 0 {
averageSize = float64(size) / float64(count)
}
if u.showDirAverageSize {
if averageSize > 0 {
extras += fmt.Sprintf("%8v ", fs.SizeSuffix(int64(averageSize)))
} else {
extras += " "
}
} }
if u.showGraph { if u.showGraph {
bars := (size + perBar/2 - 1) / perBar bars := (size + perBar/2 - 1) / perBar
@ -661,16 +675,17 @@ func (u *UI) toggleSort(sortType *int8) {
// NewUI creates a new user interface for ncdu on f // NewUI creates a new user interface for ncdu on f
func NewUI(f fs.Fs) *UI { func NewUI(f fs.Fs) *UI {
return &UI{ return &UI{
f: f, f: f,
path: "Waiting for root...", path: "Waiting for root...",
dirListHeight: 20, // updated in Draw dirListHeight: 20, // updated in Draw
fsName: f.Name() + ":" + f.Root(), fsName: f.Name() + ":" + f.Root(),
showGraph: true, showGraph: true,
showCounts: false, showCounts: false,
sortByName: 0, // +1 for normal, 0 for off, -1 for reverse showDirAverageSize: false,
sortBySize: 1, sortByName: 0, // +1 for normal, 0 for off, -1 for reverse
sortByCount: 0, sortBySize: 1,
dirPosMap: make(map[string]dirPos), sortByCount: 0,
dirPosMap: make(map[string]dirPos),
} }
} }
@ -758,6 +773,8 @@ outer:
u.showCounts = !u.showCounts u.showCounts = !u.showCounts
case 'g': case 'g':
u.showGraph = !u.showGraph u.showGraph = !u.showGraph
case 'a':
u.showDirAverageSize = !u.showDirAverageSize
case 'n': case 'n':
u.toggleSort(&u.sortByName) u.toggleSort(&u.sortByName)
case 's': case 's':