sxyazi / yazi

💥 Blazing fast terminal file manager written in Rust, based on async I/O.
https://yazi-rs.github.io
MIT License
15.07k stars 341 forks source link

Contextual preview panel visibility #1708

Closed Acumane closed 2 hours ago

Acumane commented 3 hours ago

yazi --debug output

N/A

Please describe the problem you're trying to solve

Only show the preview panel for certain MIME types and optionally hide it on error/unsupported types.

Would you be willing to contribute this feature?

Describe the solution you'd like

Example: I really only care to see previews of images

# yazi.toml
[open]
rules = [
    # Folder
    { name = "*/", use = [ "edit", "open", "reveal" ], preview = false },
    # Text
    { mime = "text/*", use = [ "edit", "reveal" ], preview = false },
    # Image
    { mime = "image/*", use = [ "open", "reveal" ], preview = true  },
    # ...
]

(I know it's under [open] but we're already enumerating MIME types here, so...)

Additional context

No response

Validations

sxyazi commented 2 hours ago

We already have the hide-preview.yazi plugin, and it provides an API to do so.

Based on that, implementing your request is very easy and only requires a few lines of Lua code, save these lines as ~/.config/yazi/plugins/contextual-preview.yazi/init.lua:

local function setup()
  ps.sub("hover", function()
    local h = cx.active.current.hovered
    if not h then
      return
    end

    local is_image = (h:mime() or ""):find("image/") ~= nil
    if is_image == require("hide-preview"):enabled() then
      require("hide-preview"):entry()
    end
  end)
end

return { setup = setup }

Then enable it in your init.lua:

require("contextual-preview"):setup()

https://github.com/user-attachments/assets/e07a0313-0ad2-4e5e-988e-23c48b48f5ae