Sort by custom method
Is there a way to sort by a user-defined method, either by passing lf the sorted file list or passing it a function to sort by? If not, would it be too hard to implement?
@XPhyro Unfortunately, there are no such custom sorting possible. And if people came up with a workaround, I'm not aware of it.
I can't think of an easy way to implement it either. I think we would essentially need some form of lambda functions to implement this and we don't even have any control flow in the configuration syntax at this point. So we rely on implementing sorting orders as builtin. I think we have a few missing sorting orders waiting as feature requests. Feel free to suggest another if yours is not on the list and it's not too specific to your use case.
Sadly, I was hoping for specific cases for different directories. I don't think I can implement a control flow to allow for the passing of functions to sort by, but I'll have a look around the codebase to see if I can find an easy way to do this.
Before diving into the codebase, I'm thinking: piping or passing as a file the sorted list seems inefficient, but the easiest to accomplish. It might also be the most extensible on the user side. Could this be implemented by adding a special command that we pipe the list to and receive the sorted list from stdout? It could have the same syntax as any other user-defined shell command.
@XPhyro Any chance you are looking for #117 ? If not, can you give an example for what you have in mind? I'm not a fan on adding new features without proper use cases. Also, I think you are right, it would be much easier to have an external command to take a list of names and output a sorted list.
No, I am looking for proper custom sorts. The mentioned issue is not equivalent, but what I am describing would definitely benefit from it.
Allow me to elaborate. I have some directories that I use as galleries, that I sometimes like to just explore with lf. For this, I want to sort the directory randomly to have a pseudo-unique experience every time. This is possible with alternatives like ranger that include random sorting. More importantly, I want to sort git repositories in a special way. I thought it would be useful if untracked files were at the end, changed files were at the beginning, and regular tracked files were in the middle. This, as far as I know, is not included in any of the file managers. I don't really have another purpose (currently), but I'm sure I would come up with more minor ways to spice up lf. Though there are clear use cases, these are non-essential and extremely specific, so you might rightfully prefer not to include the functionality in lf.
@XPhyro There was a feature request for random sorting. I wasn't very interested in it but I guess I would merge if a clean patch is submitted. Git use case is more interesting and it might be useful for other users as well. So then maybe we could add an option like sorter much like previewer and cleaner that would take the path of a directory as an argument and sorts file names from stdin to stdout. That can be added to readdir function in nav.go before the for loop to sort names. Then, we might also need further checks in the code to somehow prevent sortby to resort these directories. I can't think of many complications so I think we might merge such a patch if someone sends a PR. Even if the patch is not merged, I don't think it is too difficult to maintain such a patch outside of the tree as the core functionality should be more or less stable these days.
Sounds reasonable. I'll take a shot at implementing this.
I'd like to give some updates on the progress of this feature. First off, I haven't had much time to work on it and this is my first experience with go, so it's still in an early stage.
The sorting mechanism works but I brought many bugs with it. After the sorting, the selected file essentially becomes random. I'd rather it select the first file (after sorting) if it is a new directory, and the previously selected file if not. Furthermore, I experience some random crashes when the reload command is used in succession (which only happens when I use the modified lf).
I'll send a draft PR when I get rid of these issues and clean up the code.
Hi I'm also interested in this. Maybe something changed in 3 years? :smile:
My use-case is the following: I have this set of directories, which I want to be shown in this order (which I achieve with the numeral prefixes):
0_inbox
1_project
2_areas
3_resources
4_archive
Now cding into them is kind of annoying because of the prefix. I'd rather have them named inbox, etc. while retaining the above order.
@jneidel I don't think anything changed. There is still no support for this within my knowledge, and the aforementioned patch of mine was too unstable & hacky. If, at some point, I find the time to learn some proper Go, I'll give it another shot.