From 0949e7654d462aace413e50e005072213a51d52c Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 28 Sep 2024 22:22:36 +0800 Subject: [PATCH] feat: complete mactag.yazi --- hide-preview.yazi/init.lua | 4 +++- mactag.yazi/README.md | 25 ++++++++++++++----------- mactag.yazi/init.lua | 25 ++++++++++++++++++++----- max-preview.yazi/init.lua | 4 +++- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/hide-preview.yazi/init.lua b/hide-preview.yazi/init.lua index 5040321..ad4df0b 100644 --- a/hide-preview.yazi/init.lua +++ b/hide-preview.yazi/init.lua @@ -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 } diff --git a/mactag.yazi/README.md b/mactag.yazi/README.md index e5644a6..1643a16 100644 --- a/mactag.yazi/README.md +++ b/mactag.yazi/README.md @@ -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`. diff --git a/mactag.yazi/init.lua b/mactag.yazi/init.lua index 1c07a0b..b735073 100644 --- a/mactag.yazi/init.lua +++ b/mactag.yazi/init.lua @@ -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) diff --git a/max-preview.yazi/init.lua b/max-preview.yazi/init.lua index d3d6083..ae4efe6 100644 --- a/max-preview.yazi/init.lua +++ b/max-preview.yazi/init.lua @@ -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 }