Skip to content

Commit

Permalink
Move get file extension logic to util
Browse files Browse the repository at this point in the history
  • Loading branch information
Limero committed Mar 31, 2024
1 parent ac016fe commit f1240e2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
6 changes: 3 additions & 3 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func copySize(srcs []string) (int64, error) {
}

func copyFile(src, dst string, preserve []string, info os.FileInfo, nums chan int64) error {
var dst_mode os.FileMode = 0666
var dst_mode os.FileMode = 0o666
preserve_timestamps := false
for _, s := range preserve {
switch s {
Expand Down Expand Up @@ -107,9 +107,9 @@ func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64,
file := filepath.Base(src)
dst := filepath.Join(dstDir, file)

_, err := os.Lstat(dst)
lstat, err := os.Lstat(dst)
if !os.IsNotExist(err) {
ext := filepath.Ext(file)
ext := getFileExtension(lstat)
basename := file[:len(file)-len(ext)]
var newPath string
for i := 1; !os.IsNotExist(err); i++ {
Expand Down
4 changes: 2 additions & 2 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,8 +1611,8 @@ func (e *callExpr) eval(app *app, args []string) {
}
normal(app)
app.ui.cmdPrefix = "rename: "
extension := filepath.Ext(curr.Name())
if len(extension) == 0 || extension == curr.Name() || curr.IsDir() {
extension := getFileExtension(curr)
if extension == "" {
// no extension or .hidden or is directory
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
} else {
Expand Down
30 changes: 25 additions & 5 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"io/fs"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -291,12 +292,31 @@ func naturalLess(s1, s2 string) bool {
}
}

var reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
var reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`)
// This function returns the extension of a file with a leading dot
// it returns an empty string if extension could not be determined
// i.e. directories, filenames without extensions
func getFileExtension(file fs.FileInfo) string {
if file.IsDir() {
return ""
}
extension := filepath.Ext(file.Name())
if strings.Count(file.Name(), ".") == 1 && file.Name()[0] == '.' {
// hidden file without extension
return ""
}
return extension
}

var reWord = regexp.MustCompile(`(\pL|\pN)+`)
var reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
var reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
var (
reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`)
)

var (
reWord = regexp.MustCompile(`(\pL|\pN)+`)
reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
reWordEnd = regexp.MustCompile(`(\pL|\pN)([^\pL\pN]|$)`)
)

func min(a, b int) int {
if a < b {
Expand Down
46 changes: 46 additions & 0 deletions misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,49 @@ func TestNaturalLess(t *testing.T) {
}
}
}

func TestGetFileExtension(t *testing.T) {
tests := []struct {
name string
fileName string
expectedExtension string
}{
{"normal file", "file.txt", ".txt"},
{"file without extension", "file", ""},
{"hidden file", ".gitignore", ""},
{"hidden file with extension", ".file.txt", ".txt"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dir := t.TempDir()
file, err := os.Create(dir + "/" + test.fileName)
if err != nil {
t.Fatalf("failed to create file: %v", err)
}

fileInfo, err := file.Stat()
if err != nil {
t.Fatalf("failed to get file info: %v", err)
}

if got := getFileExtension(fileInfo); got != test.expectedExtension {
t.Errorf("at input %q expected %q but got %q", test.fileName, test.expectedExtension, got)
}
})
}

t.Run("directory", func(t *testing.T) {
dir := t.TempDir()

fileInfo, err := os.Stat(dir)
if err != nil {
t.Fatalf("failed to get file info: %v", err)
}

expectedExtension := ""
if got := getFileExtension(fileInfo); got != expectedExtension {
t.Errorf("at input %q expected %q but got %q", dir, expectedExtension, got)
}
})
}
17 changes: 4 additions & 13 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type fakeStat struct {

func (fs *fakeStat) Name() string { return fs.name }
func (fs *fakeStat) Size() int64 { return 0 }
func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0000) }
func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0o000) }
func (fs *fakeStat) ModTime() time.Time { return time.Unix(0, 0) }
func (fs *fakeStat) IsDir() bool { return false }
func (fs *fakeStat) Sys() any { return nil }
Expand Down Expand Up @@ -88,7 +88,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1,
accessTime: time.Unix(0, 0),
changeTime: time.Unix(0, 0),
ext: filepath.Ext(fpath),
ext: getFileExtension(lstat),
err: err,
})
continue
Expand Down Expand Up @@ -122,10 +122,6 @@ func readdir(path string) ([]*file, error) {
ct = lstat.ModTime()
}

// returns an empty string if extension could not be determined
// i.e. directories, filenames without extensions
ext := filepath.Ext(fpath)

dirCount := -1
if lstat.IsDir() && gOpts.dircounts {
d, err := os.Open(fpath)
Expand All @@ -152,7 +148,7 @@ func readdir(path string) ([]*file, error) {
dirSize: -1,
accessTime: at,
changeTime: ct,
ext: ext,
ext: getFileExtension(lstat),
err: nil,
})
}
Expand Down Expand Up @@ -492,7 +488,6 @@ func (nav *nav) loadDirInternal(path string) *dir {
nav.dirPreviewChan <- d
}
nav.dirChan <- d

}()
return d
}
Expand Down Expand Up @@ -773,7 +768,6 @@ func matchPattern(pattern, name, path string) bool {
}

func (nav *nav) previewDir(dir *dir, win *win) {

defer func() {
dir.loading = false
nav.dirChan <- dir
Expand Down Expand Up @@ -829,11 +823,9 @@ func (nav *nav) previewDir(dir *dir, win *win) {
log.Printf("loading dir: %s", buf.Err())
}
}

}

func (nav *nav) preview(path string, win *win) {

reg := &reg{loadTime: time.Now(), path: path}
defer func() { nav.regChan <- reg }()

Expand Down Expand Up @@ -1408,7 +1400,7 @@ func (nav *nav) moveAsync(app *app, srcs []string, dstDir string) {
app.ui.exprChan <- echo
continue
} else if !os.IsNotExist(err) {
ext := filepath.Ext(file)
ext := getFileExtension(dstStat)
basename := file[:len(file)-len(ext)]
var newPath string
for i := 1; !os.IsNotExist(err); i++ {
Expand Down Expand Up @@ -1991,7 +1983,6 @@ func (m indexedSelections) Swap(i, j int) {
func (m indexedSelections) Less(i, j int) bool { return m.indices[i] < m.indices[j] }

func (nav *nav) currSelections() []string {

currDirOnly := gOpts.selmode == "dir"
currDirPath := ""
if currDirOnly {
Expand Down

0 comments on commit f1240e2

Please sign in to comment.