2017-06-15 19:10:54 +00:00
|
|
|
// Package ncdu implements a text based user interface for exploring a remote
|
|
|
|
|
2020-07-31 18:57:48 +00:00
|
|
|
//+build !plan9,!solaris,!js
|
2017-06-15 19:10:54 +00:00
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
package ncdu
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2017-04-01 18:53:44 +00:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2019-06-23 05:40:54 +00:00
|
|
|
"reflect"
|
2017-04-01 18:53:44 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-23 05:40:54 +00:00
|
|
|
"github.com/atotto/clipboard"
|
2019-02-26 19:18:52 +00:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2017-04-01 18:53:44 +00:00
|
|
|
termbox "github.com/nsf/termbox-go"
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/ncdu/scan"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-04-01 18:53:44 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 15:58:11 +00:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:58:11 +00:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-04-01 18:53:44 +00:00
|
|
|
Use: "ncdu remote:path",
|
|
|
|
Short: `Explore a remote with a text based user interface.`,
|
|
|
|
Long: `
|
|
|
|
This displays a text based user interface allowing the navigation of a
|
|
|
|
remote. It is most useful for answering the question - "What is using
|
|
|
|
all my disk space?".
|
|
|
|
|
2020-05-22 11:22:52 +00:00
|
|
|
{{< asciinema 157793 >}}
|
2018-01-18 14:22:43 +00:00
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
To make the user interface it first scans the entire remote given and
|
|
|
|
builds an in memory representation. rclone ncdu can be used during
|
|
|
|
this scanning phase and you will see it building up the directory
|
|
|
|
structure as it goes along.
|
|
|
|
|
|
|
|
Here are the keys - press '?' to toggle the help on and off
|
|
|
|
|
2019-06-23 05:40:54 +00:00
|
|
|
` + strings.Join(helpText()[1:], "\n ") + `
|
2017-04-01 18:53:44 +00:00
|
|
|
|
|
|
|
This an homage to the [ncdu tool](https://dev.yorhel.nl/ncdu) but for
|
2018-10-14 14:59:27 +00:00
|
|
|
rclone remotes. It is missing lots of features at the moment
|
|
|
|
but is useful as it stands.
|
|
|
|
|
|
|
|
Note that it might take some time to delete big files/folders. The
|
|
|
|
UI won't respond in the meantime since the deletion is done synchronously.
|
2017-04-01 18:53:44 +00:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
|
|
|
cmd.Run(false, false, command, func() error {
|
|
|
|
return NewUI(fsrc).Show()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-23 05:40:54 +00:00
|
|
|
// helpText returns help text for ncdu
|
|
|
|
func helpText() (tr []string) {
|
|
|
|
tr = []string{
|
|
|
|
"rclone ncdu",
|
|
|
|
" ↑,↓ or k,j to Move",
|
|
|
|
" →,l to enter",
|
|
|
|
" ←,h to return",
|
|
|
|
" c toggle counts",
|
|
|
|
" g toggle graph",
|
2020-10-26 20:44:01 +00:00
|
|
|
" a toggle average size in directory",
|
2020-10-27 13:28:38 +00:00
|
|
|
" n,s,C,A sort by name,size,count,average size",
|
2019-06-23 05:40:54 +00:00
|
|
|
" d delete file/directory",
|
|
|
|
}
|
|
|
|
if !clipboard.Unsupported {
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 00:17:24 +00:00
|
|
|
tr = append(tr, " y copy current path to clipboard")
|
2019-06-23 05:40:54 +00:00
|
|
|
}
|
|
|
|
tr = append(tr, []string{
|
|
|
|
" Y display current path",
|
|
|
|
" ^L refresh screen",
|
|
|
|
" ? to toggle help on and off",
|
|
|
|
" q/ESC/c-C to quit",
|
|
|
|
}...)
|
|
|
|
return
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UI contains the state of the user interface
|
|
|
|
type UI struct {
|
2020-10-26 20:44:01 +00:00
|
|
|
f fs.Fs // fs being displayed
|
|
|
|
fsName string // human name of Fs
|
|
|
|
root *scan.Dir // root directory
|
|
|
|
d *scan.Dir // current directory being displayed
|
|
|
|
path string // path of current directory
|
|
|
|
showBox bool // whether to show a box
|
|
|
|
boxText []string // text to show in box
|
|
|
|
boxMenu []string // box menu options
|
|
|
|
boxMenuButton int
|
|
|
|
boxMenuHandler func(fs fs.Fs, path string, option int) (string, error)
|
|
|
|
entries fs.DirEntries // entries of current directory
|
|
|
|
sortPerm []int // order to display entries in after sorting
|
|
|
|
invSortPerm []int // inverse order
|
|
|
|
dirListHeight int // height of listing
|
|
|
|
listing bool // whether listing is in progress
|
|
|
|
showGraph bool // toggle showing graph
|
|
|
|
showCounts bool // toggle showing counts
|
|
|
|
showDirAverageSize bool // toggle average size
|
|
|
|
sortByName int8 // +1 for normal, 0 for off, -1 for reverse
|
|
|
|
sortBySize int8
|
|
|
|
sortByCount int8
|
|
|
|
sortByAverageSize int8
|
|
|
|
dirPosMap map[string]dirPos // store for directory positions
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Where we have got to in the directory listing
|
|
|
|
type dirPos struct {
|
|
|
|
entry int
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print a string
|
|
|
|
func Print(x, y int, fg, bg termbox.Attribute, msg string) {
|
|
|
|
for _, c := range msg {
|
|
|
|
termbox.SetCell(x, y, c, fg, bg)
|
|
|
|
x++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf a string
|
|
|
|
func Printf(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
|
|
|
|
s := fmt.Sprintf(format, args...)
|
|
|
|
Print(x, y, fg, bg, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line prints a string to given xmax, with given space
|
|
|
|
func Line(x, y, xmax int, fg, bg termbox.Attribute, spacer rune, msg string) {
|
|
|
|
for _, c := range msg {
|
|
|
|
termbox.SetCell(x, y, c, fg, bg)
|
2019-02-26 19:18:52 +00:00
|
|
|
x += runewidth.RuneWidth(c)
|
2017-04-01 18:53:44 +00:00
|
|
|
if x >= xmax {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ; x < xmax; x++ {
|
|
|
|
termbox.SetCell(x, y, spacer, fg, bg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linef a string
|
|
|
|
func Linef(x, y, xmax int, fg, bg termbox.Attribute, spacer rune, format string, args ...interface{}) {
|
|
|
|
s := fmt.Sprintf(format, args...)
|
|
|
|
Line(x, y, xmax, fg, bg, spacer, s)
|
|
|
|
}
|
|
|
|
|
2018-10-14 14:59:27 +00:00
|
|
|
// LineOptions Print line of selectable options
|
|
|
|
func LineOptions(x, y, xmax int, fg, bg termbox.Attribute, options []string, selected int) {
|
|
|
|
defaultBg := bg
|
|
|
|
defaultFg := fg
|
|
|
|
|
|
|
|
// Print left+right whitespace to center the options
|
|
|
|
xoffset := ((xmax - x) - lineOptionLength(options)) / 2
|
|
|
|
for j := x; j < x+xoffset; j++ {
|
|
|
|
termbox.SetCell(j, y, ' ', fg, bg)
|
|
|
|
}
|
|
|
|
for j := xmax - xoffset; j < xmax; j++ {
|
|
|
|
termbox.SetCell(j, y, ' ', fg, bg)
|
|
|
|
}
|
|
|
|
x += xoffset
|
|
|
|
|
|
|
|
for i, o := range options {
|
|
|
|
termbox.SetCell(x, y, ' ', fg, bg)
|
|
|
|
|
|
|
|
if i == selected {
|
|
|
|
bg = termbox.ColorBlack
|
|
|
|
fg = termbox.ColorWhite
|
|
|
|
}
|
|
|
|
termbox.SetCell(x+1, y, '<', fg, bg)
|
|
|
|
x += 2
|
|
|
|
|
|
|
|
// print option text
|
|
|
|
for _, c := range o {
|
|
|
|
termbox.SetCell(x, y, c, fg, bg)
|
|
|
|
x++
|
|
|
|
}
|
|
|
|
|
|
|
|
termbox.SetCell(x, y, '>', fg, bg)
|
|
|
|
bg = defaultBg
|
|
|
|
fg = defaultFg
|
|
|
|
|
|
|
|
termbox.SetCell(x+1, y, ' ', fg, bg)
|
|
|
|
x += 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lineOptionLength(o []string) int {
|
|
|
|
count := 0
|
|
|
|
for _, i := range o {
|
|
|
|
count += len(i)
|
|
|
|
}
|
|
|
|
return count + 4*len(o) // spacer and arrows <entry>
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
// Box the u.boxText onto the screen
|
|
|
|
func (u *UI) Box() {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
|
|
|
|
// Find dimensions of text
|
|
|
|
boxWidth := 10
|
|
|
|
for _, s := range u.boxText {
|
|
|
|
if len(s) > boxWidth && len(s) < w-4 {
|
|
|
|
boxWidth = len(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boxHeight := len(u.boxText)
|
|
|
|
|
|
|
|
// position
|
|
|
|
x := (w - boxWidth) / 2
|
|
|
|
y := (h - boxHeight) / 2
|
|
|
|
xmax := x + boxWidth
|
2018-10-14 14:59:27 +00:00
|
|
|
if len(u.boxMenu) != 0 {
|
|
|
|
count := lineOptionLength(u.boxMenu)
|
|
|
|
if x+boxWidth > x+count {
|
|
|
|
xmax = x + boxWidth
|
|
|
|
} else {
|
|
|
|
xmax = x + count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ymax := y + len(u.boxText)
|
2017-04-01 18:53:44 +00:00
|
|
|
|
|
|
|
// draw text
|
|
|
|
fg, bg := termbox.ColorRed, termbox.ColorWhite
|
|
|
|
for i, s := range u.boxText {
|
|
|
|
Line(x, y+i, xmax, fg, bg, ' ', s)
|
|
|
|
fg = termbox.ColorBlack
|
|
|
|
}
|
|
|
|
|
2018-10-14 14:59:27 +00:00
|
|
|
if len(u.boxMenu) != 0 {
|
|
|
|
ymax++
|
|
|
|
LineOptions(x, ymax-1, xmax, fg, bg, u.boxMenu, u.boxMenuButton)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw top border
|
|
|
|
for i := y; i < ymax; i++ {
|
|
|
|
termbox.SetCell(x-1, i, '│', fg, bg)
|
|
|
|
termbox.SetCell(xmax, i, '│', fg, bg)
|
|
|
|
}
|
|
|
|
for j := x; j < xmax; j++ {
|
|
|
|
termbox.SetCell(j, y-1, '─', fg, bg)
|
|
|
|
termbox.SetCell(j, ymax, '─', fg, bg)
|
|
|
|
}
|
|
|
|
|
|
|
|
termbox.SetCell(x-1, y-1, '┌', fg, bg)
|
|
|
|
termbox.SetCell(xmax, y-1, '┐', fg, bg)
|
|
|
|
termbox.SetCell(x-1, ymax, '└', fg, bg)
|
|
|
|
termbox.SetCell(xmax, ymax, '┘', fg, bg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UI) moveBox(to int) {
|
|
|
|
if len(u.boxMenu) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if to > 0 { // move right
|
|
|
|
u.boxMenuButton++
|
|
|
|
} else { // move left
|
|
|
|
u.boxMenuButton--
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.boxMenuButton >= len(u.boxMenu) {
|
|
|
|
u.boxMenuButton = len(u.boxMenu) - 1
|
|
|
|
} else if u.boxMenuButton < 0 {
|
|
|
|
u.boxMenuButton = 0
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the biggest entry in the current listing
|
|
|
|
func (u *UI) biggestEntry() (biggest int64) {
|
|
|
|
if u.d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range u.entries {
|
2020-12-28 14:08:12 +00:00
|
|
|
size, _, _, _, _, _ := u.d.AttrI(u.sortPerm[i])
|
2017-04-01 18:53:44 +00:00
|
|
|
if size > biggest {
|
|
|
|
biggest = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-30 15:54:59 +00:00
|
|
|
// hasEmptyDir returns true if there is empty folder in current listing
|
|
|
|
func (u *UI) hasEmptyDir() bool {
|
|
|
|
if u.d == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range u.entries {
|
|
|
|
_, count, isDir, _, _, _ := u.d.AttrI(u.sortPerm[i])
|
|
|
|
if isDir && count == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
// Draw the current screen
|
|
|
|
func (u *UI) Draw() error {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
u.dirListHeight = h - 3
|
|
|
|
|
|
|
|
// Plot
|
|
|
|
err := termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to clear screen")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header line
|
|
|
|
Linef(0, 0, w, termbox.ColorBlack, termbox.ColorWhite, ' ', "rclone ncdu %s - use the arrow keys to navigate, press ? for help", fs.Version)
|
|
|
|
|
|
|
|
// Directory line
|
|
|
|
Linef(0, 1, w, termbox.ColorWhite, termbox.ColorBlack, '-', "-- %s ", u.path)
|
|
|
|
|
|
|
|
// graphs
|
|
|
|
const (
|
|
|
|
graphBars = 10
|
|
|
|
graph = "########## "
|
|
|
|
)
|
|
|
|
|
|
|
|
// Directory listing
|
|
|
|
if u.d != nil {
|
|
|
|
y := 2
|
|
|
|
perBar := u.biggestEntry() / graphBars
|
|
|
|
if perBar == 0 {
|
|
|
|
perBar = 1
|
|
|
|
}
|
2020-10-30 15:54:59 +00:00
|
|
|
showEmptyDir := u.hasEmptyDir()
|
2017-04-01 18:53:44 +00:00
|
|
|
dirPos := u.dirPosMap[u.path]
|
|
|
|
for i, j := range u.sortPerm[dirPos.offset:] {
|
|
|
|
entry := u.entries[j]
|
|
|
|
n := i + dirPos.offset
|
|
|
|
if y >= h-1 {
|
|
|
|
break
|
|
|
|
}
|
2020-12-28 14:08:12 +00:00
|
|
|
size, count, isDir, readable, entriesHaveErrors, err := u.d.AttrI(u.sortPerm[n])
|
2017-04-01 18:53:44 +00:00
|
|
|
fg := termbox.ColorWhite
|
2020-12-28 14:08:12 +00:00
|
|
|
if entriesHaveErrors {
|
|
|
|
fg = termbox.ColorYellow
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fg = termbox.ColorRed
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
bg := termbox.ColorBlack
|
|
|
|
if n == dirPos.entry {
|
|
|
|
fg, bg = bg, fg
|
|
|
|
}
|
|
|
|
mark := ' '
|
|
|
|
if isDir {
|
|
|
|
mark = '/'
|
|
|
|
}
|
2020-12-28 15:20:23 +00:00
|
|
|
fileFlag := ' '
|
2017-04-01 18:53:44 +00:00
|
|
|
message := ""
|
|
|
|
if !readable {
|
|
|
|
message = " [not read yet]"
|
|
|
|
}
|
2020-12-28 14:08:12 +00:00
|
|
|
if entriesHaveErrors {
|
|
|
|
message = " [some subdirectories could not be read, size may be underestimated]"
|
2020-12-28 15:20:23 +00:00
|
|
|
fileFlag = '.'
|
2020-12-28 14:08:12 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
message = fmt.Sprintf(" [%s]", err)
|
2020-12-28 15:20:23 +00:00
|
|
|
fileFlag = '!'
|
2020-12-28 14:08:12 +00:00
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
extras := ""
|
|
|
|
if u.showCounts {
|
|
|
|
if count > 0 {
|
2021-04-07 10:10:31 +00:00
|
|
|
extras += fmt.Sprintf("%8v ", fs.CountSuffix(count))
|
2017-04-01 18:53:44 +00:00
|
|
|
} else {
|
|
|
|
extras += " "
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:44:01 +00:00
|
|
|
}
|
|
|
|
var averageSize float64
|
|
|
|
if count > 0 {
|
|
|
|
averageSize = float64(size) / float64(count)
|
|
|
|
}
|
|
|
|
if u.showDirAverageSize {
|
|
|
|
if averageSize > 0 {
|
2021-03-02 19:11:57 +00:00
|
|
|
extras += fmt.Sprintf("%9v ", fs.SizeSuffix(int64(averageSize)))
|
2020-10-26 20:44:01 +00:00
|
|
|
} else {
|
2021-03-02 19:11:57 +00:00
|
|
|
extras += " "
|
2020-10-26 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
2020-10-30 15:54:59 +00:00
|
|
|
if showEmptyDir {
|
2020-12-28 15:20:23 +00:00
|
|
|
if isDir && count == 0 && fileFlag == ' ' {
|
|
|
|
fileFlag = 'e'
|
2020-10-30 15:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
if u.showGraph {
|
|
|
|
bars := (size + perBar/2 - 1) / perBar
|
|
|
|
// clip if necessary - only happens during startup
|
|
|
|
if bars > 10 {
|
|
|
|
bars = 10
|
|
|
|
} else if bars < 0 {
|
|
|
|
bars = 0
|
|
|
|
}
|
|
|
|
extras += "[" + graph[graphBars-bars:2*graphBars-bars] + "] "
|
|
|
|
}
|
2021-03-02 19:11:57 +00:00
|
|
|
Linef(0, y, w, fg, bg, ' ', "%c %9v %s%c%s%s", fileFlag, fs.SizeSuffix(size), extras, mark, path.Base(entry.Remote()), message)
|
2017-04-01 18:53:44 +00:00
|
|
|
y++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Footer
|
|
|
|
if u.d == nil {
|
|
|
|
Line(0, h-1, w, termbox.ColorBlack, termbox.ColorWhite, ' ', "Waiting for root directory...")
|
|
|
|
} else {
|
|
|
|
message := ""
|
|
|
|
if u.listing {
|
|
|
|
message = " [listing in progress]"
|
|
|
|
}
|
|
|
|
size, count := u.d.Attr()
|
|
|
|
Linef(0, h-1, w, termbox.ColorBlack, termbox.ColorWhite, ' ', "Total usage: %v, Objects: %d%s", fs.SizeSuffix(size), count, message)
|
|
|
|
}
|
|
|
|
|
2019-04-30 12:06:24 +00:00
|
|
|
// Show the box on top if required
|
2017-04-01 18:53:44 +00:00
|
|
|
if u.showBox {
|
|
|
|
u.Box()
|
|
|
|
}
|
|
|
|
err = termbox.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to flush screen")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the cursor this many spaces adjusting the viewport as necessary
|
|
|
|
func (u *UI) move(d int) {
|
|
|
|
if u.d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
absD := d
|
|
|
|
if d < 0 {
|
|
|
|
absD = -d
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := len(u.entries)
|
|
|
|
|
|
|
|
// Fetch current dirPos
|
|
|
|
dirPos := u.dirPosMap[u.path]
|
|
|
|
|
|
|
|
dirPos.entry += d
|
|
|
|
|
|
|
|
// check entry in range
|
|
|
|
if dirPos.entry < 0 {
|
|
|
|
dirPos.entry = 0
|
|
|
|
} else if dirPos.entry >= entries {
|
|
|
|
dirPos.entry = entries - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// check cursor still on screen
|
|
|
|
p := dirPos.entry - dirPos.offset // where dirPos.entry appears on the screen
|
|
|
|
if p < 0 {
|
|
|
|
dirPos.offset -= absD
|
|
|
|
} else if p >= u.dirListHeight {
|
|
|
|
dirPos.offset += absD
|
|
|
|
}
|
|
|
|
|
|
|
|
// check dirPos.offset in bounds
|
2017-12-12 13:52:58 +00:00
|
|
|
if entries == 0 || dirPos.offset < 0 {
|
2017-04-01 18:53:44 +00:00
|
|
|
dirPos.offset = 0
|
|
|
|
} else if dirPos.offset >= entries {
|
|
|
|
dirPos.offset = entries - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// write dirPos back for later
|
|
|
|
u.dirPosMap[u.path] = dirPos
|
|
|
|
}
|
|
|
|
|
2018-10-14 14:59:27 +00:00
|
|
|
func (u *UI) removeEntry(pos int) {
|
|
|
|
u.d.Remove(pos)
|
|
|
|
u.setCurrentDir(u.d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the entry at the current position
|
|
|
|
func (u *UI) delete() {
|
2021-04-14 16:20:17 +00:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 08:34:30 +00:00
|
|
|
ctx := context.Background()
|
2021-04-14 16:20:17 +00:00
|
|
|
cursorPos := u.dirPosMap[u.path]
|
|
|
|
dirPos := u.sortPerm[cursorPos.entry]
|
|
|
|
dirEntry := u.entries[dirPos]
|
2018-10-14 14:59:27 +00:00
|
|
|
u.boxMenu = []string{"cancel", "confirm"}
|
2021-04-14 16:20:17 +00:00
|
|
|
if obj, isFile := dirEntry.(fs.Object); isFile {
|
2018-10-14 14:59:27 +00:00
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2019-06-17 08:34:30 +00:00
|
|
|
err := operations.DeleteFile(ctx, obj)
|
2018-10-14 14:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-14 16:20:17 +00:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 14:59:27 +00:00
|
|
|
return "Successfully deleted file!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Delete this file?",
|
2021-04-14 16:20:17 +00:00
|
|
|
u.fsName + dirEntry.String()})
|
2018-10-14 14:59:27 +00:00
|
|
|
} else {
|
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2021-04-14 16:20:17 +00:00
|
|
|
err := operations.Purge(ctx, f, dirEntry.String())
|
2018-10-14 14:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-14 16:20:17 +00:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 14:59:27 +00:00
|
|
|
return "Successfully purged folder!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Purge this directory?",
|
|
|
|
"ALL files in it will be deleted",
|
2021-04-14 16:20:17 +00:00
|
|
|
u.fsName + dirEntry.String()})
|
2018-10-14 14:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 05:40:54 +00:00
|
|
|
func (u *UI) displayPath() {
|
|
|
|
u.togglePopupBox([]string{
|
|
|
|
"Current Path",
|
|
|
|
u.path,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UI) copyPath() {
|
|
|
|
if !clipboard.Unsupported {
|
|
|
|
_ = clipboard.WriteAll(u.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
// Sort by the configured sort method
|
|
|
|
type ncduSort struct {
|
|
|
|
sortPerm []int
|
|
|
|
entries fs.DirEntries
|
|
|
|
d *scan.Dir
|
|
|
|
u *UI
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is part of sort.Interface.
|
|
|
|
func (ds *ncduSort) Less(i, j int) bool {
|
2020-10-27 13:28:38 +00:00
|
|
|
var iAvgSize, jAvgSize float64
|
2020-12-28 14:08:12 +00:00
|
|
|
isize, icount, _, _, _, _ := ds.d.AttrI(ds.sortPerm[i])
|
|
|
|
jsize, jcount, _, _, _, _ := ds.d.AttrI(ds.sortPerm[j])
|
2017-04-01 18:53:44 +00:00
|
|
|
iname, jname := ds.entries[ds.sortPerm[i]].Remote(), ds.entries[ds.sortPerm[j]].Remote()
|
2020-10-27 13:28:38 +00:00
|
|
|
if icount > 0 {
|
|
|
|
iAvgSize = float64(isize / icount)
|
|
|
|
}
|
|
|
|
if jcount > 0 {
|
|
|
|
jAvgSize = float64(jsize / jcount)
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
switch {
|
|
|
|
case ds.u.sortByName < 0:
|
|
|
|
return iname > jname
|
|
|
|
case ds.u.sortByName > 0:
|
|
|
|
break
|
|
|
|
case ds.u.sortBySize < 0:
|
|
|
|
if isize != jsize {
|
|
|
|
return isize < jsize
|
|
|
|
}
|
|
|
|
case ds.u.sortBySize > 0:
|
|
|
|
if isize != jsize {
|
|
|
|
return isize > jsize
|
|
|
|
}
|
|
|
|
case ds.u.sortByCount < 0:
|
|
|
|
if icount != jcount {
|
|
|
|
return icount < jcount
|
|
|
|
}
|
|
|
|
case ds.u.sortByCount > 0:
|
|
|
|
if icount != jcount {
|
|
|
|
return icount > jcount
|
|
|
|
}
|
2020-10-27 13:28:38 +00:00
|
|
|
case ds.u.sortByAverageSize < 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize < jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
|
|
|
return isize < jsize
|
|
|
|
case ds.u.sortByAverageSize > 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize > jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
|
|
|
return isize > jsize
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
// if everything equal, sort by name
|
|
|
|
return iname < jname
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is part of sort.Interface.
|
|
|
|
func (ds *ncduSort) Swap(i, j int) {
|
|
|
|
ds.sortPerm[i], ds.sortPerm[j] = ds.sortPerm[j], ds.sortPerm[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len is part of sort.Interface.
|
|
|
|
func (ds *ncduSort) Len() int {
|
|
|
|
return len(ds.sortPerm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the permutation map of the current directory
|
|
|
|
func (u *UI) sortCurrentDir() {
|
|
|
|
u.sortPerm = u.sortPerm[:0]
|
|
|
|
for i := range u.entries {
|
|
|
|
u.sortPerm = append(u.sortPerm, i)
|
|
|
|
}
|
|
|
|
data := ncduSort{
|
|
|
|
sortPerm: u.sortPerm,
|
|
|
|
entries: u.entries,
|
|
|
|
d: u.d,
|
|
|
|
u: u,
|
|
|
|
}
|
|
|
|
sort.Sort(&data)
|
|
|
|
if len(u.invSortPerm) < len(u.sortPerm) {
|
|
|
|
u.invSortPerm = make([]int, len(u.sortPerm))
|
|
|
|
}
|
|
|
|
for i, j := range u.sortPerm {
|
|
|
|
u.invSortPerm[j] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCurrentDir sets the current directory
|
|
|
|
func (u *UI) setCurrentDir(d *scan.Dir) {
|
|
|
|
u.d = d
|
|
|
|
u.entries = d.Entries()
|
|
|
|
u.path = path.Join(u.fsName, d.Path())
|
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// enters the current entry
|
|
|
|
func (u *UI) enter() {
|
2017-12-12 13:52:58 +00:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
2017-04-01 18:53:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dirPos := u.dirPosMap[u.path]
|
|
|
|
d, _ := u.d.GetDir(u.sortPerm[dirPos.entry])
|
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.setCurrentDir(d)
|
|
|
|
}
|
|
|
|
|
2018-10-14 14:59:27 +00:00
|
|
|
// handles a box option that was selected
|
|
|
|
func (u *UI) handleBoxOption() {
|
|
|
|
msg, err := u.boxMenuHandler(u.f, u.path, u.boxMenuButton)
|
|
|
|
// reset
|
|
|
|
u.boxMenuButton = 0
|
|
|
|
u.boxMenu = []string{}
|
|
|
|
u.boxMenuHandler = nil
|
|
|
|
if err != nil {
|
|
|
|
u.popupBox([]string{
|
|
|
|
"error:",
|
|
|
|
err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u.popupBox([]string{"Finished:", msg})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-01 18:53:44 +00:00
|
|
|
// up goes up to the parent directory
|
|
|
|
func (u *UI) up() {
|
|
|
|
if u.d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parent := u.d.Parent()
|
|
|
|
if parent != nil {
|
|
|
|
u.setCurrentDir(parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// popupBox shows a box with the text in
|
|
|
|
func (u *UI) popupBox(text []string) {
|
|
|
|
u.boxText = text
|
|
|
|
u.showBox = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// togglePopupBox shows a box with the text in
|
|
|
|
func (u *UI) togglePopupBox(text []string) {
|
2019-06-23 05:40:54 +00:00
|
|
|
if u.showBox && reflect.DeepEqual(u.boxText, text) {
|
2017-04-01 18:53:44 +00:00
|
|
|
u.showBox = false
|
|
|
|
} else {
|
|
|
|
u.popupBox(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toggle the sorting for the flag passed in
|
|
|
|
func (u *UI) toggleSort(sortType *int8) {
|
|
|
|
old := *sortType
|
|
|
|
u.sortBySize = 0
|
|
|
|
u.sortByCount = 0
|
|
|
|
u.sortByName = 0
|
2020-10-27 13:28:38 +00:00
|
|
|
u.sortByAverageSize = 0
|
2017-04-01 18:53:44 +00:00
|
|
|
if old == 0 {
|
|
|
|
*sortType = 1
|
|
|
|
} else {
|
|
|
|
*sortType = -old
|
|
|
|
}
|
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUI creates a new user interface for ncdu on f
|
|
|
|
func NewUI(f fs.Fs) *UI {
|
|
|
|
return &UI{
|
2020-10-26 20:44:01 +00:00
|
|
|
f: f,
|
|
|
|
path: "Waiting for root...",
|
|
|
|
dirListHeight: 20, // updated in Draw
|
|
|
|
fsName: f.Name() + ":" + f.Root(),
|
|
|
|
showGraph: true,
|
|
|
|
showCounts: false,
|
|
|
|
showDirAverageSize: false,
|
|
|
|
sortByName: 0, // +1 for normal, 0 for off, -1 for reverse
|
|
|
|
sortBySize: 1,
|
|
|
|
sortByCount: 0,
|
|
|
|
dirPosMap: make(map[string]dirPos),
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show shows the user interface
|
|
|
|
func (u *UI) Show() error {
|
|
|
|
err := termbox.Init()
|
|
|
|
if err != nil {
|
2018-09-06 13:19:02 +00:00
|
|
|
return errors.Wrap(err, "termbox init")
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
defer termbox.Close()
|
|
|
|
|
|
|
|
// scan the disk in the background
|
|
|
|
u.listing = true
|
2019-06-17 08:34:30 +00:00
|
|
|
rootChan, errChan, updated := scan.Scan(context.Background(), u.f)
|
2017-04-01 18:53:44 +00:00
|
|
|
|
|
|
|
// Poll the events into a channel
|
|
|
|
events := make(chan termbox.Event)
|
|
|
|
doneWithEvent := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
events <- termbox.PollEvent()
|
|
|
|
<-doneWithEvent
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Main loop, waiting for events and channels
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
//Reset()
|
|
|
|
err := u.Draw()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "draw failed")
|
|
|
|
}
|
|
|
|
var root *scan.Dir
|
|
|
|
select {
|
|
|
|
case root = <-rootChan:
|
|
|
|
u.root = root
|
|
|
|
u.setCurrentDir(root)
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ncdu directory listing")
|
|
|
|
}
|
|
|
|
u.listing = false
|
|
|
|
case <-updated:
|
|
|
|
// redraw
|
|
|
|
// might want to limit updates per second
|
|
|
|
u.sortCurrentDir()
|
|
|
|
case ev := <-events:
|
|
|
|
doneWithEvent <- true
|
|
|
|
if ev.Type == termbox.EventKey {
|
|
|
|
switch ev.Key + termbox.Key(ev.Ch) {
|
|
|
|
case termbox.KeyEsc, termbox.KeyCtrlC, 'q':
|
|
|
|
if u.showBox {
|
|
|
|
u.showBox = false
|
|
|
|
} else {
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
case termbox.KeyArrowDown, 'j':
|
|
|
|
u.move(1)
|
|
|
|
case termbox.KeyArrowUp, 'k':
|
|
|
|
u.move(-1)
|
|
|
|
case termbox.KeyPgdn, '-', '_':
|
|
|
|
u.move(u.dirListHeight)
|
|
|
|
case termbox.KeyPgup, '=', '+':
|
|
|
|
u.move(-u.dirListHeight)
|
|
|
|
case termbox.KeyArrowLeft, 'h':
|
2018-10-14 14:59:27 +00:00
|
|
|
if u.showBox {
|
|
|
|
u.moveBox(-1)
|
|
|
|
break
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
u.up()
|
2018-10-14 14:59:27 +00:00
|
|
|
case termbox.KeyEnter:
|
|
|
|
if len(u.boxMenu) > 0 {
|
|
|
|
u.handleBoxOption()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
u.enter()
|
|
|
|
case termbox.KeyArrowRight, 'l':
|
|
|
|
if u.showBox {
|
|
|
|
u.moveBox(1)
|
|
|
|
break
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
u.enter()
|
|
|
|
case 'c':
|
|
|
|
u.showCounts = !u.showCounts
|
|
|
|
case 'g':
|
|
|
|
u.showGraph = !u.showGraph
|
2020-10-26 20:44:01 +00:00
|
|
|
case 'a':
|
|
|
|
u.showDirAverageSize = !u.showDirAverageSize
|
2017-04-01 18:53:44 +00:00
|
|
|
case 'n':
|
|
|
|
u.toggleSort(&u.sortByName)
|
|
|
|
case 's':
|
|
|
|
u.toggleSort(&u.sortBySize)
|
|
|
|
case 'C':
|
|
|
|
u.toggleSort(&u.sortByCount)
|
2020-10-27 13:28:38 +00:00
|
|
|
case 'A':
|
|
|
|
u.toggleSort(&u.sortByAverageSize)
|
2019-06-23 05:40:54 +00:00
|
|
|
case 'y':
|
|
|
|
u.copyPath()
|
|
|
|
case 'Y':
|
|
|
|
u.displayPath()
|
2018-10-14 14:59:27 +00:00
|
|
|
case 'd':
|
|
|
|
u.delete()
|
2017-04-01 18:53:44 +00:00
|
|
|
case '?':
|
2019-06-23 05:40:54 +00:00
|
|
|
u.togglePopupBox(helpText())
|
2018-03-24 17:55:20 +00:00
|
|
|
|
|
|
|
// Refresh the screen. Not obvious what key to map
|
|
|
|
// this onto, but ^L is a common choice.
|
|
|
|
case termbox.KeyCtrlL:
|
|
|
|
err := termbox.Sync()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "termbox sync returned error: %v", err)
|
|
|
|
}
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 22:07:12 +00:00
|
|
|
// listen to key presses, etc.
|
2017-04-01 18:53:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|