Upgraded yazi plugins

This commit is contained in:
2025-02-19 12:47:15 +00:00
parent 4ace4ba9a0
commit cc00c77508
21 changed files with 243 additions and 109 deletions

View File

@@ -1,5 +1,42 @@
[plugin]
deps = [{ use = "Reledia/glow", rev = "5ce76dc" }, { use = "Reledia/hexyl", rev = "39d3d4e" }, { use = "Rolv-Apneseth/starship", rev = "9c37d37" }, { use = "yazi-rs/plugins:git", rev = "71c4fc2" }, { use = "yazi-rs/plugins:full-border", rev = "71c4fc2" }, { use = "yazi-rs/plugins:chmod", rev = "71c4fc2" }, { use = "yazi-rs/plugins:max-preview", rev = "71c4fc2" }, { use = "yazi-rs/plugins:mime-ext", rev = "71c4fc2" }]
[[plugin.deps]]
use = "Reledia/glow"
rev = "5ce76dc"
hash = "52e5f5c602962e7cbf874da28f52ba45"
[[plugin.deps]]
use = "Reledia/hexyl"
rev = "39d3d4e"
hash = "dd624cbaff94af65f39fd86bc57b340"
[[plugin.deps]]
use = "Rolv-Apneseth/starship"
rev = "f6939fb"
hash = "8ae899541dc7accb680ee4fd382a09c"
[[plugin.deps]]
use = "yazi-rs/plugins:git"
rev = "beb586a"
hash = "771f18427fb75fb19990ce602bb322f4"
[[plugin.deps]]
use = "yazi-rs/plugins:full-border"
rev = "beb586a"
hash = "ae9e1d0c6bfd68cdebc98cc684c22b45"
[[plugin.deps]]
use = "yazi-rs/plugins:chmod"
rev = "beb586a"
hash = "f28138c2e11e87962b66d583fef724c3"
[[plugin.deps]]
use = "yazi-rs/plugins:max-preview"
rev = "beb586a"
hash = "15b2ff7f6563c14d5b2e93b3e9da35de"
[[plugin.deps]]
use = "yazi-rs/plugins:mime-ext"
rev = "beb586a"
hash = "b03f3d80d26b6d7bb490b1380e9cb754"
[flavor]
deps = []

View File

@@ -1,3 +1,5 @@
--- @since 25.2.7
local selected_or_hovered = ya.sync(function()
local tab, paths = cx.active, {}
for _, u in pairs(tab.selected) do

View File

@@ -1,3 +1,5 @@
--- @since 25.2.7
local function setup(_, opts)
local type = opts and opts.type or ui.Border.ROUNDED
local old_build = Tab.build
@@ -17,9 +19,9 @@ local function setup(_, opts)
local c = self._chunks
self._chunks = {
c[1]:padding(ui.Padding.y(1)),
c[2]:padding(ui.Padding(c[1].w > 0 and 0 or 1, c[3].w > 0 and 0 or 1, 1, 1)),
c[3]:padding(ui.Padding.y(1)),
c[1]:pad(ui.Pad.y(1)),
c[2]:pad(ui.Pad(1, c[3].w > 0 and 0 or 1, 1, c[1].w > 0 and 0 or 1)),
c[3]:pad(ui.Pad.y(1)),
}
local style = THEME.manager.border_style

View File

@@ -1,7 +1,7 @@
# git.yazi
> [!NOTE]
> Yazi v0.4.1 or later is required for this plugin to work.
> Yazi v25.2.7 or later is required for this plugin to work.
Show the status of Git file changes as linemode in the file list.

View File

@@ -1,3 +1,5 @@
--- @since 25.2.7
local WIN = ya.target_family() == "windows"
local PATS = {
{ "[MT]", 6 }, -- Modified
@@ -145,11 +147,11 @@ local function setup(st, opts)
end
if not change or signs[change] == "" then
return ui.Line("")
return ""
elseif self._file:is_hovered() then
return ui.Line { ui.Span(" "), ui.Span(signs[change]) }
return ui.Line { " ", signs[change] }
else
return ui.Line { ui.Span(" "), ui.Span(signs[change]):style(styles[change]) }
return ui.Line { " ", ui.Span(signs[change]):style(styles[change]) }
end
end, opts.order)
end
@@ -159,7 +161,7 @@ local function fetch(_, job)
local repo = root(cwd)
if not repo then
remove(tostring(cwd))
return 1
return true
end
local paths = {}
@@ -175,8 +177,7 @@ local function fetch(_, job)
:stdout(Command.PIPED)
:output()
if not output then
ya.err("Cannot spawn git command, error: " .. err)
return 0
return true, Err("Cannot spawn `git` command, error: %s", err)
end
local changed, ignored = {}, {}
@@ -202,7 +203,7 @@ local function fetch(_, job)
end
add(tostring(cwd), repo, changed)
return 3
return false
end
return { setup = setup, fetch = fetch }

View File

@@ -1,3 +1,4 @@
--- @since 25.2.7
--- @sync entry
local function entry(st)

View File

@@ -17,7 +17,6 @@ Add this to your `~/.config/yazi/yazi.toml`:
```toml
[[plugin.prepend_fetchers]]
id = "mime"
if = "!(mime|dummy)"
name = "*"
run = "mime-ext"
prio = "high"

View File

@@ -1,8 +1,11 @@
--- @since 25.2.7
local FILES = {
[".envrc"] = "text/plain",
[".gitconfig"] = "text/plain",
[".gitignore"] = "text/plain",
[".luacheckrc"] = "text/lua",
[".npmrc"] = "text/plain",
[".styluaignore"] = "text/plain",
[".zshenv"] = "text/plain",
[".zshrc"] = "text/plain",
@@ -72,6 +75,8 @@ local EXTS = {
bcpio = "application/bcpio",
bdf = "application/font-bdf",
bdm = "application/syncml.dm+wbxml",
bean = "text/plain",
beancount = "text/plain",
bed = "application/realvnc.bed",
bh2 = "application/fujitsu.oasysprs",
bin = "application/octet-stream",
@@ -602,6 +607,7 @@ local EXTS = {
p7s = "application/pkcs7-signature",
p8 = "application/pkcs8",
pas = "text/pascal",
patch = "text/diff",
paw = "application/pawaafile",
pbd = "application/powerbuilder6",
pbm = "image/portable-bitmap",
@@ -1064,8 +1070,13 @@ function M:fetch(job)
local merged_files = ya.dict_merge(FILES, opts.with_files or {})
local merged_exts = ya.dict_merge(EXTS, opts.with_exts or {})
local updates, unknown = {}, {}
for _, file in ipairs(job.files) do
local updates, unknown, state = {}, {}, {}
for i, file in ipairs(job.files) do
if file.cha.is_dummy then
state[i] = false
goto continue
end
local mime
if file.cha.len == 0 then
mime = "inode/empty"
@@ -1075,12 +1086,13 @@ function M:fetch(job)
end
if mime then
updates[tostring(file.url)] = mime
updates[tostring(file.url)], state[i] = mime, true
elseif opts.fallback_file1 then
unknown[#unknown + 1] = file
else
updates[tostring(file.url)] = "application/octet-stream"
updates[tostring(file.url)], state[i] = "application/octet-stream", true
end
::continue::
end
if next(updates) then
@@ -1088,11 +1100,27 @@ function M:fetch(job)
end
if #unknown > 0 then
job.files = unknown
return require("mime"):fetch(job)
return self.fallback_builtin(job, unknown, state)
end
return 1
return state
end
function M.fallback_builtin(job, unknown, state)
local indices = {}
for i, f in ipairs(job.files) do
indices[f:hash()] = i
end
local result = require("mime"):fetch(ya.dict_merge(job, { files = unknown }))
for i, f in ipairs(unknown) do
if type(result) == "table" then
state[indices[f:hash()]] = result[i]
else
state[indices[f:hash()]] = result
end
end
return state
end
return M

View File

@@ -32,15 +32,24 @@ Add this to `~/.config/yazi/init.lua`:
require("starship"):setup()
```
If you wish to define a custom config file for `starship` to use, you can pass in a path
to the setup function like this:
Make sure you have [starship](https://github.com/starship/starship) installed and in your `PATH`.
## Config
Here is an example with all available config options:
```lua
starship:setup({ config_file = "/home/rolv/.config/starship_secondary.toml" })
require("starship"):setup({
-- Hide flags (such as filter, find and search). This is recommended for starship themes which
-- are intended to go across the entire width of the terminal.
hide_flags = false, -- Default: false
-- Whether to place flags after the starship prompt. False means the flags will be placed before the prompt.
flags_after_prompt = true, -- Default: true
-- Custom starship configuration file to use
config_file = "~/.config/starship_full.toml", -- Default: nil
})
```
Make sure you have [starship](https://github.com/starship/starship) installed and in your `PATH`.
## Extra
If you use a `starship` theme with a background colour, it might look a bit to cramped on just the one line `Yazi` gives the header by default. To fix this, you can add this to your `init.lua`:

View File

@@ -1,83 +0,0 @@
local save = ya.sync(function(st, cwd, output)
if cx.active.current.cwd == Url(cwd) then
st.output = output
ya.render()
end
end)
-- Helper function for accessing the `config_file` state variable
---@return string
local get_config_file = ya.sync(function(st)
return st.config_file
end)
return {
---User arguments for setup method
---@class SetupArgs
---@field config_file string Absolute path to a starship config file
--- Setup plugin
--- @param st table State
--- @param args SetupArgs|nil
setup = function(st, args)
-- Replace default header widget
Header:children_remove(1, Header.LEFT)
Header:children_add(function()
return ui.Line.parse(st.output or "")
end, 1000, Header.LEFT)
-- Check for custom starship config file
if args ~= nil and args.config_file ~= nil then
local url = Url(args.config_file)
if url.is_regular then
local config_file = args.config_file
-- Manually replace '~' and '$HOME' at the start of the path with the OS environment variable
local home = os.getenv("HOME")
if home then
home = tostring(home)
config_file = config_file:gsub("^~", home):gsub("^$HOME", home)
end
st.config_file = config_file
end
end
-- Pass current working directory and custom config path (if specified) to the plugin's entry point
---Callback for subscribers to update the prompt
local callback = function()
local cwd = cx.active.current.cwd
if st.cwd ~= cwd then
st.cwd = cwd
ya.manager_emit("plugin", {
st._id,
args = ya.quote(tostring(cwd), true),
})
end
end
-- Subscribe to events
ps.sub("cd", callback)
ps.sub("tab", callback)
end,
entry = function(_, job_or_args)
-- yazi 2024-11-29 changed the way arguments are passed to the plugin
-- entry point. They were moved inside {args = {...}}. If the user is using
-- a version before this change, they can use the old implementation.
-- https://github.com/sxyazi/yazi/pull/1966
local args = job_or_args.args or job_or_args
local command = Command("starship"):arg("prompt"):cwd(args[1]):env("STARSHIP_SHELL", "")
-- Point to custom starship config
local config_file = get_config_file()
if config_file then
command = command:env("STARSHIP_CONFIG", config_file)
end
local output = command:output()
if output then
save(args[1], output.stdout:gsub("^%s+", ""))
end
end,
}

View File

@@ -0,0 +1,138 @@
--- @since 25.2.7
-- For development
--[[ local function notify(message) ]]
--[[ ya.notify({ title = "Starship", content = message, timeout = 5 }) ]]
--[[ end ]]
local save = ya.sync(function(st, cwd, output)
if cx.active.current.cwd == Url(cwd) then
st.output = output
ya.render()
end
end)
-- Helper function for accessing the `config_file` state variable
---@return string
local get_config_file = ya.sync(function(st)
return st.config_file
end)
return {
---User arguments for setup method
---@class SetupArgs
---@field config_file string Absolute path to a starship config file
---@field hide_flags boolean Whether to hide all flags (such as filter and search). Recommended for themes which are intended to take the full width of the terminal.
---@field flags_after_prompt boolean Whether to place flags (such as filter and search) after the starship prompt. By default this is true.
--- Setup plugin
--- @param st table State
--- @param args SetupArgs|nil
setup = function(st, args)
local hide_flags = false
local flags_after_prompt = true
-- Check setup args
if args ~= nil then
if args.config_file ~= nil then
local url = Url(args.config_file)
if url.is_regular then
local config_file = args.config_file
-- Manually replace '~' and '$HOME' at the start of the path with the OS environment variable
local home = os.getenv("HOME")
if home then
home = tostring(home)
config_file = config_file:gsub("^~", home):gsub("^$HOME", home)
end
st.config_file = config_file
end
end
if args.hide_flags ~= nil then
hide_flags = args.hide_flags
end
if args.flags_after_prompt ~= nil then
flags_after_prompt = args.flags_after_prompt
end
end
-- Replace default header widget
Header:children_remove(1, Header.LEFT)
Header:children_add(function(self)
local max = self._area.w - self._right_width
if max <= 0 then
return ""
end
if hide_flags or not st.output then
return ui.Line.parse(st.output or "")
end
-- Split `st.output` at the first line break (or keep as is if none was found)
local output = st.output:match("([^\n]*)\n?") or st.output
local flags = self:flags()
if flags_after_prompt then
output = output .. " " .. flags
else
output = flags .. " " .. output
end
return ui.Line.parse(output)
end, 1000, Header.LEFT)
-- Pass current working directory and custom config path (if specified) to the plugin's entry point
---Callback for subscribers to update the prompt
local callback = function()
local cwd = cx.active.current.cwd
if st.cwd ~= cwd then
st.cwd = cwd
if ya.confirm then
-- >= yazi 25.2.7
ya.manager_emit("plugin", {
st._id,
ya.quote(tostring(cwd), true),
})
else
-- < yazi 25.2.7
ya.manager_emit("plugin", {
st._id,
args = ya.quote(tostring(cwd), true),
})
end
end
end
-- Subscribe to events
ps.sub("cd", callback)
ps.sub("tab", callback)
end,
entry = function(_, job_or_args)
-- yazi 2024-11-29 changed the way arguments are passed to the plugin
-- entry point. They were moved inside {args = {...}}. If the user is using
-- a version before this change, they can use the old implementation.
-- https://github.com/sxyazi/yazi/pull/1966
local args = job_or_args.args or job_or_args
local command = Command("starship")
:arg("prompt")
:stdin(Command.INHERIT)
:cwd(args[1])
:env("STARSHIP_SHELL", "")
-- Point to custom starship config
local config_file = get_config_file()
if config_file then
command = command:env("STARSHIP_CONFIG", config_file)
end
local output = command:output()
if output then
save(args[1], output.stdout:gsub("^%s+", ""))
end
end,
}