feat: complete mactag.yazi

This commit is contained in:
sxyazi
2024-09-28 22:22:36 +08:00
parent c578505961
commit 0949e7654d
4 changed files with 40 additions and 18 deletions

View File

@@ -18,4 +18,6 @@ local function entry(st)
ya.app_emit("resize", {})
end
return { entry = entry }
local function enabled(st) return st.old ~= nil end
return { entry = entry, enabled = enabled }

View File

@@ -4,8 +4,6 @@ Bring macOS's awesome tagging feature to Yazi! The plugin it's only available fo
Authors: [@AnirudhG07](https://github.com/AnirudhG07), and [@sxyazi](https://github.com/sxyazi)
Note that this plugin is still under development.
## Installation
Install the plugin itself, and [jdberry/tag](https://github.com/jdberry/tag) used to tag files:
@@ -22,6 +20,14 @@ Add the following to your `~/.config/yazi/init.lua`:
```lua
require("mactag"):setup {
-- You can change the colors of the tags here
keys = {
r = "Red",
o = "Orange",
y = "Yellow",
g = "Green",
b = "Blue",
p = "Purple",
},
colors = {
Red = "#ee7b70",
Orange = "#f5bd5c",
@@ -29,7 +35,6 @@ require("mactag"):setup {
Green = "#91fc87",
Blue = "#5fa3f8",
Purple = "#cb88f8",
Gray = "#b5b5b9",
},
}
```
@@ -54,14 +59,12 @@ This plugin also provides the functionality to add and remove tags. Add followin
```toml
[[manager.prepend_keymap]]
on = [ "b", "r" ]
run = 'plugin mactag --args="add Red"'
desc = "Tag selected files with red"
on = [ "b", "a" ]
run = 'plugin mactag --args="add"'
desc = "Add tag to selected files"
[[manager.prepend_keymap]]
on = [ "b", "R" ]
run = 'plugin mactag --args="remove Red"'
desc = "Remove red tag from selected files"
on = [ "b", "r" ]
run = 'plugin mactag --args="remove"'
desc = "Remove tag from selected files"
```
`Red` can be any tag name you like. To add/remove multiple tags at once, use a comma (`,`) to separate them, like `Red,Yellow`.

View File

@@ -1,6 +1,6 @@
local update = ya.sync(function(st, tags)
for path, tag in pairs(tags) do
st.tags[path] = #tag == 0 and nil or tag
st.tags[path] = #tag > 0 and tag or nil
end
ya.render()
end)
@@ -18,6 +18,7 @@ end)
local function setup(st, opts)
st.tags = {}
st.keys = opts.keys
st.colors = opts.colors
Linemode:children_add(function(self)
@@ -25,9 +26,9 @@ local function setup(st, opts)
local spans = {}
for _, tag in ipairs(st.tags[url] or {}) do
if self._file:is_hovered() then
spans[#spans + 1] = ui.Span(""):bg(st.colors[tag] or "reset")
spans[#spans + 1] = ui.Span(" "):bg(st.colors[tag] or "reset")
else
spans[#spans + 1] = ui.Span(""):fg(st.colors[tag] or "reset")
spans[#spans + 1] = ui.Span(" "):fg(st.colors[tag] or "reset")
end
end
return ui.Line(spans)
@@ -63,11 +64,25 @@ local function fetch(self)
return 1
end
local cands = ya.sync(function(st)
local t = {}
for k, v in pairs(st.keys) do
t[#t + 1] = { on = k, desc = v }
end
return t
end)
local function entry(_, args)
assert(args[1] == "add" or args[1] == "remove", "Invalid action")
assert(args[2], "No tag specified")
ya.manager_emit("escape", { visual = true })
local t = { args[1] == "remove" and "-r" or "-a", args[2] }
local cands = cands()
local choice = ya.which { cands = cands }
if not choice then
return
end
local t = { args[1] == "remove" and "-r" or "-a", cands[choice].desc }
local files = {}
for _, url in ipairs(selected_or_hovered()) do
t[#t + 1] = tostring(url)

View File

@@ -17,4 +17,6 @@ local function entry(st)
ya.app_emit("resize", {})
end
return { entry = entry }
local function enabled(st) return st.old ~= nil end
return { entry = entry, enabled = enabled }