From edc5d977cd4db746c9ed87f3c898f1f365c6bcd3 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Thu, 7 Aug 2025 19:27:17 +1000 Subject: [PATCH] Replace save buffer with clipboard struct (#2082) --- app.go | 20 ++++++++++---------- doc.md | 10 ++++++---- doc.txt | 16 +++++++++------- eval.go | 6 +++--- lf.1 | 14 ++++++++------ nav.go | 45 +++++++++++++++++++++++++-------------------- ui.go | 18 ++++++++---------- 7 files changed, 69 insertions(+), 60 deletions(-) diff --git a/app.go b/app.go index 191f26fa..974b07e8 100644 --- a/app.go +++ b/app.go @@ -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() diff --git a/doc.md b/doc.md index 6dab271e..e9ed45bd 100644 --- a/doc.md +++ b/doc.md @@ -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 diff --git a/doc.txt b/doc.txt index 8939a8c3..af342cf5 100644 --- a/doc.txt +++ b/doc.txt @@ -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 diff --git a/eval.go b/eval.go index 7165c253..54acb16d 100644 --- a/eval.go +++ b/eval.go @@ -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 } diff --git a/lf.1 b/lf.1 index 15dd1ee1..8d19a42b 100644 --- a/lf.1 +++ b/lf.1 @@ -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. diff --git a/nav.go b/nav.go index b605c6a4..08dff161 100644 --- a/nav.go +++ b/nav.go @@ -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 { diff --git a/ui.go b/ui.go index d677cb6a..9c9afaad 100644 --- a/ui.go +++ b/ui.go @@ -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()