ncdu: Display/Copy to Clipboard Current Path

This commit is contained in:
Gary Kim 2019-06-23 13:40:54 +08:00 committed by Nick Craig-Wood
parent 2655bea86f
commit 0cb7130dd2
1 changed files with 44 additions and 16 deletions

View File

@ -8,9 +8,11 @@ import (
"context" "context"
"fmt" "fmt"
"path" "path"
"reflect"
"sort" "sort"
"strings" "strings"
"github.com/atotto/clipboard"
runewidth "github.com/mattn/go-runewidth" runewidth "github.com/mattn/go-runewidth"
"github.com/ncw/rclone/cmd" "github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/cmd/ncdu/scan" "github.com/ncw/rclone/cmd/ncdu/scan"
@ -42,7 +44,7 @@ structure as it goes along.
Here are the keys - press '?' to toggle the help on and off Here are the keys - press '?' to toggle the help on and off
` + strings.Join(helpText[1:], "\n ") + ` ` + strings.Join(helpText()[1:], "\n ") + `
This an homage to the [ncdu tool](https://dev.yorhel.nl/ncdu) but for This an homage to the [ncdu tool](https://dev.yorhel.nl/ncdu) but for
rclone remotes. It is missing lots of features at the moment rclone remotes. It is missing lots of features at the moment
@ -60,19 +62,28 @@ UI won't respond in the meantime since the deletion is done synchronously.
}, },
} }
// help text // helpText returns help text for ncdu
var helpText = []string{ func helpText() (tr []string) {
"rclone ncdu", tr = []string{
" ↑,↓ or k,j to Move", "rclone ncdu",
" →,l to enter", " ↑,↓ or k,j to Move",
" ←,h to return", " →,l to enter",
" c toggle counts", " ←,h to return",
" g toggle graph", " c toggle counts",
" n,s,C sort by name,size,count", " g toggle graph",
" d delete file/directory", " n,s,C sort by name,size,count",
" ^L refresh screen", " d delete file/directory",
" ? to toggle help on and off", }
" q/ESC/c-C to quit", if !clipboard.Unsupported {
tr = append(tr, " y copy current path to clipbard")
}
tr = append(tr, []string{
" Y display current path",
" ^L refresh screen",
" ? to toggle help on and off",
" q/ESC/c-C to quit",
}...)
return
} }
// UI contains the state of the user interface // UI contains the state of the user interface
@ -462,6 +473,19 @@ func (u *UI) delete() {
} }
} }
func (u *UI) displayPath() {
u.togglePopupBox([]string{
"Current Path",
u.path,
})
}
func (u *UI) copyPath() {
if !clipboard.Unsupported {
_ = clipboard.WriteAll(u.path)
}
}
// Sort by the configured sort method // Sort by the configured sort method
type ncduSort struct { type ncduSort struct {
sortPerm []int sortPerm []int
@ -591,7 +615,7 @@ func (u *UI) popupBox(text []string) {
// togglePopupBox shows a box with the text in // togglePopupBox shows a box with the text in
func (u *UI) togglePopupBox(text []string) { func (u *UI) togglePopupBox(text []string) {
if u.showBox { if u.showBox && reflect.DeepEqual(u.boxText, text) {
u.showBox = false u.showBox = false
} else { } else {
u.popupBox(text) u.popupBox(text)
@ -718,10 +742,14 @@ outer:
u.toggleSort(&u.sortBySize) u.toggleSort(&u.sortBySize)
case 'C': case 'C':
u.toggleSort(&u.sortByCount) u.toggleSort(&u.sortByCount)
case 'y':
u.copyPath()
case 'Y':
u.displayPath()
case 'd': case 'd':
u.delete() u.delete()
case '?': case '?':
u.togglePopupBox(helpText) u.togglePopupBox(helpText())
// Refresh the screen. Not obvious what key to map // Refresh the screen. Not obvious what key to map
// this onto, but ^L is a common choice. // this onto, but ^L is a common choice.