From 28e8bdf7f8286bd431b7f3b709e79f3827b31469 Mon Sep 17 00:00:00 2001 From: TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> Date: Wed, 24 Dec 2025 13:54:03 +0900 Subject: updates --- .../.config/yazi/plugins/sudo-demo.yazi/README.md | 25 ++++++++++++ .../.config/yazi/plugins/sudo-demo.yazi/main.lua | 45 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 debian/.config/yazi/plugins/sudo-demo.yazi/README.md create mode 100644 debian/.config/yazi/plugins/sudo-demo.yazi/main.lua (limited to 'debian/.config/yazi/plugins/sudo-demo.yazi') diff --git a/debian/.config/yazi/plugins/sudo-demo.yazi/README.md b/debian/.config/yazi/plugins/sudo-demo.yazi/README.md new file mode 100644 index 0000000..8068691 --- /dev/null +++ b/debian/.config/yazi/plugins/sudo-demo.yazi/README.md @@ -0,0 +1,25 @@ +# sudo-demo.yazi + +Just an example showing how to use `sudo` in a Yazi plugin, and the plugin itself doesn't offer any features beyond logging a message. + +## Installation + +```sh +ya pkg add yazi-rs/plugins:sudo-demo +``` + +## Usage + +Add this to your `~/.config/yazi/keymap.toml`: + +```toml +[[mgr.prepend_keymap]] +on = "" +run = "plugin sudo-demo" +``` + +Press Ctrl + t to run the plugin, you should [see a message in the log](https://yazi-rs.github.io/docs/plugins/overview#logging). + +## License + +This plugin is MIT-licensed. For more information check the [LICENSE](LICENSE) file. diff --git a/debian/.config/yazi/plugins/sudo-demo.yazi/main.lua b/debian/.config/yazi/plugins/sudo-demo.yazi/main.lua new file mode 100644 index 0000000..599afe4 --- /dev/null +++ b/debian/.config/yazi/plugins/sudo-demo.yazi/main.lua @@ -0,0 +1,45 @@ +--- @since 25.5.31 + +--- Verify if `sudo` is already authenticated +--- @return boolean +local function sudo_already() + local status = Command("sudo"):arg({ "--validate", "--non-interactive" }):status() + assert(status, "Failed to run `sudo --validate --non-interactive`") + return status.success +end + +--- Run a program with `sudo` privilege +--- @param program string +--- @param args table +--- @return Output|nil output +--- @return integer|nil err +--- nil: no error +--- 1: sudo failed +local function run_with_sudo(program, args) + local cmd = Command("sudo"):arg(program):arg(args) + if sudo_already() then + return cmd:output() + end + + local permit = ui.hide and ui.hide() or ya.hide() -- TODO: remove this + print(string.format("Sudo password required to run: `%s %s`", program, table.concat(args))) + local output = cmd:output() + permit:drop() + + if output.status.success or sudo_already() then + return output + end + return nil, 1 +end + +return { + entry = function() + local output = run_with_sudo("ls", { "-l" }) + if not output then + return ya.err("Failed to run `sudo ls -l`") + end + + ya.err("stdout", output.stdout) + ya.err("status.code", output.status.code) + end, +} -- cgit v1.2.3