Replace save buffer with clipboard struct (#2082)
This commit is contained in:
1 parent
5ad11bdc84
commit
edc5d977cd
7 files changed
+69
-60
No files matched your search
@@ -116,7 +116,7 @@ func (app *app) readFile(path string) {
|
||||
}
|
||||
}
|
||||
|
||||
func loadFiles() (list []string, cp bool, err error) {
|
||||
func loadFiles() (clipboard clipboard, err error) {
|
||||
files, err := os.Open(gFilesPath)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
@@ -134,16 +134,16 @@ func loadFiles() (list []string, cp bool, err error) {
|
||||
|
||||
switch s.Text() {
|
||||
case "copy":
|
||||
cp = true
|
||||
clipboard.mode = clipboardCopy
|
||||
case "move":
|
||||
cp = false
|
||||
clipboard.mode = clipboardCut
|
||||
default:
|
||||
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
|
||||
return
|
||||
}
|
||||
|
||||
for s.Scan() && s.Text() != "" {
|
||||
list = append(list, s.Text())
|
||||
clipboard.paths = append(clipboard.paths, s.Text())
|
||||
}
|
||||
|
||||
if s.Err() != nil {
|
||||
@@ -151,12 +151,12 @@ func loadFiles() (list []string, cp bool, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("loading files: %v", list)
|
||||
log.Printf("loading files: %v", clipboard.paths)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func saveFiles(list []string, cp bool) error {
|
||||
func saveFiles(clipboard clipboard) error {
|
||||
if err := os.MkdirAll(filepath.Dir(gFilesPath), os.ModePerm); err != nil {
|
||||
return fmt.Errorf("creating data directory: %s", err)
|
||||
}
|
||||
@@ -167,16 +167,16 @@ func saveFiles(list []string, cp bool) error {
|
||||
}
|
||||
defer files.Close()
|
||||
|
||||
log.Printf("saving files: %v", list)
|
||||
log.Printf("saving files: %v", clipboard.paths)
|
||||
|
||||
if cp {
|
||||
if clipboard.mode == clipboardCopy {
|
||||
fmt.Fprintln(files, "copy")
|
||||
} else {
|
||||
fmt.Fprintln(files, "move")
|
||||
}
|
||||
|
||||
for _, f := range list {
|
||||
fmt.Fprintln(files, f)
|
||||
for _, path := range clipboard.paths {
|
||||
fmt.Fprintln(files, path)
|
||||
}
|
||||
|
||||
files.Sync()
|
||||
|
||||
@@ -432,20 +432,22 @@ Select/unselect files that match the given glob.
|
||||
|
||||
## copy (default `y`)
|
||||
|
||||
If there are no selections, save the path of the current file to the copy buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be copied.
|
||||
If there are no selected files, the path of the current file is used instead.
|
||||
|
||||
## cut (default `d`)
|
||||
|
||||
If there are no selections, save the path of the current file to the cut buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be moved.
|
||||
If there are no selected files, the path of the current file is used instead.
|
||||
|
||||
## paste (default `p`)
|
||||
|
||||
Copy/Move files in the copy/cut buffer to the current working directory.
|
||||
Copy/Move files in the clipboard to the current working directory.
|
||||
A custom `paste` command can be defined to override this default.
|
||||
|
||||
## clear (default `c`)
|
||||
|
||||
Clear file paths in copy/cut buffer.
|
||||
Clear file paths in the clipboard.
|
||||
|
||||
## sync
|
||||
|
||||
|
||||
@@ -434,22 +434,24 @@ Select/unselect files that match the given glob.
|
||||
|
||||
copy (default y)
|
||||
|
||||
If there are no selections, save the path of the current file to the
|
||||
copy buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be copied.
|
||||
If there are no selected files, the path of the current file is used
|
||||
instead.
|
||||
|
||||
cut (default d)
|
||||
|
||||
If there are no selections, save the path of the current file to the cut
|
||||
buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be moved.
|
||||
If there are no selected files, the path of the current file is used
|
||||
instead.
|
||||
|
||||
paste (default p)
|
||||
|
||||
Copy/Move files in the copy/cut buffer to the current working directory.
|
||||
A custom paste command can be defined to override this default.
|
||||
Copy/Move files in the clipboard to the current working directory. A
|
||||
custom paste command can be defined to override this default.
|
||||
|
||||
clear (default c)
|
||||
|
||||
Clear file paths in copy/cut buffer.
|
||||
Clear file paths in the clipboard.
|
||||
|
||||
sync
|
||||
|
||||
|
||||
@@ -1271,7 +1271,7 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := app.nav.save(true); err != nil {
|
||||
if err := app.nav.save(clipboardCopy); err != nil {
|
||||
app.ui.echoerrf("copy: %s", err)
|
||||
return
|
||||
}
|
||||
@@ -1293,7 +1293,7 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := app.nav.save(false); err != nil {
|
||||
if err := app.nav.save(clipboardCut); err != nil {
|
||||
app.ui.echoerrf("cut: %s", err)
|
||||
return
|
||||
}
|
||||
@@ -1327,7 +1327,7 @@ func (e *callExpr) eval(app *app, args []string) {
|
||||
if !app.nav.init {
|
||||
return
|
||||
}
|
||||
if err := saveFiles(nil, false); err != nil {
|
||||
if err := saveFiles(clipboard{nil, clipboardCut}); err != nil {
|
||||
app.ui.echoerrf("clear: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -451,17 +451,19 @@ Remove the selection of all files in all directories.
|
||||
.SS glob\-select, glob\-unselect
|
||||
Select/unselect files that match the given glob.
|
||||
.SS copy (default \f[CR]y\f[R])
|
||||
If there are no selections, save the path of the current file to the
|
||||
copy buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be copied.
|
||||
If there are no selected files, the path of the current file is used
|
||||
instead.
|
||||
.SS cut (default \f[CR]d\f[R])
|
||||
If there are no selections, save the path of the current file to the cut
|
||||
buffer, otherwise, copy the paths of selected files.
|
||||
Save the paths of selected files to the clipboard as files to be moved.
|
||||
If there are no selected files, the path of the current file is used
|
||||
instead.
|
||||
.SS paste (default \f[CR]p\f[R])
|
||||
Copy/Move files in the copy/cut buffer to the current working directory.
|
||||
Copy/Move files in the clipboard to the current working directory.
|
||||
A custom \f[CR]paste\f[R] command can be defined to override this
|
||||
default.
|
||||
.SS clear (default \f[CR]c\f[R])
|
||||
Clear file paths in copy/cut buffer.
|
||||
Clear file paths in the clipboard.
|
||||
.SS sync
|
||||
Synchronize copied/cut files with the server.
|
||||
This command is automatically called when required.
|
||||
|
||||
@@ -513,6 +513,18 @@ func (dir *dir) boundPos(height int) {
|
||||
dir.pos = max(dir.pos, height-(len(dir.files)-dir.ind))
|
||||
}
|
||||
|
||||
type clipboardMode byte
|
||||
|
||||
const (
|
||||
clipboardCopy = iota
|
||||
clipboardCut
|
||||
)
|
||||
|
||||
type clipboard struct {
|
||||
paths []string
|
||||
mode clipboardMode
|
||||
}
|
||||
|
||||
type nav struct {
|
||||
init bool
|
||||
dirs []*dir
|
||||
@@ -538,7 +550,7 @@ type nav struct {
|
||||
delChan chan string
|
||||
dirCache map[string]*dir
|
||||
regCache map[string]*reg
|
||||
saves map[string]bool
|
||||
clipboard clipboard
|
||||
marks map[string]string
|
||||
renameOldPath string
|
||||
renameNewPath string
|
||||
@@ -683,7 +695,6 @@ func newNav(height int) *nav {
|
||||
delChan: make(chan string),
|
||||
dirCache: make(map[string]*dir),
|
||||
regCache: make(map[string]*reg),
|
||||
saves: make(map[string]bool),
|
||||
marks: make(map[string]string),
|
||||
selections: make(map[string]int),
|
||||
tags: make(map[string]string),
|
||||
@@ -1314,21 +1325,18 @@ func (nav *nav) unselect() {
|
||||
nav.selectionInd = 0
|
||||
}
|
||||
|
||||
func (nav *nav) save(cp bool) error {
|
||||
func (nav *nav) save(mode clipboardMode) error {
|
||||
list, err := nav.currFileOrSelections()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := saveFiles(list, cp); err != nil {
|
||||
clipboard := clipboard{list, mode}
|
||||
if err := saveFiles(clipboard); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clear(nav.saves)
|
||||
for _, f := range list {
|
||||
nav.saves[f] = cp
|
||||
}
|
||||
|
||||
nav.clipboard = clipboard
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1501,21 +1509,21 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
|
||||
}
|
||||
|
||||
func (nav *nav) paste(app *app) error {
|
||||
srcs, cp, err := loadFiles()
|
||||
clipboard, err := loadFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(srcs) == 0 {
|
||||
return errors.New("no file in copy/cut buffer")
|
||||
if len(clipboard.paths) == 0 {
|
||||
return errors.New("no files in clipboard")
|
||||
}
|
||||
|
||||
dstDir := nav.currDir().path
|
||||
|
||||
if cp {
|
||||
go nav.copyAsync(app, srcs, dstDir)
|
||||
if clipboard.mode == clipboardCopy {
|
||||
go nav.copyAsync(app, clipboard.paths, dstDir)
|
||||
} else {
|
||||
go nav.moveAsync(app, srcs, dstDir)
|
||||
go nav.moveAsync(app, clipboard.paths, dstDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -1597,15 +1605,12 @@ func (nav *nav) rename() error {
|
||||
}
|
||||
|
||||
func (nav *nav) sync() error {
|
||||
list, cp, err := loadFiles()
|
||||
clipboard, err := loadFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
clear(nav.saves)
|
||||
for _, f := range list {
|
||||
nav.saves[f] = cp
|
||||
}
|
||||
nav.clipboard = clipboard
|
||||
|
||||
tempmarks := make(map[string]string)
|
||||
for _, ch := range gOpts.tempmarks {
|
||||
|
||||
@@ -349,7 +349,7 @@ func fileInfo(f *file, d *dir, userWidth int, groupWidth int, customWidth int) (
|
||||
|
||||
type dirContext struct {
|
||||
selections map[string]int
|
||||
saves map[string]bool
|
||||
clipboard clipboard
|
||||
tags map[string]string
|
||||
}
|
||||
|
||||
@@ -459,8 +459,8 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
|
||||
win.print(ui.screen, lnwidth, i, parseEscapeSequence(gOpts.visualfmt), " ")
|
||||
} else if _, ok := context.selections[path]; ok {
|
||||
win.print(ui.screen, lnwidth, i, parseEscapeSequence(gOpts.selectfmt), " ")
|
||||
} else if cp, ok := context.saves[path]; ok {
|
||||
if cp {
|
||||
} else if slices.Contains(context.clipboard.paths, path) {
|
||||
if context.clipboard.mode == clipboardCopy {
|
||||
win.print(ui.screen, lnwidth, i, parseEscapeSequence(gOpts.copyfmt), " ")
|
||||
} else {
|
||||
win.print(ui.screen, lnwidth, i, parseEscapeSequence(gOpts.cutfmt), " ")
|
||||
@@ -952,12 +952,10 @@ func (ui *ui) drawRuler(nav *nav) {
|
||||
|
||||
copy := 0
|
||||
move := 0
|
||||
for _, cp := range nav.saves {
|
||||
if cp {
|
||||
copy++
|
||||
} else {
|
||||
move++
|
||||
}
|
||||
if nav.clipboard.mode == clipboardCopy {
|
||||
copy = len(nav.clipboard.paths)
|
||||
} else {
|
||||
move = len(nav.clipboard.paths)
|
||||
}
|
||||
|
||||
currSelections := nav.currSelections()
|
||||
@@ -1079,7 +1077,7 @@ func (ui *ui) dirOfWin(nav *nav, wind int) *dir {
|
||||
|
||||
func (ui *ui) draw(nav *nav) {
|
||||
st := tcell.StyleDefault
|
||||
context := dirContext{selections: nav.selections, saves: nav.saves, tags: nav.tags}
|
||||
context := dirContext{selections: nav.selections, clipboard: nav.clipboard, tags: nav.tags}
|
||||
|
||||
ui.screen.Clear()
|
||||
|
||||
|
||||
Reference in new issue
Block a user