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

Folder-local options for the file manager #623

Closed
1 task done
jyothisv opened this issue Feb 3, 2024 · 5 comments
Closed
1 task done

Folder-local options for the file manager #623

jyothisv opened this issue Feb 3, 2024 · 5 comments
Labels
feature New feature request

Comments

@jyothisv
Copy link

jyothisv commented Feb 3, 2024

Is your feature request related to a problem? Please describe

First of all, thank you very much for this awesome project! I am impressed by Yazi's speed as well as features. I currently use lf and would love to switch over.

One feature that I really like in lf is the ability to set properties only in a specific directory. For example, we can use time-based sorting for say the Downloads directory while keeping natural sorting for all other directories. I think that would be a a nice addition to Yazi.

Will you be willing to contribute this feature?

  • Yes, I'll give it a shot

Describe the solution you'd like

There should be an option to specify directory-specific settings in yazi.toml. Probably we can add a list of directory, settings table under the [manager].

An example:

[[directory_local_settings]]
directory = "~/Downloads"
sort_by = "modified"
sort_reverse = true

If the directory name ends with a slash, the settings would be applied to its sub-directories as well.

A related feature would be a global option to make sorting commands always be directory-local. Usually, when I sort the files in a directory, I don't want the sorting to persist when I go to the parent directory. So this option would be quite useful to me.

Describe alternatives you've considered

No response

Additional context

No response

@jyothisv jyothisv added the feature New feature request label Feb 3, 2024
@sxyazi
Copy link
Owner

sxyazi commented Feb 5, 2024

Sounds good to me. Maybe we can have:

[folder]
rules = [
  { name = "Downloads/", sort_by = "modified", sort_reverse = true, preload = false, ... }
]

to keep the same style as others.

@tianze0926
Copy link

tianze0926 commented May 6, 2024

With the introduction of DDS I can now achieve a basic version using the plugin system:

-- ~/.config/yazi/plugins/folder-rules.yazi/init.lua
return {
    setup = function()
        ps.sub("cd", function()
            local dir = tostring(cx.active.current.cwd)
            if string.find(dir, "Downloads") then
                ya.manager_emit("sort", { "modified", reverse = true, dir_first = true })
            else
                ya.manager_emit("sort", { "alphabetical", reverse = false, dir_first = true })
            end
        end)
    end,
}
-- ~/.config/yazi/init.lua
require("folder-rules"):setup()

But I’m uncertain about how much overhead there would be compared to when it’s implemented in Rust.

@gaesa
Copy link
Contributor

gaesa commented Jun 10, 2024

I disagree with the proposed direction of adding directory-specific settings directly into the configuration file for several reasons:

  1. Frequency of Changes: Directory-specific configurations, such as sorting rules, tend to change much more frequently than general configurations. Storing them in the main configuration file is semantically incorrect.

  2. Convenience: The current approach requires modifying the configuration file every time a directory-specific sort order changes, which is inconvenient for users.

  3. Privacy Concerns: General configurations are often tracked by version control systems like Git, while directory-specific configurations typically are not. Including directory paths in the main configuration file raises privacy issues.

Inspired by GUI file managers, a better approach is to make the currently stateless, globally-affecting sort shortcuts folder-specific and stateful. These states could be stored in a dedicated state directory, such as ~/.local/state/yazi/folders.json or even in a SQLite database.

Example folders.json:

[
  { "name": "~/Downloads/", "sort_by": "modified", "sort_reverse": true }
]

This method provides a clearer separation of concerns, enhances convenience, and protects user privacy. This is one possible improvement over the current proposal, though it might not be the only or the best solution, and further refinement could still be beneficial.

@sxyazi
Copy link
Owner

sxyazi commented Sep 30, 2024

With the introduction of DDS, I think this is now well-supported. I've added it to the Tips page of the documentation, https://yazi-rs.github.io/docs/tips/#folder-rules, based on a code snippet shared by @tianze0926 - thank you for sharing it!

This approach is more flexible because you can call all Yazi-supported commands in Lua, not just sort, but also commands like hidden (to set different hidden states for different directories), linemode (to set different line modes for different directories), etc. You can also programmatically combine any conditions you want, no matter how complex they may be.

And there's no impact on performance - these commands skip unnecessary computations and use cached results. For example, if you set directory A to sort by creation time but it's already sorted by creation time, it won't re-execute, as that would be an unnecessary calculation.

@sxyazi sxyazi closed this as completed Sep 30, 2024
Copy link

I'm going to lock this issue because it has been closed for 30 days. ⏳
This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please file a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New feature request
Projects
None yet
Development

No branches or pull requests

4 participants