2022-06-19 18:22:45 +08:00
|
|
|
//go:build !plan9 && !js
|
|
|
|
// +build !plan9,!js
|
2017-06-16 03:10:54 +08:00
|
|
|
|
2022-08-28 19:21:57 +08:00
|
|
|
// Package ncdu implements a text based user interface for exploring a remote
|
2017-04-02 02:53:44 +08:00
|
|
|
package ncdu
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2017-04-02 02:53:44 +08:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2019-06-23 13:40:54 +08:00
|
|
|
"reflect"
|
2017-04-02 02:53:44 +08:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-23 13:40:54 +08:00
|
|
|
"github.com/atotto/clipboard"
|
2022-11-12 21:19:50 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2019-02-27 03:18:52 +08:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/ncdu/scan"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-11-09 23:09:12 +08:00
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
2023-01-18 01:57:56 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2022-11-12 21:19:50 +08:00
|
|
|
"github.com/rivo/uniseg"
|
2017-04-02 02:53:44 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 23:58:11 +08:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 23:58:11 +08:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-04-02 02:53:44 +08: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 19:22:52 +08:00
|
|
|
{{< asciinema 157793 >}}
|
2018-01-18 22:22:43 +08:00
|
|
|
|
2017-04-02 02:53:44 +08: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.
|
|
|
|
|
2022-04-06 04:27:07 +08:00
|
|
|
You can interact with the user interface using key presses,
|
|
|
|
press '?' to toggle the help on and off. The supported keys are:
|
2017-04-02 02:53:44 +08:00
|
|
|
|
2019-06-23 13:40:54 +08:00
|
|
|
` + strings.Join(helpText()[1:], "\n ") + `
|
2017-04-02 02:53:44 +08:00
|
|
|
|
2022-04-06 04:27:07 +08:00
|
|
|
Listed files/directories may be prefixed by a one-character flag,
|
2022-08-14 10:56:32 +08:00
|
|
|
some of them combined with a description in brackets at end of line.
|
2022-04-06 04:27:07 +08:00
|
|
|
These flags have the following meaning:
|
|
|
|
|
|
|
|
e means this is an empty directory, i.e. contains no files (but
|
|
|
|
may contain empty subdirectories)
|
|
|
|
~ means this is a directory where some of the files (possibly in
|
|
|
|
subdirectories) have unknown size, and therefore the directory
|
|
|
|
size may be underestimated (and average size inaccurate, as it
|
|
|
|
is average of the files with known sizes).
|
|
|
|
. means an error occurred while reading a subdirectory, and
|
|
|
|
therefore the directory size may be underestimated (and average
|
|
|
|
size inaccurate)
|
|
|
|
! means an error occurred while reading this directory
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
This an homage to the [ncdu tool](https://dev.yorhel.nl/ncdu) but for
|
2018-10-14 22:59:27 +08:00
|
|
|
rclone remotes. It is missing lots of features at the moment
|
|
|
|
but is useful as it stands.
|
|
|
|
|
2022-04-06 04:27:07 +08:00
|
|
|
Note that it might take some time to delete big files/directories. The
|
2018-10-14 22:59:27 +08:00
|
|
|
UI won't respond in the meantime since the deletion is done synchronously.
|
2022-06-19 21:51:37 +08:00
|
|
|
|
|
|
|
For a non-interactive listing of the remote, see the
|
|
|
|
[tree](/commands/rclone_tree/) command. To just get the total size of
|
|
|
|
the remote you can also use the [size](/commands/rclone_size/) command.
|
2017-04-02 02:53:44 +08:00
|
|
|
`,
|
2022-11-27 06:40:49 +08:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.37",
|
2023-07-11 01:34:10 +08:00
|
|
|
"groups": "Filter,Listing",
|
2022-11-27 06:40:49 +08:00
|
|
|
},
|
2017-04-02 02:53:44 +08: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 {
|
2023-01-17 21:44:54 +08:00
|
|
|
return NewUI(fsrc).Run()
|
2017-04-02 02:53:44 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-23 13:40:54 +08: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",
|
|
|
|
" g toggle graph",
|
2022-11-04 21:19:25 +08:00
|
|
|
" c toggle counts",
|
2020-10-27 04:44:01 +08:00
|
|
|
" a toggle average size in directory",
|
2022-11-04 21:19:25 +08:00
|
|
|
" m toggle modified time",
|
2021-04-03 02:17:32 +08:00
|
|
|
" u toggle human-readable format",
|
2022-11-04 21:19:25 +08:00
|
|
|
" n,s,C,A,M sort by name,size,count,asize,mtime",
|
2019-06-23 13:40:54 +08:00
|
|
|
" d delete file/directory",
|
2021-05-21 03:39:04 +08:00
|
|
|
" v select file/directory",
|
|
|
|
" V enter visual select mode",
|
|
|
|
" D delete selected files/directories",
|
2019-06-23 13:40:54 +08:00
|
|
|
}
|
|
|
|
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 08:17:24 +08:00
|
|
|
tr = append(tr, " y copy current path to clipboard")
|
2019-06-23 13:40:54 +08:00
|
|
|
}
|
|
|
|
tr = append(tr, []string{
|
|
|
|
" Y display current path",
|
2022-06-19 22:06:04 +08:00
|
|
|
" ^L refresh screen (fix screen corruption)",
|
2023-08-30 01:32:40 +08:00
|
|
|
" r recalculate file sizes",
|
2019-06-23 13:40:54 +08:00
|
|
|
" ? to toggle help on and off",
|
2022-06-19 21:52:23 +08:00
|
|
|
" q/ESC/^c to quit",
|
2019-06-23 13:40:54 +08:00
|
|
|
}...)
|
|
|
|
return
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UI contains the state of the user interface
|
|
|
|
type UI struct {
|
2022-11-12 21:19:50 +08:00
|
|
|
s tcell.Screen
|
2020-10-27 04:44:01 +08:00
|
|
|
f fs.Fs // fs being displayed
|
2023-08-30 01:32:40 +08:00
|
|
|
cancel func() // cancel the current scanning process
|
2020-10-27 04:44:01 +08:00
|
|
|
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
|
2022-11-04 21:19:25 +08:00
|
|
|
showModTime bool // toggle showing timestamps
|
2021-04-03 02:17:32 +08:00
|
|
|
humanReadable bool // toggle human-readable format
|
2021-05-21 03:39:04 +08:00
|
|
|
visualSelectMode bool // toggle visual selection mode
|
2022-11-04 21:19:25 +08:00
|
|
|
sortByName int8 // +1 for normal (lexical), 0 for off, -1 for reverse
|
|
|
|
sortBySize int8 // +1 for normal (largest first), 0 for off, -1 for reverse (smallest first)
|
2020-10-27 04:44:01 +08:00
|
|
|
sortByCount int8
|
|
|
|
sortByAverageSize int8
|
2022-11-04 21:19:25 +08:00
|
|
|
sortByModTime int8 // +1 for normal (newest first), 0 for off, -1 for reverse (oldest first)
|
2020-10-27 04:44:01 +08:00
|
|
|
dirPosMap map[string]dirPos // store for directory positions
|
2021-05-21 03:39:04 +08:00
|
|
|
selectedEntries map[string]dirPos // selected entries of current directory
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Where we have got to in the directory listing
|
|
|
|
type dirPos struct {
|
|
|
|
entry int
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
// graphemeWidth returns the number of cells in rs.
|
|
|
|
//
|
|
|
|
// The original [runewidth.StringWidth] iterates through graphemes
|
|
|
|
// and uses this same logic. To avoid iterating through graphemes
|
|
|
|
// repeatedly, we separate that out into its own function.
|
|
|
|
func graphemeWidth(rs []rune) (wd int) {
|
|
|
|
// copied/adapted from [runewidth.StringWidth]
|
|
|
|
for _, r := range rs {
|
|
|
|
wd = runewidth.RuneWidth(r)
|
|
|
|
if wd > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
// Print a string
|
2022-11-12 21:19:50 +08:00
|
|
|
func (u *UI) Print(x, y int, style tcell.Style, msg string) {
|
|
|
|
g := uniseg.NewGraphemes(msg)
|
|
|
|
for g.Next() {
|
|
|
|
rs := g.Runes()
|
|
|
|
u.s.SetContent(x, y, rs[0], rs[1:], style)
|
|
|
|
x += graphemeWidth(rs)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf a string
|
2022-11-12 21:19:50 +08:00
|
|
|
func (u *UI) Printf(x, y int, style tcell.Style, format string, args ...interface{}) {
|
2017-04-02 02:53:44 +08:00
|
|
|
s := fmt.Sprintf(format, args...)
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Print(x, y, style, s)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line prints a string to given xmax, with given space
|
2022-11-12 21:19:50 +08:00
|
|
|
func (u *UI) Line(x, y, xmax int, style tcell.Style, spacer rune, msg string) {
|
|
|
|
g := uniseg.NewGraphemes(msg)
|
|
|
|
for g.Next() {
|
|
|
|
rs := g.Runes()
|
|
|
|
u.s.SetContent(x, y, rs[0], rs[1:], style)
|
|
|
|
x += graphemeWidth(rs)
|
2017-04-02 02:53:44 +08:00
|
|
|
if x >= xmax {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ; x < xmax; x++ {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x, y, spacer, nil, style)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linef a string
|
2022-11-12 21:19:50 +08:00
|
|
|
func (u *UI) Linef(x, y, xmax int, style tcell.Style, spacer rune, format string, args ...interface{}) {
|
2017-04-02 02:53:44 +08:00
|
|
|
s := fmt.Sprintf(format, args...)
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Line(x, y, xmax, style, spacer, s)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 22:59:27 +08:00
|
|
|
// LineOptions Print line of selectable options
|
2022-11-12 21:19:50 +08:00
|
|
|
func (u *UI) LineOptions(x, y, xmax int, style tcell.Style, options []string, selected int) {
|
|
|
|
for x := x; x < xmax; x++ {
|
|
|
|
u.s.SetContent(x, y, ' ', nil, style) // fill
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
2022-11-12 21:19:50 +08:00
|
|
|
x += ((xmax - x) - lineOptionLength(options)) / 2 // center
|
2018-10-14 22:59:27 +08:00
|
|
|
|
|
|
|
for i, o := range options {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x, y, ' ', nil, style)
|
|
|
|
x++
|
2018-10-14 22:59:27 +08:00
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
ostyle := style
|
2018-10-14 22:59:27 +08:00
|
|
|
if i == selected {
|
2022-11-12 21:19:50 +08:00
|
|
|
ostyle = tcell.StyleDefault
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x, y, '<', nil, ostyle)
|
|
|
|
x++
|
|
|
|
|
|
|
|
g := uniseg.NewGraphemes(o)
|
|
|
|
for g.Next() {
|
|
|
|
rs := g.Runes()
|
|
|
|
u.s.SetContent(x, y, rs[0], rs[1:], ostyle)
|
|
|
|
x += graphemeWidth(rs)
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x, y, '>', nil, ostyle)
|
|
|
|
x++
|
2018-10-14 22:59:27 +08:00
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x, y, ' ', nil, style)
|
|
|
|
x++
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02 02:53:44 +08:00
|
|
|
// Box the u.boxText onto the screen
|
|
|
|
func (u *UI) Box() {
|
2022-11-12 21:19:50 +08:00
|
|
|
w, h := u.s.Size()
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// 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 22:59:27 +08: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-02 02:53:44 +08:00
|
|
|
|
|
|
|
// draw text
|
2022-11-12 21:19:50 +08:00
|
|
|
style := tcell.StyleDefault.Background(tcell.ColorRed).Reverse(true)
|
2017-04-02 02:53:44 +08:00
|
|
|
for i, s := range u.boxText {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Line(x, y+i, xmax, style, ' ', s)
|
|
|
|
style = tcell.StyleDefault.Reverse(true)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
2018-10-14 22:59:27 +08:00
|
|
|
if len(u.boxMenu) != 0 {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.LineOptions(x, ymax, xmax, style, u.boxMenu, u.boxMenuButton)
|
2018-10-14 22:59:27 +08:00
|
|
|
ymax++
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw top border
|
|
|
|
for i := y; i < ymax; i++ {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x-1, i, tcell.RuneVLine, nil, style)
|
|
|
|
u.s.SetContent(xmax, i, tcell.RuneVLine, nil, style)
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
for j := x; j < xmax; j++ {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(j, y-1, tcell.RuneHLine, nil, style)
|
|
|
|
u.s.SetContent(j, ymax, tcell.RuneHLine, nil, style)
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.SetContent(x-1, y-1, tcell.RuneULCorner, nil, style)
|
|
|
|
u.s.SetContent(xmax, y-1, tcell.RuneURCorner, nil, style)
|
|
|
|
u.s.SetContent(x-1, ymax, tcell.RuneLLCorner, nil, style)
|
|
|
|
u.s.SetContent(xmax, ymax, tcell.RuneLRCorner, nil, style)
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-02 02:53:44 +08: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 {
|
2022-04-11 23:06:01 +08:00
|
|
|
attrs, _ := u.d.AttrI(u.sortPerm[i])
|
|
|
|
if attrs.Size > biggest {
|
|
|
|
biggest = attrs.Size
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-30 23:54:59 +08: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 {
|
2022-04-11 23:06:01 +08:00
|
|
|
attrs, _ := u.d.AttrI(u.sortPerm[i])
|
|
|
|
if attrs.IsDir && attrs.Count == 0 {
|
2020-10-30 23:54:59 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
// Draw the current screen
|
2023-01-17 21:44:54 +08:00
|
|
|
func (u *UI) Draw() {
|
2022-11-04 21:19:25 +08:00
|
|
|
ctx := context.Background()
|
2022-11-12 21:19:50 +08:00
|
|
|
w, h := u.s.Size()
|
2017-04-02 02:53:44 +08:00
|
|
|
u.dirListHeight = h - 3
|
|
|
|
|
|
|
|
// Plot
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.Clear()
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// Header line
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Linef(0, 0, w, tcell.StyleDefault.Reverse(true), ' ', "rclone ncdu %s - use the arrow keys to navigate, press ? for help", fs.Version)
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// Directory line
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Linef(0, 1, w, tcell.StyleDefault, '-', "-- %s ", u.path)
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// 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 23:54:59 +08:00
|
|
|
showEmptyDir := u.hasEmptyDir()
|
2017-04-02 02:53:44 +08: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
|
|
|
|
}
|
2022-11-04 21:19:25 +08:00
|
|
|
var attrs scan.Attrs
|
|
|
|
var err error
|
|
|
|
if u.showModTime {
|
|
|
|
attrs, err = u.d.AttrWithModTimeI(ctx, u.sortPerm[n])
|
|
|
|
} else {
|
|
|
|
attrs, err = u.d.AttrI(u.sortPerm[n])
|
|
|
|
}
|
2021-05-21 03:39:04 +08:00
|
|
|
_, isSelected := u.selectedEntries[entry.String()]
|
2022-11-12 21:19:50 +08:00
|
|
|
style := tcell.StyleDefault
|
2022-04-11 23:06:01 +08:00
|
|
|
if attrs.EntriesHaveErrors {
|
2022-11-12 21:19:50 +08:00
|
|
|
style = style.Foreground(tcell.ColorYellow)
|
2020-12-28 22:08:12 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-11-12 21:19:50 +08:00
|
|
|
style = style.Foreground(tcell.ColorRed)
|
2020-12-28 22:08:12 +08:00
|
|
|
}
|
2021-05-21 03:39:04 +08:00
|
|
|
if isSelected {
|
2022-11-12 21:19:50 +08:00
|
|
|
style = style.Foreground(tcell.ColorLightYellow)
|
2021-05-21 03:39:04 +08:00
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
if n == dirPos.entry {
|
2022-11-12 21:19:50 +08:00
|
|
|
style = style.Reverse(true)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
mark := ' '
|
2022-04-11 23:06:01 +08:00
|
|
|
if attrs.IsDir {
|
2017-04-02 02:53:44 +08:00
|
|
|
mark = '/'
|
|
|
|
}
|
2020-12-28 23:20:23 +08:00
|
|
|
fileFlag := ' '
|
2017-04-02 02:53:44 +08:00
|
|
|
message := ""
|
2022-04-11 23:06:01 +08:00
|
|
|
if !attrs.Readable {
|
2017-04-02 02:53:44 +08:00
|
|
|
message = " [not read yet]"
|
|
|
|
}
|
2022-04-11 23:06:01 +08:00
|
|
|
if attrs.CountUnknownSize > 0 {
|
|
|
|
message = fmt.Sprintf(" [%d of %d files have unknown size, size may be underestimated]", attrs.CountUnknownSize, attrs.Count)
|
2022-04-05 21:46:56 +08:00
|
|
|
fileFlag = '~'
|
|
|
|
}
|
2022-04-11 23:06:01 +08:00
|
|
|
if attrs.EntriesHaveErrors {
|
2020-12-28 22:08:12 +08:00
|
|
|
message = " [some subdirectories could not be read, size may be underestimated]"
|
2020-12-28 23:20:23 +08:00
|
|
|
fileFlag = '.'
|
2020-12-28 22:08:12 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
message = fmt.Sprintf(" [%s]", err)
|
2020-12-28 23:20:23 +08:00
|
|
|
fileFlag = '!'
|
2020-12-28 22:08:12 +08:00
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
extras := ""
|
|
|
|
if u.showCounts {
|
2022-04-11 23:06:01 +08:00
|
|
|
ss := operations.CountStringField(attrs.Count, u.humanReadable, 9) + " "
|
|
|
|
if attrs.Count > 0 {
|
2021-04-03 02:17:32 +08:00
|
|
|
extras += ss
|
2017-04-02 02:53:44 +08:00
|
|
|
} else {
|
2021-04-03 02:17:32 +08:00
|
|
|
extras += strings.Repeat(" ", len(ss))
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
2020-10-27 04:44:01 +08:00
|
|
|
}
|
|
|
|
if u.showDirAverageSize {
|
2022-04-11 23:06:01 +08:00
|
|
|
avg := attrs.AverageSize()
|
|
|
|
ss := operations.SizeStringField(int64(avg), u.humanReadable, 9) + " "
|
|
|
|
if avg > 0 {
|
2021-04-03 02:17:32 +08:00
|
|
|
extras += ss
|
2020-10-27 04:44:01 +08:00
|
|
|
} else {
|
2021-04-03 02:17:32 +08:00
|
|
|
extras += strings.Repeat(" ", len(ss))
|
2020-10-27 04:44:01 +08:00
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
2022-11-04 21:19:25 +08:00
|
|
|
if u.showModTime {
|
|
|
|
extras += attrs.ModTime.Local().Format("2006-01-02 15:04:05") + " "
|
|
|
|
}
|
2020-10-30 23:54:59 +08:00
|
|
|
if showEmptyDir {
|
2022-04-11 23:06:01 +08:00
|
|
|
if attrs.IsDir && attrs.Count == 0 && fileFlag == ' ' {
|
2020-12-28 23:20:23 +08:00
|
|
|
fileFlag = 'e'
|
2020-10-30 23:54:59 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
if u.showGraph {
|
2022-04-11 23:06:01 +08:00
|
|
|
bars := (attrs.Size + perBar/2 - 1) / perBar
|
2017-04-02 02:53:44 +08:00
|
|
|
// 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] + "] "
|
|
|
|
}
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Linef(0, y, w, style, ' ', "%c %s %s%c%s%s",
|
|
|
|
fileFlag, operations.SizeStringField(attrs.Size, u.humanReadable, 12), extras, mark, path.Base(entry.Remote()), message)
|
2017-04-02 02:53:44 +08:00
|
|
|
y++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Footer
|
|
|
|
if u.d == nil {
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Line(0, h-1, w, tcell.StyleDefault.Reverse(true), ' ', "Waiting for root directory...")
|
2017-04-02 02:53:44 +08:00
|
|
|
} else {
|
|
|
|
message := ""
|
|
|
|
if u.listing {
|
|
|
|
message = " [listing in progress]"
|
|
|
|
}
|
|
|
|
size, count := u.d.Attr()
|
2022-11-12 21:19:50 +08:00
|
|
|
u.Linef(0, h-1, w, tcell.StyleDefault.Reverse(true), ' ', "Total usage: %s, Objects: %s%s",
|
|
|
|
operations.SizeString(size, u.humanReadable), operations.CountString(count, u.humanReadable), message)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 20:06:24 +08:00
|
|
|
// Show the box on top if required
|
2017-04-02 02:53:44 +08:00
|
|
|
if u.showBox {
|
|
|
|
u.Box()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 21:52:58 +08:00
|
|
|
if entries == 0 || dirPos.offset < 0 {
|
2017-04-02 02:53:44 +08:00
|
|
|
dirPos.offset = 0
|
|
|
|
} else if dirPos.offset >= entries {
|
|
|
|
dirPos.offset = entries - 1
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:39:04 +08:00
|
|
|
// toggle the current file for selection in selection mode
|
|
|
|
if u.visualSelectMode {
|
|
|
|
u.toggleSelectForCursor()
|
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
// write dirPos back for later
|
|
|
|
u.dirPosMap[u.path] = dirPos
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:59:27 +08:00
|
|
|
func (u *UI) removeEntry(pos int) {
|
|
|
|
u.d.Remove(pos)
|
|
|
|
u.setCurrentDir(u.d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UI) delete() {
|
2021-04-15 00:20:17 +08:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-05-21 03:39:04 +08:00
|
|
|
if len(u.selectedEntries) > 0 {
|
|
|
|
u.deleteSelected()
|
|
|
|
} else {
|
|
|
|
u.deleteSingle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the entry at the current position
|
|
|
|
func (u *UI) deleteSingle() {
|
2019-06-17 16:34:30 +08:00
|
|
|
ctx := context.Background()
|
2021-04-15 00:20:17 +08:00
|
|
|
cursorPos := u.dirPosMap[u.path]
|
|
|
|
dirPos := u.sortPerm[cursorPos.entry]
|
|
|
|
dirEntry := u.entries[dirPos]
|
2018-10-14 22:59:27 +08:00
|
|
|
u.boxMenu = []string{"cancel", "confirm"}
|
2021-04-15 00:20:17 +08:00
|
|
|
if obj, isFile := dirEntry.(fs.Object); isFile {
|
2018-10-14 22:59:27 +08:00
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
err := operations.DeleteFile(ctx, obj)
|
2018-10-14 22:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-15 00:20:17 +08:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 22:59:27 +08:00
|
|
|
return "Successfully deleted file!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Delete this file?",
|
2021-11-09 23:09:12 +08:00
|
|
|
fspath.JoinRootPath(u.fsName, dirEntry.String())})
|
2018-10-14 22:59:27 +08:00
|
|
|
} else {
|
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2021-04-15 00:20:17 +08:00
|
|
|
err := operations.Purge(ctx, f, dirEntry.String())
|
2018-10-14 22:59:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-15 00:20:17 +08:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 22:59:27 +08:00
|
|
|
return "Successfully purged folder!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Purge this directory?",
|
|
|
|
"ALL files in it will be deleted",
|
2021-11-09 23:09:12 +08:00
|
|
|
fspath.JoinRootPath(u.fsName, dirEntry.String())})
|
2018-10-14 22:59:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:39:04 +08:00
|
|
|
func (u *UI) deleteSelected() {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
u.boxMenu = []string{"cancel", "confirm"}
|
|
|
|
|
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
positionsToDelete := make([]int, len(u.selectedEntries))
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for key, cursorPos := range u.selectedEntries {
|
|
|
|
|
|
|
|
dirPos := u.sortPerm[cursorPos.entry]
|
|
|
|
dirEntry := u.entries[dirPos]
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if obj, isFile := dirEntry.(fs.Object); isFile {
|
|
|
|
err = operations.DeleteFile(ctx, obj)
|
|
|
|
} else {
|
|
|
|
err = operations.Purge(ctx, f, dirEntry.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(u.selectedEntries, key)
|
|
|
|
positionsToDelete[i] = dirPos
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleting all entries at once, as doing it during the deletions
|
|
|
|
// could cause issues.
|
|
|
|
sort.Slice(positionsToDelete, func(i, j int) bool {
|
|
|
|
return positionsToDelete[i] > positionsToDelete[j]
|
|
|
|
})
|
|
|
|
for _, dirPos := range positionsToDelete {
|
|
|
|
u.removeEntry(dirPos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// move cursor at end if needed
|
|
|
|
cursorPos := u.dirPosMap[u.path]
|
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Successfully deleted all items!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Delete selected items?",
|
|
|
|
fmt.Sprintf("ALL %d items will be deleted", len(u.selectedEntries))})
|
|
|
|
}
|
|
|
|
|
2019-06-23 13:40:54 +08: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-02 02:53:44 +08: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 21:28:38 +08:00
|
|
|
var iAvgSize, jAvgSize float64
|
2022-11-04 21:19:25 +08:00
|
|
|
var iattrs, jattrs scan.Attrs
|
|
|
|
if ds.u.sortByModTime != 0 {
|
|
|
|
ctx := context.Background()
|
|
|
|
iattrs, _ = ds.d.AttrWithModTimeI(ctx, ds.sortPerm[i])
|
|
|
|
jattrs, _ = ds.d.AttrWithModTimeI(ctx, ds.sortPerm[j])
|
|
|
|
} else {
|
|
|
|
iattrs, _ = ds.d.AttrI(ds.sortPerm[i])
|
|
|
|
jattrs, _ = ds.d.AttrI(ds.sortPerm[j])
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
iname, jname := ds.entries[ds.sortPerm[i]].Remote(), ds.entries[ds.sortPerm[j]].Remote()
|
2022-04-11 23:06:01 +08:00
|
|
|
if iattrs.Count > 0 {
|
|
|
|
iAvgSize = iattrs.AverageSize()
|
2020-10-27 21:28:38 +08:00
|
|
|
}
|
2022-04-11 23:06:01 +08:00
|
|
|
if jattrs.Count > 0 {
|
|
|
|
jAvgSize = jattrs.AverageSize()
|
2020-10-27 21:28:38 +08:00
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
switch {
|
|
|
|
case ds.u.sortByName < 0:
|
|
|
|
return iname > jname
|
|
|
|
case ds.u.sortByName > 0:
|
|
|
|
break
|
|
|
|
case ds.u.sortBySize < 0:
|
2022-04-11 23:06:01 +08:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size < jattrs.Size
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
case ds.u.sortBySize > 0:
|
2022-04-11 23:06:01 +08:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size > jattrs.Size
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
2022-11-04 21:19:25 +08:00
|
|
|
case ds.u.sortByModTime < 0:
|
|
|
|
if iattrs.ModTime != jattrs.ModTime {
|
|
|
|
return iattrs.ModTime.Before(jattrs.ModTime)
|
|
|
|
}
|
|
|
|
case ds.u.sortByModTime > 0:
|
|
|
|
if iattrs.ModTime != jattrs.ModTime {
|
|
|
|
return iattrs.ModTime.After(jattrs.ModTime)
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
case ds.u.sortByCount < 0:
|
2022-04-11 23:06:01 +08:00
|
|
|
if iattrs.Count != jattrs.Count {
|
|
|
|
return iattrs.Count < jattrs.Count
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
case ds.u.sortByCount > 0:
|
2022-04-11 23:06:01 +08:00
|
|
|
if iattrs.Count != jattrs.Count {
|
|
|
|
return iattrs.Count > jattrs.Count
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
2020-10-27 21:28:38 +08:00
|
|
|
case ds.u.sortByAverageSize < 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize < jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
2022-11-04 19:08:41 +08:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size < jattrs.Size
|
|
|
|
}
|
2020-10-27 21:28:38 +08:00
|
|
|
case ds.u.sortByAverageSize > 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize > jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
2022-11-04 19:08:41 +08:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size > jattrs.Size
|
|
|
|
}
|
2017-04-02 02:53:44 +08: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()
|
2021-11-09 23:09:12 +08:00
|
|
|
u.path = fspath.JoinRootPath(u.fsName, d.Path())
|
2021-05-21 03:39:04 +08:00
|
|
|
u.selectedEntries = make(map[string]dirPos)
|
|
|
|
u.visualSelectMode = false
|
2017-04-02 02:53:44 +08:00
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// enters the current entry
|
|
|
|
func (u *UI) enter() {
|
2017-12-12 21:52:58 +08:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
2017-04-02 02:53:44 +08: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 22:59:27 +08: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-02 02:53:44 +08: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 13:40:54 +08:00
|
|
|
if u.showBox && reflect.DeepEqual(u.boxText, text) {
|
2017-04-02 02:53:44 +08: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 21:28:38 +08:00
|
|
|
u.sortByAverageSize = 0
|
2017-04-02 02:53:44 +08:00
|
|
|
if old == 0 {
|
|
|
|
*sortType = 1
|
|
|
|
} else {
|
|
|
|
*sortType = -old
|
|
|
|
}
|
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
2021-05-21 03:39:04 +08:00
|
|
|
func (u *UI) toggleSelectForCursor() {
|
|
|
|
cursorPos := u.dirPosMap[u.path]
|
|
|
|
dirPos := u.sortPerm[cursorPos.entry]
|
|
|
|
dirEntry := u.entries[dirPos]
|
|
|
|
|
|
|
|
_, present := u.selectedEntries[dirEntry.String()]
|
|
|
|
|
|
|
|
if present {
|
|
|
|
delete(u.selectedEntries, dirEntry.String())
|
|
|
|
} else {
|
|
|
|
u.selectedEntries[dirEntry.String()] = cursorPos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
// NewUI creates a new user interface for ncdu on f
|
|
|
|
func NewUI(f fs.Fs) *UI {
|
|
|
|
return &UI{
|
2020-10-27 04:44:01 +08:00
|
|
|
f: f,
|
|
|
|
path: "Waiting for root...",
|
|
|
|
dirListHeight: 20, // updated in Draw
|
2021-11-09 23:09:12 +08:00
|
|
|
fsName: fs.ConfigString(f),
|
2020-10-27 04:44:01 +08:00
|
|
|
showGraph: true,
|
|
|
|
showCounts: false,
|
|
|
|
showDirAverageSize: false,
|
2021-04-03 02:17:32 +08:00
|
|
|
humanReadable: true,
|
2022-11-04 21:19:25 +08:00
|
|
|
sortByName: 0,
|
|
|
|
sortBySize: 1, // Sort by largest first
|
|
|
|
sortByModTime: 0,
|
2020-10-27 04:44:01 +08:00
|
|
|
sortByCount: 0,
|
|
|
|
dirPosMap: make(map[string]dirPos),
|
2021-05-21 03:39:04 +08:00
|
|
|
selectedEntries: make(map[string]dirPos),
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 01:32:40 +08:00
|
|
|
func (u *UI) scan() (chan *scan.Dir, chan error, chan struct{}) {
|
|
|
|
if cancel := u.cancel; cancel != nil {
|
|
|
|
cancel()
|
|
|
|
}
|
2023-08-30 21:29:46 +08:00
|
|
|
u.listing = true
|
2023-08-30 01:32:40 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, u.cancel = context.WithCancel(ctx)
|
|
|
|
return scan.Scan(ctx, u.f)
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:44:54 +08:00
|
|
|
// Run shows the user interface
|
|
|
|
func (u *UI) Run() error {
|
2022-11-12 21:19:50 +08:00
|
|
|
var err error
|
|
|
|
u.s, err = tcell.NewScreen()
|
2017-04-02 02:53:44 +08:00
|
|
|
if err != nil {
|
2022-11-12 21:19:50 +08:00
|
|
|
return fmt.Errorf("screen new: %w", err)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
2022-11-12 21:19:50 +08:00
|
|
|
err = u.s.Init()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("screen init: %w", err)
|
|
|
|
}
|
2023-01-18 01:57:56 +08:00
|
|
|
|
|
|
|
// Hijack fs.LogPrint so that it doesn't corrupt the screen.
|
|
|
|
if logPrint := fs.LogPrint; !log.Redirected() {
|
|
|
|
type log struct {
|
|
|
|
text string
|
|
|
|
level fs.LogLevel
|
|
|
|
}
|
|
|
|
var logs []log
|
|
|
|
fs.LogPrint = func(level fs.LogLevel, text string) {
|
|
|
|
if len(logs) > 100 {
|
|
|
|
logs = logs[len(logs)-100:]
|
|
|
|
}
|
|
|
|
logs = append(logs, log{level: level, text: text})
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
fs.LogPrint = logPrint
|
|
|
|
for i := range logs {
|
|
|
|
logPrint(logs[i].level, logs[i].text)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-11-12 21:19:50 +08:00
|
|
|
defer u.s.Fini()
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// scan the disk in the background
|
2023-08-30 01:32:40 +08:00
|
|
|
rootChan, errChan, updated := u.scan()
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// Poll the events into a channel
|
2022-11-12 21:19:50 +08:00
|
|
|
events := make(chan tcell.Event)
|
|
|
|
go u.s.ChannelEvents(events, nil)
|
2017-04-02 02:53:44 +08:00
|
|
|
|
|
|
|
// Main loop, waiting for events and channels
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
2022-11-12 21:19:50 +08:00
|
|
|
case root := <-rootChan:
|
2017-04-02 02:53:44 +08:00
|
|
|
u.root = root
|
|
|
|
u.setCurrentDir(root)
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return fmt.Errorf("ncdu directory listing: %w", err)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
u.listing = false
|
|
|
|
case <-updated:
|
2022-11-12 21:19:50 +08:00
|
|
|
// TODO: might want to limit updates per second
|
2017-04-02 02:53:44 +08:00
|
|
|
u.sortCurrentDir()
|
|
|
|
case ev := <-events:
|
2022-11-12 21:19:50 +08:00
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventResize:
|
2023-01-17 21:44:54 +08:00
|
|
|
u.Draw()
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.Sync()
|
2023-01-17 21:44:54 +08:00
|
|
|
continue // don't draw again
|
2022-11-12 21:19:50 +08:00
|
|
|
case *tcell.EventKey:
|
|
|
|
var c rune
|
|
|
|
if k := ev.Key(); k == tcell.KeyRune {
|
|
|
|
c = ev.Rune()
|
|
|
|
} else {
|
|
|
|
c = key(k)
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case key(tcell.KeyEsc), key(tcell.KeyCtrlC), 'q':
|
2017-04-02 02:53:44 +08:00
|
|
|
if u.showBox {
|
|
|
|
u.showBox = false
|
|
|
|
} else {
|
|
|
|
break outer
|
|
|
|
}
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyDown), 'j':
|
2017-04-02 02:53:44 +08:00
|
|
|
u.move(1)
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyUp), 'k':
|
2017-04-02 02:53:44 +08:00
|
|
|
u.move(-1)
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyPgDn), '-', '_':
|
2017-04-02 02:53:44 +08:00
|
|
|
u.move(u.dirListHeight)
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyPgUp), '=', '+':
|
2017-04-02 02:53:44 +08:00
|
|
|
u.move(-u.dirListHeight)
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyLeft), 'h':
|
2018-10-14 22:59:27 +08:00
|
|
|
if u.showBox {
|
|
|
|
u.moveBox(-1)
|
|
|
|
break
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
u.up()
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyEnter):
|
2018-10-14 22:59:27 +08:00
|
|
|
if len(u.boxMenu) > 0 {
|
|
|
|
u.handleBoxOption()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
u.enter()
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyRight), 'l':
|
2018-10-14 22:59:27 +08:00
|
|
|
if u.showBox {
|
|
|
|
u.moveBox(1)
|
|
|
|
break
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
u.enter()
|
|
|
|
case 'c':
|
|
|
|
u.showCounts = !u.showCounts
|
2022-11-04 21:19:25 +08:00
|
|
|
case 'm':
|
|
|
|
u.showModTime = !u.showModTime
|
2017-04-02 02:53:44 +08:00
|
|
|
case 'g':
|
|
|
|
u.showGraph = !u.showGraph
|
2020-10-27 04:44:01 +08:00
|
|
|
case 'a':
|
|
|
|
u.showDirAverageSize = !u.showDirAverageSize
|
2017-04-02 02:53:44 +08:00
|
|
|
case 'n':
|
|
|
|
u.toggleSort(&u.sortByName)
|
|
|
|
case 's':
|
|
|
|
u.toggleSort(&u.sortBySize)
|
2022-11-04 21:19:25 +08:00
|
|
|
case 'M':
|
|
|
|
u.toggleSort(&u.sortByModTime)
|
2021-05-21 03:39:04 +08:00
|
|
|
case 'v':
|
|
|
|
u.toggleSelectForCursor()
|
|
|
|
case 'V':
|
|
|
|
u.visualSelectMode = !u.visualSelectMode
|
2017-04-02 02:53:44 +08:00
|
|
|
case 'C':
|
|
|
|
u.toggleSort(&u.sortByCount)
|
2020-10-27 21:28:38 +08:00
|
|
|
case 'A':
|
|
|
|
u.toggleSort(&u.sortByAverageSize)
|
2019-06-23 13:40:54 +08:00
|
|
|
case 'y':
|
|
|
|
u.copyPath()
|
|
|
|
case 'Y':
|
|
|
|
u.displayPath()
|
2018-10-14 22:59:27 +08:00
|
|
|
case 'd':
|
|
|
|
u.delete()
|
2021-04-03 02:17:32 +08:00
|
|
|
case 'u':
|
|
|
|
u.humanReadable = !u.humanReadable
|
2021-05-21 03:39:04 +08:00
|
|
|
case 'D':
|
|
|
|
u.deleteSelected()
|
2017-04-02 02:53:44 +08:00
|
|
|
case '?':
|
2019-06-23 13:40:54 +08:00
|
|
|
u.togglePopupBox(helpText())
|
2023-08-30 01:32:40 +08:00
|
|
|
case 'r':
|
|
|
|
// restart scan
|
|
|
|
rootChan, errChan, updated = u.scan()
|
2018-03-25 01:55:20 +08:00
|
|
|
|
|
|
|
// Refresh the screen. Not obvious what key to map
|
|
|
|
// this onto, but ^L is a common choice.
|
2022-11-12 21:19:50 +08:00
|
|
|
case key(tcell.KeyCtrlL):
|
2023-01-17 21:44:54 +08:00
|
|
|
u.Draw()
|
2022-11-12 21:19:50 +08:00
|
|
|
u.s.Sync()
|
2023-01-17 21:44:54 +08:00
|
|
|
continue // don't draw again
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 21:44:54 +08:00
|
|
|
|
|
|
|
u.Draw()
|
|
|
|
u.s.Show()
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-12 21:19:50 +08:00
|
|
|
|
2022-11-27 06:02:45 +08:00
|
|
|
// key returns a rune representing the key k. It is a negative value, to not collide with Unicode code-points.
|
2022-11-12 21:19:50 +08:00
|
|
|
func key(k tcell.Key) rune {
|
2022-11-27 06:02:45 +08:00
|
|
|
return rune(-k)
|
2022-11-12 21:19:50 +08:00
|
|
|
}
|