-
-
Notifications
You must be signed in to change notification settings - Fork 248
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
Improving file panel rendering. #589
Open
lazysegtree
wants to merge
3
commits into
main
Choose a base branch
from
fix_tmp_issue_585
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−63
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"os" | ||
"path/filepath" | ||
"regexp" | ||
"slices" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
@@ -41,104 +42,98 @@ func returnFocusType(focusPanel focusPanelType) filePanelFocusType { | |
return secondFocus | ||
} | ||
|
||
func returnFolderElement(location string, displayDotFile bool, sortOptions sortOptionsModelData) (directoryElement []element) { | ||
|
||
files, err := os.ReadDir(location) | ||
if len(files) == 0 { | ||
func returnDirElement(location string, displayDotFile bool, sortOptions sortOptionsModelData) (directoryElement []element) { | ||
dirEntries, err := os.ReadDir(location) | ||
if err != nil { | ||
outPutLog("Return folder element function error", err) | ||
return directoryElement | ||
} | ||
|
||
if err != nil { | ||
outPutLog("Return folder element function error", err) | ||
dirEntries = slices.DeleteFunc(dirEntries, func(e os.DirEntry) bool { | ||
// Entries not needed to be considered | ||
_, err := e.Info() | ||
return err != nil || (strings.HasPrefix(e.Name(), ".") && !displayDotFile) | ||
}) | ||
|
||
// No files/directoes to process | ||
if len(dirEntries) == 0 { | ||
return directoryElement | ||
} | ||
|
||
// Sort files | ||
var order func(i, j int) bool | ||
reversed := sortOptions.reversed | ||
|
||
// Todo : These strings should not be hardcoded here, but defined as constants | ||
switch sortOptions.options[sortOptions.selected] { | ||
case "Name": | ||
order = func(i, j int) bool { | ||
_, errI := os.ReadDir(location + "/" + files[i].Name()) | ||
_, errJ := os.ReadDir(location + "/" + files[j].Name()) | ||
if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) { | ||
return true | ||
} | ||
if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) { | ||
return false | ||
slog.Debug("sort func", "i", i, "j", j) | ||
|
||
// One of them is a directory, and other is not | ||
if dirEntries[i].IsDir() != dirEntries[j].IsDir() { | ||
return dirEntries[i].IsDir() | ||
} | ||
|
||
if Config.CaseSensitiveSort { | ||
return files[i].Name() < files[j].Name() != reversed | ||
return dirEntries[i].Name() < dirEntries[j].Name() != reversed | ||
} else { | ||
return strings.ToLower(files[i].Name()) < strings.ToLower(files[j].Name()) != reversed | ||
return strings.ToLower(dirEntries[i].Name()) < strings.ToLower(dirEntries[j].Name()) != reversed | ||
} | ||
} | ||
case "Size": | ||
order = func(i, j int) bool { | ||
// Directories at the top sorted by direct child count (not recursive) | ||
// Files sorted by size | ||
fileInfoI, _ := files[i].Info() | ||
fileInfoJ, _ := files[j].Info() | ||
_, errI := os.ReadDir(location + "/" + files[i].Name()) | ||
_, errJ := os.ReadDir(location + "/" + files[j].Name()) | ||
|
||
if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) { | ||
return true | ||
} | ||
if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) { | ||
return false | ||
// One of them is a directory, and other is not | ||
if dirEntries[i].IsDir() != dirEntries[j].IsDir() { | ||
return dirEntries[i].IsDir() | ||
} | ||
if files[i].IsDir() && files[j].IsDir() { | ||
filesI, err := os.ReadDir(filepath.Join(location, files[i].Name())) | ||
|
||
// This needs to be improved, and we should sort by actual size only | ||
// Repeated recursive read would be slow, so we could cache | ||
if dirEntries[i].IsDir() && dirEntries[j].IsDir() { | ||
filesI, err := os.ReadDir(filepath.Join(location, dirEntries[i].Name())) | ||
// No need of early return, we only call len() on filesI, so nil would | ||
// just result in 0 | ||
if err != nil { | ||
outPutLog("Error when reading directory", err) | ||
outPutLog("Error when reading directory during sort", err) | ||
} | ||
filesJ, err := os.ReadDir(filepath.Join(location, files[j].Name())) | ||
filesJ, err := os.ReadDir(filepath.Join(location, dirEntries[j].Name())) | ||
if err != nil { | ||
outPutLog("Error when reading directory", err) | ||
outPutLog("Error when reading directory during sort", err) | ||
} | ||
return len(filesI) < len(filesJ) != reversed | ||
} else { | ||
// No need for err check, we already filtered out dirEntries with err != nil in Info() call | ||
fileInfoI, _ := dirEntries[i].Info() | ||
fileInfoJ, _ := dirEntries[j].Info() | ||
return fileInfoI.Size() < fileInfoJ.Size() != reversed | ||
} | ||
return fileInfoI.Size() < fileInfoJ.Size() != reversed | ||
|
||
} | ||
case "Date Modified": | ||
order = func(i, j int) bool { | ||
fileInfoI, _ := files[i].Info() | ||
fileInfoJ, _ := files[j].Info() | ||
// No need for err check, we already filtered out dirEntries with err != nil in Info() call | ||
fileInfoI, _ := dirEntries[i].Info() | ||
fileInfoJ, _ := dirEntries[j].Info() | ||
return fileInfoI.ModTime().After(fileInfoJ.ModTime()) != reversed | ||
} | ||
} | ||
|
||
sort.Slice(files, order) | ||
|
||
for _, item := range files { | ||
fileInfo, err := item.Info() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need of filtering dirEntries after. Filtered them before the sort. |
||
if err != nil { | ||
continue | ||
} | ||
|
||
if !displayDotFile && strings.HasPrefix(fileInfo.Name(), ".") { | ||
continue | ||
} | ||
if fileInfo == nil { | ||
continue | ||
} | ||
newElement := element{ | ||
sort.Slice(dirEntries, order) | ||
for _, item := range dirEntries { | ||
directoryElement = append(directoryElement, element{ | ||
name: item.Name(), | ||
directory: item.IsDir(), | ||
} | ||
if location == "/" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is redundant. filepath.Join() handles location being "/" |
||
newElement.location = location + item.Name() | ||
} else { | ||
newElement.location = filepath.Join(location, item.Name()) | ||
} | ||
directoryElement = append(directoryElement, newElement) | ||
location: filepath.Join(location, item.Name()), | ||
}) | ||
} | ||
|
||
return directoryElement | ||
} | ||
|
||
func returnFolderElementBySearchString(location string, displayDotFile bool, searchString string) (folderElement []element) { | ||
func returnDirElementBySearchString(location string, displayDotFile bool, searchString string) (dirElement []element) { | ||
|
||
items, err := os.ReadDir(location) | ||
if err != nil { | ||
|
@@ -182,15 +177,15 @@ func returnFolderElementBySearchString(location string, displayDotFile bool, sea | |
for _, item := range result.Matches { | ||
resultItem := folderElementMap[item.Key] | ||
resultItem.matchRate = float64(item.Score) | ||
folderElement = append(folderElement, resultItem) | ||
dirElement = append(dirElement, resultItem) | ||
} | ||
|
||
// Sort folders and files by match rate | ||
sort.Slice(folderElement, func(i, j int) bool { | ||
return folderElement[i].matchRate > folderElement[j].matchRate | ||
sort.Slice(dirElement, func(i, j int) bool { | ||
return dirElement[i].matchRate > dirElement[j].matchRate | ||
}) | ||
|
||
return folderElement | ||
return dirElement | ||
} | ||
|
||
func panelElementHeight(mainPanelHeight int) int { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We dont need to do ReadDir syscall here at all. We can just decide based on IsDir()