Replace save buffer with clipboard struct (#2082)

This commit is contained in:
Joe Lim authored and GitHub committed 2025-08-07 19:27:17 +10:00
1 parent 5ad11bdc84
commit edc5d977cd
7 files changed
+69 -60

No files matched your search

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