Make duplicated file format configurable (#1315)

* Make duplicated file format configurable

* Add %b and %f options to dupfilefmt
This commit is contained in:
David Pedersen authored and GitHub committed 2023-07-04 12:29:24 +03:00
1 parent 835c5b35e2
commit f20d0e5fb4
8 files changed
+47 -4

No files matched your search

+1
View File
@@ -140,6 +140,7 @@ var (
"drawbox", "drawbox",
"nodrawbox", "nodrawbox",
"drawbox!", "drawbox!",
"dupfilefmt",
"globsearch", "globsearch",
"noglobsearch", "noglobsearch",
"globsearch!", "globsearch!",
+11 -2
View File
@@ -5,6 +5,8 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings"
"github.com/djherbis/times" "github.com/djherbis/times"
) )
@@ -102,13 +104,20 @@ func copyAll(srcs []string, dstDir string) (nums chan int64, errs chan error) {
go func() { go func() {
for _, src := range srcs { for _, src := range srcs {
dst := filepath.Join(dstDir, filepath.Base(src)) file := filepath.Base(src)
dst := filepath.Join(dstDir, file)
_, err := os.Lstat(dst) _, err := os.Lstat(dst)
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
ext := filepath.Ext(file)
basename := filepath.Base(file[:len(file)-len(ext)])
var newPath string var newPath string
for i := 1; !os.IsNotExist(err); i++ { 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) _, err = os.Lstat(newPath)
} }
dst = newPath dst = newPath
+6
View File
@@ -130,6 +130,7 @@ The following options can be used to customize the behavior of lf:
dironly bool (default false) dironly bool (default false)
dirpreviews bool (default false) dirpreviews bool (default false)
drawbox bool (default false) drawbox bool (default false)
dupfilefmt string (default '%f.~%n~')
errorfmt string (default "\033[7;31;47m") errorfmt string (default "\033[7;31;47m")
filesep string (default "\n") filesep string (default "\n")
findlen int (default 1) 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. 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") errorfmt string (default "\033[7;31;47m")
Format string of error messages shown in the bottom message line. Format string of error messages shown in the bottom message line.
+9
View File
@@ -133,6 +133,7 @@ The following options can be used to customize the behavior of lf:
dironly bool (default false) dironly bool (default false)
dirpreviews bool (default false) dirpreviews bool (default false)
drawbox bool (default false) drawbox bool (default false)
dupfilefmt string (default '%f.~%n~')
errorfmt string (default "\033[7;31;47m") errorfmt string (default "\033[7;31;47m")
filesep string (default "\n") filesep string (default "\n")
findlen int (default 1) findlen int (default 1)
@@ -761,6 +762,14 @@ custom previews for directories.
Draw boxes around panes with box drawing characters. 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") errorfmt string (default "\033[7;31;47m")
Format string of error messages shown in the bottom message line. Format string of error messages shown in the bottom message line.
+2
View File
@@ -230,6 +230,8 @@ func (e *setExpr) eval(app *app, args []string) {
app.nav.regCache = make(map[string]*reg) app.nav.regCache = make(map[string]*reg)
} }
app.ui.loadFile(app, true) app.ui.loadFile(app, true)
case "dupfilefmt":
gOpts.dupfilefmt = e.val
case "errorfmt": case "errorfmt":
gOpts.errorfmt = e.val gOpts.errorfmt = e.val
case "filesep": case "filesep":
+7
View File
@@ -149,6 +149,7 @@ The following options can be used to customize the behavior of lf:
dironly bool (default false) dironly bool (default false)
dirpreviews bool (default false) dirpreviews bool (default false)
drawbox bool (default false) drawbox bool (default false)
dupfilefmt string (default '%f.~%n~')
errorfmt string (default "\e033[7;31;47m") errorfmt string (default "\e033[7;31;47m")
filesep string (default "\en") filesep string (default "\en")
findlen int (default 1) findlen int (default 1)
@@ -876,6 +877,12 @@ If enabled, directories will also be passed to the previewer script. This allows
.PP .PP
Draw boxes around panes with box drawing characters. Draw boxes around panes with box drawing characters.
.PP .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 .EX
errorfmt string (default "\e033[7;31;47m") errorfmt string (default "\e033[7;31;47m")
.EE .EE
+9 -2
View File
@@ -1340,7 +1340,8 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
continue continue
} }
dst := filepath.Join(dstDir, filepath.Base(src)) file := filepath.Base(src)
dst := filepath.Join(dstDir, file)
dstStat, err := os.Stat(dst) dstStat, err := os.Stat(dst)
if os.SameFile(srcStat, dstStat) { if os.SameFile(srcStat, dstStat) {
@@ -1349,9 +1350,15 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
app.ui.exprChan <- echo app.ui.exprChan <- echo
continue continue
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
ext := filepath.Ext(file)
basename := filepath.Base(file[:len(file)-len(ext)])
var newPath string var newPath string
for i := 1; !os.IsNotExist(err); i++ { 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) _, err = os.Lstat(newPath)
} }
dst = newPath dst = newPath
+2
View File
@@ -39,6 +39,7 @@ var gOpts struct {
dironly bool dironly bool
dirpreviews bool dirpreviews bool
drawbox bool drawbox bool
dupfilefmt string
globsearch bool globsearch bool
icons bool icons bool
ignorecase bool ignorecase bool
@@ -98,6 +99,7 @@ func init() {
gOpts.dironly = false gOpts.dironly = false
gOpts.dirpreviews = false gOpts.dirpreviews = false
gOpts.drawbox = false gOpts.drawbox = false
gOpts.dupfilefmt = "%f.~%n~"
gOpts.borderfmt = "\033[0m" gOpts.borderfmt = "\033[0m"
gOpts.cursoractivefmt = "\033[7m" gOpts.cursoractivefmt = "\033[7m"
gOpts.cursorparentfmt = "\033[7m" gOpts.cursorparentfmt = "\033[7m"