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

feat!: support passing arguments to previewer/preloader/spotter/fetcher #1966

Merged
merged 2 commits into from
Nov 29, 2024

Conversation

sxyazi
Copy link
Owner

@sxyazi sxyazi commented Nov 28, 2024

This PR adds the ability to pass parameters to the previewer/preloader/spotter/fetcher. For example, you can pass parameters to your previewer in yazi.toml like this:

# yazi.toml
[plugin]
prepend_previewers = [
  { mime = "video/*", run = 'my-plugin test --foo --bar=baz' }
]

Then, the first parameter job in the peek() method of the my-plugin plugin will receive it:

-- ~/.config/yazi/plugins/my-plugin.yazi/init.lua
local M = {}

function M:peek(job)
  ya.err(job)
end

return M

Which logs:

{
  area = Rect: 0x600002a80d58,
  args = {  -- <- here
    [1] = "test",
    foo = true,
    bar = "baz",
  },
  file = File: 0x600002a80da8,
  mime = "video/mp4",
  skip = 0,
}

Similarly, preloader/spotter/fetcher also support parameter passing:

prepend_preloaders = [
  { mime = "image/*", run = 'my-plugin test --foo --bar=baz' }
]
prepend_spotters = [
  { mime = "image/*", run = 'my-plugin test --foo --bar=baz' }
]
prepend_fetchers = [
  { id = "test", mime = "image/*", run = 'my-plugin test --foo --bar=baz' }
]

Then, their respective preload(), spot(), and fetch() methods will receive the job.args:

-- ~/.config/yazi/plugins/my-plugin.yazi/init.lua
local M = {}

function M:preload(job)
  ya.err("preload args:", job.args)
end

function M:spot(job)
  ya.err("spot args:", job.args)
end

function M:fetch(job)
  ya.err("fetch args:", job.args)
end

return M

⚠️ Breaking changes

1. Task information for peek()/seek()/preload() has been moved from self to the job parameter.

Before:

function M:peek()
  ya.err("area:", self.area)
  ya.err("file:", self.file)
  ya.err("skip:", self.skip)
end

Now:

function M:peek(job)
  ya.err("area:", job.area)
  ya.err("file:", job.file)
  ya.err("skip:", job.skip)
end

These properties can still be accessed via self, but a deprecation warning will be shown. This compatibility layer will be fully removed in Yazi 0.4.2.

The change aims to resolve potential conflicts between Yazi’s internal properties and user plugin properties.

2. The first parameter of entry() has been replaced with the new job parameter.

Before:

function M:entry(args)
  ya.err("first arg:", args[1])
end

Now:

function M:entry(job)
  ya.err("first arg:", job.args[1])
end

The new job can still be used just like the original args (like job[1] or ipairs(job)), but a deprecation warning will be shown. This compatibility layer will be fully removed in Yazi 0.4.2.

3. The first parameter of seek() has been changed from a number units to a table job.

Before:

function M:seek(units)
  ya.err("units:", units)
end

Now:

function M:seek(job)
  ya.err("units:", job.units)
end

This change is made to provide extensibility for adding more fields in the future.

README.md Show resolved Hide resolved
@sxyazi sxyazi merged commit d388d4e into main Nov 29, 2024
6 checks passed
@sxyazi sxyazi deleted the pr-a19cbdfe branch November 29, 2024 13:48
sxyazi added a commit to yazi-rs/plugins that referenced this pull request Nov 29, 2024
TornaxO7 added a commit to TornaxO7/bookmarks.yazi that referenced this pull request Nov 29, 2024
boydaihungst added a commit to boydaihungst/mediainfo.yazi that referenced this pull request Nov 29, 2024
zooeywm added a commit to zooeywm/sudo.yazi that referenced this pull request Nov 30, 2024
TD-Sky pushed a commit to TD-Sky/sudo.yazi that referenced this pull request Nov 30, 2024
…for `assets`

* `ya pack` -a support assets folder contains miscellaneous files

* sxyazi/yazi#1966
GrzegorzKozub added a commit to GrzegorzKozub/mdcat.yazi that referenced this pull request Nov 30, 2024
mikavilpas added a commit to mikavilpas/starship.yazi that referenced this pull request Nov 30, 2024
Issue
=====

The nightly version of yazi has changed the way arguments are passed to
the plugin entry point. Previously they were a table that contained the
arguments, but they were moved inside {args = {...}}.

If the user is using a nightly version of yazi, they will see a
deprecation warning.

sxyazi/yazi#1966

Solution
========

This change removes the warning and supports both versions.
Rolv-Apneseth pushed a commit to Rolv-Apneseth/starship.yazi that referenced this pull request Nov 30, 2024
Issue
=====

The nightly version of yazi has changed the way arguments are passed to
the plugin entry point. Previously they were a table that contained the
arguments, but they were moved inside {args = {...}}.

If the user is using a nightly version of yazi, they will see a
deprecation warning.

sxyazi/yazi#1966

Solution
========

This change removes the warning and supports both versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant