Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move get file extension logic to util #1670

Merged
merged 3 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
joelim-work marked this conversation as resolved.
Show resolved Hide resolved
extension := getFileExtension(curr)
if len(extension) == 0 {
// no extension or .hidden or is directory
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
} else {
Expand Down
29 changes: 24 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,30 @@ 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 ""
}
if strings.Count(file.Name(), ".") == 1 && file.Name()[0] == '.' {
// hidden file without extension
return ""
}
return filepath.Ext(file.Name())
}

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
38 changes: 38 additions & 0 deletions misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strings"
"testing"
"time"
)

func TestIsRoot(t *testing.T) {
Expand Down Expand Up @@ -293,3 +294,40 @@ func TestNaturalLess(t *testing.T) {
}
}
}

type fakeFileInfo struct {
name string
isDir bool
}

func (fileinfo fakeFileInfo) Name() string { return fileinfo.name }
func (fileinfo fakeFileInfo) Size() int64 { return 0 }
func (fileinfo fakeFileInfo) Mode() os.FileMode { return os.FileMode(0o000) }
func (fileinfo fakeFileInfo) ModTime() time.Time { return time.Unix(0, 0) }
func (fileinfo fakeFileInfo) IsDir() bool { return fileinfo.isDir }
func (fileinfo fakeFileInfo) Sys() any { return nil }

func TestGetFileExtension(t *testing.T) {
tests := []struct {
name string
fileName string
isDir bool
expectedExtension string
}{
{"normal file", "file.txt", false, ".txt"},
{"file without extension", "file", false, ""},
{"hidden file", ".gitignore", false, ""},
{"hidden file with extension", ".file.txt", false, ".txt"},
{"directory", "dir", true, ""},
Limero marked this conversation as resolved.
Show resolved Hide resolved
{"hidden directory", ".git", true, ""},
{"directory with dot", "profile.d", true, ""},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := getFileExtension(fakeFileInfo{test.fileName, test.isDir}); got != test.expectedExtension {
t.Errorf("at input %q expected %q but got %q", test.fileName, test.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