2022-06-19 12:22:45 +02:00
|
|
|
//go:build !plan9 && !js
|
|
|
|
// +build !plan9,!js
|
2017-06-15 21:10:54 +02:00
|
|
|
|
2022-08-28 13:21:57 +02:00
|
|
|
// Package ncdu implements a text based user interface for exploring a remote
|
2017-04-01 20:53:44 +02:00
|
|
|
package ncdu
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2017-04-01 20:53:44 +02:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2019-06-23 07:40:54 +02:00
|
|
|
"reflect"
|
2017-04-01 20:53:44 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-06-23 07:40:54 +02:00
|
|
|
"github.com/atotto/clipboard"
|
2022-06-19 12:22:45 +02:00
|
|
|
"github.com/gdamore/tcell/v2/termbox"
|
2019-02-26 20:18:52 +01:00
|
|
|
runewidth "github.com/mattn/go-runewidth"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/ncdu/scan"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-11-09 16:09:12 +01:00
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-04-01 20:53:44 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-04-01 20:53:44 +02: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 13:22:52 +02:00
|
|
|
{{< asciinema 157793 >}}
|
2018-01-18 15:22:43 +01:00
|
|
|
|
2017-04-01 20:53:44 +02: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-05 22:27:07 +02: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-01 20:53:44 +02:00
|
|
|
|
2019-06-23 07:40:54 +02:00
|
|
|
` + strings.Join(helpText()[1:], "\n ") + `
|
2017-04-01 20:53:44 +02:00
|
|
|
|
2022-04-05 22:27:07 +02:00
|
|
|
Listed files/directories may be prefixed by a one-character flag,
|
2022-08-14 04:56:32 +02:00
|
|
|
some of them combined with a description in brackets at end of line.
|
2022-04-05 22:27:07 +02: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-01 20:53:44 +02:00
|
|
|
This an homage to the [ncdu tool](https://dev.yorhel.nl/ncdu) but for
|
2018-10-14 16:59:27 +02:00
|
|
|
rclone remotes. It is missing lots of features at the moment
|
|
|
|
but is useful as it stands.
|
|
|
|
|
2022-04-05 22:27:07 +02:00
|
|
|
Note that it might take some time to delete big files/directories. The
|
2018-10-14 16:59:27 +02:00
|
|
|
UI won't respond in the meantime since the deletion is done synchronously.
|
2022-06-19 15:51:37 +02: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-01 20:53:44 +02: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 07:40:54 +02: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 21:44:01 +01:00
|
|
|
" a toggle average size in directory",
|
2021-04-02 20:17:32 +02:00
|
|
|
" u toggle human-readable format",
|
2020-10-27 14:28:38 +01:00
|
|
|
" n,s,C,A sort by name,size,count,average size",
|
2019-06-23 07:40:54 +02:00
|
|
|
" d delete file/directory",
|
2021-05-20 21:39:04 +02:00
|
|
|
" v select file/directory",
|
|
|
|
" V enter visual select mode",
|
|
|
|
" D delete selected files/directories",
|
2019-06-23 07:40:54 +02: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 02:17:24 +02:00
|
|
|
tr = append(tr, " y copy current path to clipboard")
|
2019-06-23 07:40:54 +02:00
|
|
|
}
|
|
|
|
tr = append(tr, []string{
|
|
|
|
" Y display current path",
|
2022-06-19 16:06:04 +02:00
|
|
|
" ^L refresh screen (fix screen corruption)",
|
2019-06-23 07:40:54 +02:00
|
|
|
" ? to toggle help on and off",
|
2022-06-19 15:52:23 +02:00
|
|
|
" q/ESC/^c to quit",
|
2019-06-23 07:40:54 +02:00
|
|
|
}...)
|
|
|
|
return
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UI contains the state of the user interface
|
|
|
|
type UI struct {
|
2020-10-26 21:44:01 +01: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
|
2021-04-02 20:17:32 +02:00
|
|
|
humanReadable bool // toggle human-readable format
|
2021-05-20 21:39:04 +02:00
|
|
|
visualSelectMode bool // toggle visual selection mode
|
2020-10-26 21:44:01 +01:00
|
|
|
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
|
2021-05-20 21:39:04 +02:00
|
|
|
selectedEntries map[string]dirPos // selected entries of current directory
|
2017-04-01 20:53:44 +02: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 20:18:52 +01:00
|
|
|
x += runewidth.RuneWidth(c)
|
2017-04-01 20:53:44 +02: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 16:59:27 +02: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 20:53:44 +02: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 16:59:27 +02: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 20:53:44 +02: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 16:59:27 +02: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 20:53:44 +02: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 17:06:01 +02:00
|
|
|
attrs, _ := u.d.AttrI(u.sortPerm[i])
|
|
|
|
if attrs.Size > biggest {
|
|
|
|
biggest = attrs.Size
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-30 16:54:59 +01: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 17:06:01 +02:00
|
|
|
attrs, _ := u.d.AttrI(u.sortPerm[i])
|
|
|
|
if attrs.IsDir && attrs.Count == 0 {
|
2020-10-30 16:54:59 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-01 20:53:44 +02:00
|
|
|
// Draw the current screen
|
|
|
|
func (u *UI) Draw() error {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
u.dirListHeight = h - 3
|
|
|
|
|
|
|
|
// Plot
|
2022-06-19 12:22:45 +02:00
|
|
|
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
|
2017-04-01 20:53:44 +02:00
|
|
|
|
|
|
|
// 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 16:54:59 +01:00
|
|
|
showEmptyDir := u.hasEmptyDir()
|
2017-04-01 20:53:44 +02: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-04-11 17:06:01 +02:00
|
|
|
attrs, err := u.d.AttrI(u.sortPerm[n])
|
2021-05-20 21:39:04 +02:00
|
|
|
_, isSelected := u.selectedEntries[entry.String()]
|
2017-04-01 20:53:44 +02:00
|
|
|
fg := termbox.ColorWhite
|
2022-04-11 17:06:01 +02:00
|
|
|
if attrs.EntriesHaveErrors {
|
2020-12-28 15:08:12 +01:00
|
|
|
fg = termbox.ColorYellow
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fg = termbox.ColorRed
|
|
|
|
}
|
2022-06-19 12:22:45 +02:00
|
|
|
const colorLightYellow = termbox.ColorYellow + 8
|
2021-05-20 21:39:04 +02:00
|
|
|
if isSelected {
|
2022-06-19 12:22:45 +02:00
|
|
|
fg = colorLightYellow
|
2021-05-20 21:39:04 +02:00
|
|
|
}
|
2017-04-01 20:53:44 +02:00
|
|
|
bg := termbox.ColorBlack
|
|
|
|
if n == dirPos.entry {
|
|
|
|
fg, bg = bg, fg
|
|
|
|
}
|
|
|
|
mark := ' '
|
2022-04-11 17:06:01 +02:00
|
|
|
if attrs.IsDir {
|
2017-04-01 20:53:44 +02:00
|
|
|
mark = '/'
|
|
|
|
}
|
2020-12-28 16:20:23 +01:00
|
|
|
fileFlag := ' '
|
2017-04-01 20:53:44 +02:00
|
|
|
message := ""
|
2022-04-11 17:06:01 +02:00
|
|
|
if !attrs.Readable {
|
2017-04-01 20:53:44 +02:00
|
|
|
message = " [not read yet]"
|
|
|
|
}
|
2022-04-11 17:06:01 +02: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 15:46:56 +02:00
|
|
|
fileFlag = '~'
|
|
|
|
}
|
2022-04-11 17:06:01 +02:00
|
|
|
if attrs.EntriesHaveErrors {
|
2020-12-28 15:08:12 +01:00
|
|
|
message = " [some subdirectories could not be read, size may be underestimated]"
|
2020-12-28 16:20:23 +01:00
|
|
|
fileFlag = '.'
|
2020-12-28 15:08:12 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
message = fmt.Sprintf(" [%s]", err)
|
2020-12-28 16:20:23 +01:00
|
|
|
fileFlag = '!'
|
2020-12-28 15:08:12 +01:00
|
|
|
}
|
2017-04-01 20:53:44 +02:00
|
|
|
extras := ""
|
|
|
|
if u.showCounts {
|
2022-04-11 17:06:01 +02:00
|
|
|
ss := operations.CountStringField(attrs.Count, u.humanReadable, 9) + " "
|
|
|
|
if attrs.Count > 0 {
|
2021-04-02 20:17:32 +02:00
|
|
|
extras += ss
|
2017-04-01 20:53:44 +02:00
|
|
|
} else {
|
2021-04-02 20:17:32 +02:00
|
|
|
extras += strings.Repeat(" ", len(ss))
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
2020-10-26 21:44:01 +01:00
|
|
|
}
|
|
|
|
if u.showDirAverageSize {
|
2022-04-11 17:06:01 +02:00
|
|
|
avg := attrs.AverageSize()
|
|
|
|
ss := operations.SizeStringField(int64(avg), u.humanReadable, 9) + " "
|
|
|
|
if avg > 0 {
|
2021-04-02 20:17:32 +02:00
|
|
|
extras += ss
|
2020-10-26 21:44:01 +01:00
|
|
|
} else {
|
2021-04-02 20:17:32 +02:00
|
|
|
extras += strings.Repeat(" ", len(ss))
|
2020-10-26 21:44:01 +01:00
|
|
|
}
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
2020-10-30 16:54:59 +01:00
|
|
|
if showEmptyDir {
|
2022-04-11 17:06:01 +02:00
|
|
|
if attrs.IsDir && attrs.Count == 0 && fileFlag == ' ' {
|
2020-12-28 16:20:23 +01:00
|
|
|
fileFlag = 'e'
|
2020-10-30 16:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-01 20:53:44 +02:00
|
|
|
if u.showGraph {
|
2022-04-11 17:06:01 +02:00
|
|
|
bars := (attrs.Size + perBar/2 - 1) / perBar
|
2017-04-01 20:53:44 +02: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-04-11 17:06:01 +02:00
|
|
|
Linef(0, y, w, fg, bg, ' ', "%c %s %s%c%s%s", fileFlag, operations.SizeStringField(attrs.Size, u.humanReadable, 12), extras, mark, path.Base(entry.Remote()), message)
|
2017-04-01 20:53:44 +02: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()
|
2021-04-02 20:17:32 +02:00
|
|
|
Linef(0, h-1, w, termbox.ColorBlack, termbox.ColorWhite, ' ', "Total usage: %s, Objects: %s%s", operations.SizeString(size, u.humanReadable), operations.CountString(count, u.humanReadable), message)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:06:24 +02:00
|
|
|
// Show the box on top if required
|
2017-04-01 20:53:44 +02:00
|
|
|
if u.showBox {
|
|
|
|
u.Box()
|
|
|
|
}
|
2022-06-19 12:22:45 +02:00
|
|
|
err := termbox.Flush()
|
2017-04-01 20:53:44 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("failed to flush screen: %w", err)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
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 14:52:58 +01:00
|
|
|
if entries == 0 || dirPos.offset < 0 {
|
2017-04-01 20:53:44 +02:00
|
|
|
dirPos.offset = 0
|
|
|
|
} else if dirPos.offset >= entries {
|
|
|
|
dirPos.offset = entries - 1
|
|
|
|
}
|
|
|
|
|
2021-05-20 21:39:04 +02:00
|
|
|
// toggle the current file for selection in selection mode
|
|
|
|
if u.visualSelectMode {
|
|
|
|
u.toggleSelectForCursor()
|
|
|
|
}
|
|
|
|
|
2017-04-01 20:53:44 +02:00
|
|
|
// write dirPos back for later
|
|
|
|
u.dirPosMap[u.path] = dirPos
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:59:27 +02:00
|
|
|
func (u *UI) removeEntry(pos int) {
|
|
|
|
u.d.Remove(pos)
|
|
|
|
u.setCurrentDir(u.d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UI) delete() {
|
2021-04-14 18:20:17 +02:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-05-20 21:39:04 +02: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 10:34:30 +02:00
|
|
|
ctx := context.Background()
|
2021-04-14 18:20:17 +02:00
|
|
|
cursorPos := u.dirPosMap[u.path]
|
|
|
|
dirPos := u.sortPerm[cursorPos.entry]
|
|
|
|
dirEntry := u.entries[dirPos]
|
2018-10-14 16:59:27 +02:00
|
|
|
u.boxMenu = []string{"cancel", "confirm"}
|
2021-04-14 18:20:17 +02:00
|
|
|
if obj, isFile := dirEntry.(fs.Object); isFile {
|
2018-10-14 16:59:27 +02:00
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
err := operations.DeleteFile(ctx, obj)
|
2018-10-14 16:59:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-14 18:20:17 +02:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 16:59:27 +02:00
|
|
|
return "Successfully deleted file!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Delete this file?",
|
2021-11-09 16:09:12 +01:00
|
|
|
fspath.JoinRootPath(u.fsName, dirEntry.String())})
|
2018-10-14 16:59:27 +02:00
|
|
|
} else {
|
|
|
|
u.boxMenuHandler = func(f fs.Fs, p string, o int) (string, error) {
|
|
|
|
if o != 1 {
|
|
|
|
return "Aborted!", nil
|
|
|
|
}
|
2021-04-14 18:20:17 +02:00
|
|
|
err := operations.Purge(ctx, f, dirEntry.String())
|
2018-10-14 16:59:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u.removeEntry(dirPos)
|
2021-04-14 18:20:17 +02:00
|
|
|
if cursorPos.entry >= len(u.entries) {
|
|
|
|
u.move(-1) // move back onto a valid entry
|
|
|
|
}
|
2018-10-14 16:59:27 +02:00
|
|
|
return "Successfully purged folder!", nil
|
|
|
|
}
|
|
|
|
u.popupBox([]string{
|
|
|
|
"Purge this directory?",
|
|
|
|
"ALL files in it will be deleted",
|
2021-11-09 16:09:12 +01:00
|
|
|
fspath.JoinRootPath(u.fsName, dirEntry.String())})
|
2018-10-14 16:59:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 21:39:04 +02: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 07:40:54 +02: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 20:53:44 +02: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 14:28:38 +01:00
|
|
|
var iAvgSize, jAvgSize float64
|
2022-04-11 17:06:01 +02:00
|
|
|
iattrs, _ := ds.d.AttrI(ds.sortPerm[i])
|
|
|
|
jattrs, _ := ds.d.AttrI(ds.sortPerm[j])
|
2017-04-01 20:53:44 +02:00
|
|
|
iname, jname := ds.entries[ds.sortPerm[i]].Remote(), ds.entries[ds.sortPerm[j]].Remote()
|
2022-04-11 17:06:01 +02:00
|
|
|
if iattrs.Count > 0 {
|
|
|
|
iAvgSize = iattrs.AverageSize()
|
2020-10-27 14:28:38 +01:00
|
|
|
}
|
2022-04-11 17:06:01 +02:00
|
|
|
if jattrs.Count > 0 {
|
|
|
|
jAvgSize = jattrs.AverageSize()
|
2020-10-27 14:28:38 +01:00
|
|
|
}
|
|
|
|
|
2017-04-01 20:53:44 +02:00
|
|
|
switch {
|
|
|
|
case ds.u.sortByName < 0:
|
|
|
|
return iname > jname
|
|
|
|
case ds.u.sortByName > 0:
|
|
|
|
break
|
|
|
|
case ds.u.sortBySize < 0:
|
2022-04-11 17:06:01 +02:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size < jattrs.Size
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
case ds.u.sortBySize > 0:
|
2022-04-11 17:06:01 +02:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size > jattrs.Size
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
case ds.u.sortByCount < 0:
|
2022-04-11 17:06:01 +02:00
|
|
|
if iattrs.Count != jattrs.Count {
|
|
|
|
return iattrs.Count < jattrs.Count
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
case ds.u.sortByCount > 0:
|
2022-04-11 17:06:01 +02:00
|
|
|
if iattrs.Count != jattrs.Count {
|
|
|
|
return iattrs.Count > jattrs.Count
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
2020-10-27 14:28:38 +01:00
|
|
|
case ds.u.sortByAverageSize < 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize < jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
2022-11-04 12:08:41 +01:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size < jattrs.Size
|
|
|
|
}
|
2020-10-27 14:28:38 +01:00
|
|
|
case ds.u.sortByAverageSize > 0:
|
|
|
|
if iAvgSize != jAvgSize {
|
|
|
|
return iAvgSize > jAvgSize
|
|
|
|
}
|
|
|
|
// if avgSize is equal, sort by size
|
2022-11-04 12:08:41 +01:00
|
|
|
if iattrs.Size != jattrs.Size {
|
|
|
|
return iattrs.Size > jattrs.Size
|
|
|
|
}
|
2017-04-01 20:53:44 +02: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 16:09:12 +01:00
|
|
|
u.path = fspath.JoinRootPath(u.fsName, d.Path())
|
2021-05-20 21:39:04 +02:00
|
|
|
u.selectedEntries = make(map[string]dirPos)
|
|
|
|
u.visualSelectMode = false
|
2017-04-01 20:53:44 +02:00
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// enters the current entry
|
|
|
|
func (u *UI) enter() {
|
2017-12-12 14:52:58 +01:00
|
|
|
if u.d == nil || len(u.entries) == 0 {
|
2017-04-01 20:53:44 +02: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 16:59:27 +02: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 20:53:44 +02: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 07:40:54 +02:00
|
|
|
if u.showBox && reflect.DeepEqual(u.boxText, text) {
|
2017-04-01 20:53:44 +02: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 14:28:38 +01:00
|
|
|
u.sortByAverageSize = 0
|
2017-04-01 20:53:44 +02:00
|
|
|
if old == 0 {
|
|
|
|
*sortType = 1
|
|
|
|
} else {
|
|
|
|
*sortType = -old
|
|
|
|
}
|
|
|
|
u.sortCurrentDir()
|
|
|
|
}
|
|
|
|
|
2021-05-20 21:39:04 +02: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-01 20:53:44 +02:00
|
|
|
// NewUI creates a new user interface for ncdu on f
|
|
|
|
func NewUI(f fs.Fs) *UI {
|
|
|
|
return &UI{
|
2020-10-26 21:44:01 +01:00
|
|
|
f: f,
|
|
|
|
path: "Waiting for root...",
|
|
|
|
dirListHeight: 20, // updated in Draw
|
2021-11-09 16:09:12 +01:00
|
|
|
fsName: fs.ConfigString(f),
|
2020-10-26 21:44:01 +01:00
|
|
|
showGraph: true,
|
|
|
|
showCounts: false,
|
|
|
|
showDirAverageSize: false,
|
2021-04-02 20:17:32 +02:00
|
|
|
humanReadable: true,
|
2020-10-26 21:44:01 +01:00
|
|
|
sortByName: 0, // +1 for normal, 0 for off, -1 for reverse
|
|
|
|
sortBySize: 1,
|
|
|
|
sortByCount: 0,
|
|
|
|
dirPosMap: make(map[string]dirPos),
|
2021-05-20 21:39:04 +02:00
|
|
|
selectedEntries: make(map[string]dirPos),
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show shows the user interface
|
|
|
|
func (u *UI) Show() error {
|
|
|
|
err := termbox.Init()
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("termbox init: %w", err)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
defer termbox.Close()
|
|
|
|
|
|
|
|
// scan the disk in the background
|
|
|
|
u.listing = true
|
2019-06-17 10:34:30 +02:00
|
|
|
rootChan, errChan, updated := scan.Scan(context.Background(), u.f)
|
2017-04-01 20:53:44 +02: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 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("draw failed: %w", err)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
var root *scan.Dir
|
|
|
|
select {
|
|
|
|
case root = <-rootChan:
|
|
|
|
u.root = root
|
|
|
|
u.setCurrentDir(root)
|
|
|
|
case err := <-errChan:
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("ncdu directory listing: %w", err)
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
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 16:59:27 +02:00
|
|
|
if u.showBox {
|
|
|
|
u.moveBox(-1)
|
|
|
|
break
|
|
|
|
}
|
2017-04-01 20:53:44 +02:00
|
|
|
u.up()
|
2018-10-14 16:59:27 +02: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 20:53:44 +02:00
|
|
|
u.enter()
|
|
|
|
case 'c':
|
|
|
|
u.showCounts = !u.showCounts
|
|
|
|
case 'g':
|
|
|
|
u.showGraph = !u.showGraph
|
2020-10-26 21:44:01 +01:00
|
|
|
case 'a':
|
|
|
|
u.showDirAverageSize = !u.showDirAverageSize
|
2017-04-01 20:53:44 +02:00
|
|
|
case 'n':
|
|
|
|
u.toggleSort(&u.sortByName)
|
|
|
|
case 's':
|
|
|
|
u.toggleSort(&u.sortBySize)
|
2021-05-20 21:39:04 +02:00
|
|
|
case 'v':
|
|
|
|
u.toggleSelectForCursor()
|
|
|
|
case 'V':
|
|
|
|
u.visualSelectMode = !u.visualSelectMode
|
2017-04-01 20:53:44 +02:00
|
|
|
case 'C':
|
|
|
|
u.toggleSort(&u.sortByCount)
|
2020-10-27 14:28:38 +01:00
|
|
|
case 'A':
|
|
|
|
u.toggleSort(&u.sortByAverageSize)
|
2019-06-23 07:40:54 +02:00
|
|
|
case 'y':
|
|
|
|
u.copyPath()
|
|
|
|
case 'Y':
|
|
|
|
u.displayPath()
|
2018-10-14 16:59:27 +02:00
|
|
|
case 'd':
|
|
|
|
u.delete()
|
2021-04-02 20:17:32 +02:00
|
|
|
case 'u':
|
|
|
|
u.humanReadable = !u.humanReadable
|
2021-05-20 21:39:04 +02:00
|
|
|
case 'D':
|
|
|
|
u.deleteSelected()
|
2017-04-01 20:53:44 +02:00
|
|
|
case '?':
|
2019-06-23 07:40:54 +02:00
|
|
|
u.togglePopupBox(helpText())
|
2018-03-24 18:55:20 +01: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 20:53:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 00:07:12 +02:00
|
|
|
// listen to key presses, etc.
|
2017-04-01 20:53:44 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|