From fb00b35266c39c7f0d2938590c215712bca06d26 Mon Sep 17 00:00:00 2001 From: Sami Kankaristo Date: Tue, 17 Jun 2025 19:28:16 +0300 Subject: [PATCH] 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 "$@" ``` --- main.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main.lua b/main.lua index e76c680..9833a3d 100644 --- a/main.lua +++ b/main.lua @@ -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