From f20d0e5fb46494f7d3a756581bb900bccf9cbaf3 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Tue, 4 Jul 2023 11:29:24 +0200 Subject: [PATCH] Make duplicated file format configurable (#1315) * Make duplicated file format configurable * Add %b and %f options to dupfilefmt --- complete.go | 1 + copy.go | 13 +++++++++++-- doc.go | 6 ++++++ docstring.go | 9 +++++++++ eval.go | 2 ++ lf.1 | 7 +++++++ nav.go | 11 +++++++++-- opts.go | 2 ++ 8 files changed, 47 insertions(+), 4 deletions(-) diff --git a/complete.go b/complete.go index f8910719..4bd7a213 100644 --- a/complete.go +++ b/complete.go @@ -140,6 +140,7 @@ var ( "drawbox", "nodrawbox", "drawbox!", + "dupfilefmt", "globsearch", "noglobsearch", "globsearch!", diff --git a/copy.go b/copy.go index 65bc14b0..2c7a2bd7 100644 --- a/copy.go +++ b/copy.go @@ -5,6 +5,8 @@ import ( "io" "os" "path/filepath" + "strconv" + "strings" "github.com/djherbis/times" ) @@ -102,13 +104,20 @@ func copyAll(srcs []string, dstDir string) (nums chan int64, errs chan error) { go func() { for _, src := range srcs { - dst := filepath.Join(dstDir, filepath.Base(src)) + file := filepath.Base(src) + dst := filepath.Join(dstDir, file) _, err := os.Lstat(dst) if !os.IsNotExist(err) { + ext := filepath.Ext(file) + basename := filepath.Base(file[:len(file)-len(ext)]) var newPath string for i := 1; !os.IsNotExist(err); i++ { - newPath = fmt.Sprintf("%s.~%d~", dst, i) + file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext) + file = strings.ReplaceAll(file, "%b", basename) + file = strings.ReplaceAll(file, "%e", ext) + file = strings.ReplaceAll(file, "%n", strconv.Itoa(i)) + newPath = filepath.Join(dstDir, file) _, err = os.Lstat(newPath) } dst = newPath diff --git a/doc.go b/doc.go index 8b84bb75..e486818b 100644 --- a/doc.go +++ b/doc.go @@ -130,6 +130,7 @@ The following options can be used to customize the behavior of lf: dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) + dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") findlen int (default 1) @@ -719,6 +720,11 @@ If enabled, directories will also be passed to the previewer script. This allows Draw boxes around panes with box drawing characters. + dupfilefmt string (default '%f.~%n~') + +Format string of file name when creating duplicate files. With the default format, copying a file `abc.txt` to the same directory will result in a duplicate file called `abc.txt.~1~`. +Special expansions are provided, '%f' as the file name, '%b' for basename (file name without extension), '%e' as the extension (including the dot) and '%n' as the number of duplicates. + errorfmt string (default "\033[7;31;47m") Format string of error messages shown in the bottom message line. diff --git a/docstring.go b/docstring.go index 6fb2393f..01031a09 100644 --- a/docstring.go +++ b/docstring.go @@ -133,6 +133,7 @@ The following options can be used to customize the behavior of lf: dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) + dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") findlen int (default 1) @@ -761,6 +762,14 @@ custom previews for directories. Draw boxes around panes with box drawing characters. + dupfilefmt string (default '%f.~%n~') + +Format string of file name when creating duplicate files. With the default +format, copying a file 'abc.txt' to the same directory will result in a +duplicate file called 'abc.txt.~1~'. Special expansions are provided, '%f' as +the file name, '%b' for basename (file name without extension), '%e' as the +extension (including the dot) and '%n' as the number of duplicates. + errorfmt string (default "\033[7;31;47m") Format string of error messages shown in the bottom message line. diff --git a/eval.go b/eval.go index 7d46c188..df90730f 100644 --- a/eval.go +++ b/eval.go @@ -230,6 +230,8 @@ func (e *setExpr) eval(app *app, args []string) { app.nav.regCache = make(map[string]*reg) } app.ui.loadFile(app, true) + case "dupfilefmt": + gOpts.dupfilefmt = e.val case "errorfmt": gOpts.errorfmt = e.val case "filesep": diff --git a/lf.1 b/lf.1 index d821aac3..78c1e54e 100644 --- a/lf.1 +++ b/lf.1 @@ -149,6 +149,7 @@ The following options can be used to customize the behavior of lf: dironly bool (default false) dirpreviews bool (default false) drawbox bool (default false) + dupfilefmt string (default '%f.~%n~') errorfmt string (default "\e033[7;31;47m") filesep string (default "\en") findlen int (default 1) @@ -876,6 +877,12 @@ If enabled, directories will also be passed to the previewer script. This allows .PP Draw boxes around panes with box drawing characters. .PP +.EX + dupfilefmt string (default '%f.~%n~') +.EE +.PP +Format string of file name when creating duplicate files. With the default format, copying a file `abc.txt` to the same directory will result in a duplicate file called `abc.txt.~1~`. Special expansions are provided, '%f' as the file name, '%b' for basename (file name without extension), '%e' as the extension (including the dot) and '%n' as the number of duplicates. +.PP .EX errorfmt string (default "\e033[7;31;47m") .EE diff --git a/nav.go b/nav.go index fb621d25..4d3fc3fc 100644 --- a/nav.go +++ b/nav.go @@ -1340,7 +1340,8 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) { continue } - dst := filepath.Join(dstDir, filepath.Base(src)) + file := filepath.Base(src) + dst := filepath.Join(dstDir, file) dstStat, err := os.Stat(dst) if os.SameFile(srcStat, dstStat) { @@ -1349,9 +1350,15 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) { app.ui.exprChan <- echo continue } else if !os.IsNotExist(err) { + ext := filepath.Ext(file) + basename := filepath.Base(file[:len(file)-len(ext)]) var newPath string for i := 1; !os.IsNotExist(err); i++ { - newPath = fmt.Sprintf("%s.~%d~", dst, i) + file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext) + file = strings.ReplaceAll(file, "%b", basename) + file = strings.ReplaceAll(file, "%e", ext) + file = strings.ReplaceAll(file, "%n", strconv.Itoa(i)) + newPath = filepath.Join(dstDir, file) _, err = os.Lstat(newPath) } dst = newPath diff --git a/opts.go b/opts.go index 5b8feaaf..2ea7c38e 100644 --- a/opts.go +++ b/opts.go @@ -39,6 +39,7 @@ var gOpts struct { dironly bool dirpreviews bool drawbox bool + dupfilefmt string globsearch bool icons bool ignorecase bool @@ -98,6 +99,7 @@ func init() { gOpts.dironly = false gOpts.dirpreviews = false gOpts.drawbox = false + gOpts.dupfilefmt = "%f.~%n~" gOpts.borderfmt = "\033[0m" gOpts.cursoractivefmt = "\033[7m" gOpts.cursorparentfmt = "\033[7m"