Add ability to load a project by name or "on" key

The "load" action now accepts an argument, which is used to find a
project by name/description or the "on" keyboard key.

This can be used to create a keybinding to load a specific project (here
named "Default"):
```yaml
{ on = [ "P", "P"  ], run = "plugin projects 'load Default'", desc = "Load the 'Default'project"  },
```

It can also be used to start Yazi with a specific project using a
script:
```bash
#!/bin/bash

#
# Open a project in Yazi.
#
# https://yazi-rs.github.io/docs/plugins/utils/#ya.emit
# https://github.com/MasouShizuka/projects.yazi
#

export yaziProject="$1"
shift

if [ -z "$yaziProject" ]; then
    >&2 echo "ERROR: The first argument must be a project"
    exit 64
fi

# Generate random Yazi client ID (DDS / `ya emit` uses `YAZI_ID`)
export YAZI_ID=$RANDOM

# Use Yazi's DDS to run a plugin command after Yazi has started
# (the nested subshell is only to suppress "Done" output for the job)
( (sleep 0.1; ya emit plugin projects "load $yaziProject") &)

# Run Yazi with the generated client ID
yazi --client-id $YAZI_ID "$@"
```
This commit is contained in:
Sami Kankaristo
2025-06-17 19:28:16 +03:00
parent 82febd5b45
commit fb00b35266

View File

@@ -930,6 +930,21 @@ local _merge_event = ya.sync(function(state)
end)
end)
local _find_project_index = function(list, search_term)
if not search_term then
return nil
end
for i, project in ipairs(list) do
-- Match the project by the "on" key or by "desc"
if project.on == search_term or project.desc == search_term then
return i
end
end
return nil
end
return {
setup = function(_, opts)
_load_config(opts)
@@ -1012,7 +1027,12 @@ return {
return
end
local selected_idx = ya.which({ cands = list, silent = false })
local selected_idx = (
-- Search for the project, if an argument was given
_find_project_index(list, job.args[2])
-- Ask interactively
or ya.which({ cands = list, silent = false })
)
if not selected_idx then
return
end