From c9d3b514fe0400e9a22c77a051c0aabd509835fe Mon Sep 17 00:00:00 2001 From: doryan Date: Wed, 8 Jan 2025 14:25:00 +0400 Subject: [PATCH] feat(yazi): delete sudo.yazi and add lazy.git, system-clipboard.yazi plugins --- .../DO_NOT_MODIFY_ANYTHING_IN_THIS_DIRECTORY | 0 yazi/plugins/lazygit.yazi/LICENSE | 21 ++ yazi/plugins/lazygit.yazi/README.md | 29 +++ yazi/plugins/lazygit.yazi/init.lua | 31 +++ yazi/plugins/sudo.yazi/init.lua | 196 ------------------ yazi/plugins/system-clipboard.yazi/init.lua | 54 +++++ 6 files changed, 135 insertions(+), 196 deletions(-) create mode 100644 yazi/plugins/lazygit.yazi/DO_NOT_MODIFY_ANYTHING_IN_THIS_DIRECTORY create mode 100644 yazi/plugins/lazygit.yazi/LICENSE create mode 100644 yazi/plugins/lazygit.yazi/README.md create mode 100644 yazi/plugins/lazygit.yazi/init.lua delete mode 100644 yazi/plugins/sudo.yazi/init.lua create mode 100644 yazi/plugins/system-clipboard.yazi/init.lua diff --git a/yazi/plugins/lazygit.yazi/DO_NOT_MODIFY_ANYTHING_IN_THIS_DIRECTORY b/yazi/plugins/lazygit.yazi/DO_NOT_MODIFY_ANYTHING_IN_THIS_DIRECTORY new file mode 100644 index 0000000..e69de29 diff --git a/yazi/plugins/lazygit.yazi/LICENSE b/yazi/plugins/lazygit.yazi/LICENSE new file mode 100644 index 0000000..ae1f60d --- /dev/null +++ b/yazi/plugins/lazygit.yazi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Darius + +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. diff --git a/yazi/plugins/lazygit.yazi/README.md b/yazi/plugins/lazygit.yazi/README.md new file mode 100644 index 0000000..e96c9c6 --- /dev/null +++ b/yazi/plugins/lazygit.yazi/README.md @@ -0,0 +1,29 @@ +# lazygit.yazi +Plugin for [Yazi](https://github.com/sxyazi/yazi) to manage git repos with [lazygit](https://github.com/jesseduffield/lazygit) +## Dependencies +Make sure [lazygit](https://github.com/jesseduffield/lazygit) is installed and in your `PATH`. +## Installation + +### Using `ya pack` +``` +ya pack -a Lil-Dank/lazygit +``` + +### Manual +**Linux/macOS** +``` +git clone https://github.com/Lil-Dank/lazygit.yazi.git ~/.config/yazi/plugins/lazygit.yazi +``` +**Windows** +``` +git clone https://github.com/Lil-Dank/lazygit.yazi.git %AppData%\yazi\config\plugins\lazygit.yazi +``` +## Configuration +add this to your **keymap.toml** file +```toml +[[manager.prepend_keymap]] +on = [ "g", "i" ] +run = "plugin lazygit" +desc = "run lazygit" +``` +you can customize the keybinding however you like. Please refer to the [keymap.toml](https://yazi-rs.github.io/docs/configuration/keymap) documentation diff --git a/yazi/plugins/lazygit.yazi/init.lua b/yazi/plugins/lazygit.yazi/init.lua new file mode 100644 index 0000000..a5925ea --- /dev/null +++ b/yazi/plugins/lazygit.yazi/init.lua @@ -0,0 +1,31 @@ +return { + entry = function() + local output = Command("git"):arg("status"):stderr(Command.PIPED):output() + if output.stderr ~= "" then + ya.notify({ + title = "lazygit", + content = "Not in a git directory", + level = "warn", + timeout = 5, + }) + else + permit = ya.hide() + local output, err_code = Command("lazygit"):stderr(Command.PIPED):output() + if err_code ~= nil then + ya.notify({ + title = "Failed to run lazygit command", + content = "Status: " .. err_code, + level = "error", + timeout = 5, + }) + elseif not output.status.success then + ya.notify({ + title = "lazygit in" .. cwd .. "failed, exit code " .. output.status.code, + content = output.stderr, + level = "error", + timeout = 5, + }) + end + end + end, +} \ No newline at end of file diff --git a/yazi/plugins/sudo.yazi/init.lua b/yazi/plugins/sudo.yazi/init.lua deleted file mode 100644 index 46f8f76..0000000 --- a/yazi/plugins/sudo.yazi/init.lua +++ /dev/null @@ -1,196 +0,0 @@ -local fs = os.getenv("HOME") .. "/.config/yazi/plugins/sudo.yazi/fs.nu" - -function string:ends_with_char(suffix) - return self:sub(-#suffix) == suffix -end - -function string:is_path() - local i = self:find("/") - return self == "." or self == ".." or i and i ~= #self -end - -local function list_map(self, f) - local i = nil - return function() - local v - i, v = next(self, i) - if v then - return f(v) - else - return nil - end - end -end - -local get_state = ya.sync(function(_, cmd) - if cmd == "paste" or cmd == "link" then - local yanked = {} - for _, url in pairs(cx.yanked) do - table.insert(yanked, tostring(url)) - end - - if #yanked == 0 then - return {} - end - - return { - kind = cmd, - value = { - is_cut = cx.yanked.is_cut, - yanked = yanked, - }, - } - elseif cmd == "create" then - return { kind = cmd } - elseif cmd == "remove" then - local selected = {} - - if #cx.active.selected ~= 0 then - for _, url in pairs(cx.active.selected) do - table.insert(selected, tostring(url)) - end - else - table.insert(selected, tostring(cx.active.current.hovered.url)) - end - - return { - kind = cmd, - value = { - selected = selected, - }, - } - elseif cmd == "rename" and #cx.active.selected == 0 then - return { - kind = cmd, - value = { - hovered = tostring(cx.active.current.hovered.url), - }, - } - else - return {} - end -end) - -local function sudo_cmd() - return { "sudo", "-k", "--" } -end - -local function extend_list(self, list) - for _, value in ipairs(list) do - table.insert(self, value) - end -end - -local function extend_iter(self, iter) - for item in iter do - table.insert(self, item) - end -end - -local function execute(command) - ya.manager_emit("shell", { - table.concat(command, " "), - block = true, - confirm = true, - }) -end - -local function sudo_paste(value) - local args = sudo_cmd() - - table.insert(args, fs) - if value.is_cut then - table.insert(args, "mv") - else - table.insert(args, "cp") - end - if value.force then - table.insert(args, "--force") - end - extend_iter(args, list_map(value.yanked, ya.quote)) - - execute(args) -end - -local function sudo_link(value) - local args = sudo_cmd() - - extend_list(args, { fs, "ln" }) - if value.relative then - table.insert(args, "--relative") - end - extend_iter(args, list_map(value.yanked, ya.quote)) - - execute(args) -end - -local function sudo_create() - local name, event = ya.input({ - title = "sudo create:", - position = { "top-center", y = 2, w = 40 }, - }) - - -- Input and confirm - if event == 1 and not name:is_path() then - local args = sudo_cmd() - - if name:ends_with_char("/") then - extend_list(args, { "mkdir", "-p" }) - else - table.insert(args, "touch") - end - table.insert(args, ya.quote(name)) - - execute(args) - end -end - -local function sudo_rename(value) - local new_name, event = ya.input({ - title = "sudo rename:", - position = { "top-center", y = 2, w = 40 }, - }) - - -- Input and confirm - if event == 1 and not new_name:is_path() then - local args = sudo_cmd() - extend_list(args, { "mv", ya.quote(value.hovered), ya.quote(new_name) }) - execute(args) - end -end - -local function sudo_remove(value) - local args = sudo_cmd() - - extend_list(args, { fs, "rm" }) - if value.is_permanent then - table.insert(args, "--permanent") - end - extend_iter(args, list_map(value.selected, ya.quote)) - - execute(args) -end - -return { - entry = function(_, args) - -- https://github.com/sxyazi/yazi/issues/1553#issuecomment-2309119135 - ya.manager_emit("escape", { visual = true }) - - local state = get_state(args[1]) - - if state.kind == "paste" then - state.value.force = args[2] == "-f" - sudo_paste(state.value) - elseif state.kind == "link" then - state.value.relative = args[2] == "-r" - sudo_link(state.value) - elseif state.kind == "create" then - sudo_create() - elseif state.kind == "remove" then - state.value.is_permanent = args[2] == "-P" - sudo_remove(state.value) - elseif state.kind == "rename" then - sudo_rename(state.value) - end - end, -} diff --git a/yazi/plugins/system-clipboard.yazi/init.lua b/yazi/plugins/system-clipboard.yazi/init.lua new file mode 100644 index 0000000..5ad36bc --- /dev/null +++ b/yazi/plugins/system-clipboard.yazi/init.lua @@ -0,0 +1,54 @@ +-- Meant to run at async context. (yazi system-clipboard) + +local selected_or_hovered = ya.sync(function() + local tab, paths = cx.active, {} + for _, u in pairs(tab.selected) do + paths[#paths + 1] = tostring(u) + end + if #paths == 0 and tab.current.hovered then + paths[1] = tostring(tab.current.hovered.url) + end + return paths +end) + +return { + entry = function() + ya.manager_emit("escape", { visual = true }) + + local urls = selected_or_hovered() + + if #urls == 0 then + return ya.notify({ title = "System Clipboard", content = "No file selected", level = "warn", timeout = 5 }) + end + + -- ya.notify({ title = #urls, content = table.concat(urls, " "), level = "info", timeout = 5 }) + + local status, err = + Command("cb") + :arg("copy") + :args(urls) + :spawn() + :wait() + + if status or status.succes then + ya.notify({ + title = "System Clipboard", + content = "Succesfully copied the file(s) to system clipboard", + level = "info", + timeout = 5, + }) + end + + if not status or not status.success then + ya.notify({ + title = "System Clipboard", + content = string.format( + "Could not copy selected file(s) %s", + status and status.code or err + ), + level = "error", + timeout = 5, + }) + end + end, +}