feat: new mactag plugin

Co-authored-by: Anirudh Gupta <146579014+AnirudhG07@users.noreply.github.com>
This commit is contained in:
sxyazi
2024-08-12 22:51:05 +08:00
parent ba2a7d7633
commit 2dc65ab07d
4 changed files with 156 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ For specific installation commands and configuration instructions, check the ind
- [jump-to-char.yazi](jump-to-char.yazi) - Vim-like `f<char>`, jump to the next file whose name starts with `<char>`.
- [diff.yazi](diff.yazi) - Diff the selected file with the hovered file, create a living patch, and copy it to the clipboard.
- [no-status.yazi](no-status.yazi) - Remove the status bar.
- [mactag.yazi](mactag.yazi) - Bring macOS's awesome tagging feature to Yazi! The plugin is only available for macOS just like the name says.
Note that `ya` is a newly introduced standalone CLI binary, not a shell alias for Yazi (See https://github.com/sxyazi/yazi/issues/914 for details)

21
mactag.yazi/LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 yazi-rs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

54
mactag.yazi/README.md Normal file
View File

@@ -0,0 +1,54 @@
# mactag.yazi
Bring macOS's awesome tagging feature to Yazi! The plugin it's only available for macOS just like the name says.
Authors: [@AnirudhG07](https://github.com/AnirudhG07), and [@sxyazi](https://github.com/sxyazi)
## Installation
Install the plugin itself, and [jdberry/tag](https://github.com/jdberry/tag) used to tag files:
```sh
ya pack -a yazi-rs/plugins#mactag
brew update && brew install tag
```
## Setup
Add the following to your `~/.config/yazi/init.lua`:
```lua
require("mactag"):setup()
```
And register it as fetchers in your `~/.config/yazi/yazi.toml`:
```toml
[[plugin.prepend_fetchers]]
id = "mactag"
name = "*"
run = "mactag"
[[plugin.prepend_fetchers]]
id = "mactag"
name = "*/"
run = "mactag"
```
## Usage
This plugin also provides the functionality to add and remove tags. Add following keybindings to your `~/.config/yazi/keymap.toml` to enable it:
```toml
[[manager.prepend_keymap]]
on = [ "t", "r" ]
run = 'plugin mactag --args="add Red"'
desc = "Tag selected files with red"
[[manager.prepend_keymap]]
on = [ "t", "R" ]
run = 'plugin mactag --args="remove Red"'
desc = "Remove red 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`.

80
mactag.yazi/init.lua Normal file
View File

@@ -0,0 +1,80 @@
local COLORS = { red = "red", yellow = "yellow", orange = "magenta", green = "green", blue = "blue" }
local update = ya.sync(function(st, tags)
for path, tag in pairs(tags) do
st.tags[path] = #tag == 0 and nil or tag
end
ya.render()
end)
local selected_or_hovered = ya.sync(function()
local tab, urls = cx.active, {}
for _, u in pairs(tab.selected) do
urls[#urls + 1] = u
end
if #urls == 0 and tab.current.hovered then
urls[1] = tab.current.hovered.url
end
return urls
end)
local function setup(st)
st.tags = {}
Linemode:children_add(function(self)
local url = tostring(self._file.url)
local spans = {}
for _, tag in ipairs(st.tags[url] or {}) do
spans[#spans + 1] = ui.Span(""):fg(COLORS[tag:lower()] or "reset")
end
return ui.Line(spans)
end, 500)
end
local function fetch(self)
local paths = {}
for _, file in ipairs(self.files) do
paths[#paths + 1] = tostring(file.url)
end
local output, err = Command("tag"):args(paths):stdout(Command.PIPED):output()
if not output then
return ya.err("Cannot spawn tag command, error code " .. tostring(err))
end
local i, tags = 1, {}
for line in output.stdout:gmatch("[^\r\n]+") do
if i > #paths then
break
end
tags[paths[i]] = tags[paths[i]] or {}
local joint = line:match("\t(.+)$") or ""
for s in joint:gmatch("[^,]+") do
table.insert(tags[paths[i]], s)
end
i = i + 1
end
update(tags)
return 1
end
local function entry(_, args)
assert(args[1] == "add" or args[1] == "remove", "Invalid action")
assert(args[2], "No tag specified")
local t = { args[1] == "remove" and "-r" or "-a", args[2] }
local files = {}
for _, url in ipairs(selected_or_hovered()) do
t[#t + 1] = tostring(url)
files[#files + 1] = { url = url }
end
local status = Command("tag"):args(t):status()
if status.success then
fetch { files = files }
end
end
return { setup = setup, fetch = fetch, entry = entry }