diff --git a/lsar.yazi/LICENSE b/lsar.yazi/LICENSE new file mode 100644 index 0000000..fb5b1d6 --- /dev/null +++ b/lsar.yazi/LICENSE @@ -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. diff --git a/lsar.yazi/README.md b/lsar.yazi/README.md new file mode 100644 index 0000000..03527ec --- /dev/null +++ b/lsar.yazi/README.md @@ -0,0 +1,39 @@ +# lsar.yazi + +Previewing archive contents with `lsar`, which is something you might not want to use anyway. + +It was the default archive previewer before Yazi v0.3, and after then, it was replaced with a faster and more efficient `7zip` previewer. + +This plugin is here just in case you're still interested in the old behavior, +but we strongly discourage using it unless you encounter some issues with `7zip` when previewing CJK characters - `lsar` usually does a better job recognizing these characters. + +## Installation + +```sh +ya pack -a yazi-rs/plugins#lsar +``` + +## Usage + +Add this to your `~/.config/yazi/yazi.toml`: + +```toml +[[plugin.prepend_previewers]] +mime = "application/{,g}zip" +run = "lsar" + +[[plugin.prepend_previewers]] +mime = "application/x-{tar,bzip*,7z-compressed,xz,rar}" +run = "lsar" +``` + +Make sure you have `unar` installed, and have `lsar` in your `$PATH`. You can install it with: + +```sh +# Arch Linux +sudo pacman -S unarchiver +# MacOS +brew install unar +# Windows +scoop install unar +``` diff --git a/lsar.yazi/init.lua b/lsar.yazi/init.lua new file mode 100644 index 0000000..1f70e4c --- /dev/null +++ b/lsar.yazi/init.lua @@ -0,0 +1,54 @@ +local M = {} + +function M:peek() + local child, code = Command("lsar"):arg(tostring(self.file.url)):stdout(Command.PIPED):spawn() + if not child then + return ya.err("spawn `lsar` command returns " .. tostring(code)) + end + + -- Skip the first line which is the archive file itself + while true do + local _, event = child:read_line() + if event == 0 or event ~= 1 then + break + end + end + + local limit = self.area.h + local i, lines = 0, {} + repeat + local next, event = child:read_line() + if event == 1 then + goto continue + elseif event ~= 0 then + break + end + + i = i + 1 + if i > self.skip then + lines[#lines + 1] = ui.Line(next) + end + + ::continue:: + until i >= self.skip + limit + + child:start_kill() + if self.skip > 0 and i < self.skip + limit then + ya.manager_emit("peek", { math.max(0, i - limit), only_if = self.file.url, upper_bound = true }) + else + ya.preview_widgets(self, { ui.Paragraph(self.area, lines) }) + end +end + +function M:seek(units) + local h = cx.active.current.hovered + if h and h.url == self.file.url then + local step = math.floor(units * self.area.h / 10) + ya.manager_emit("peek", { + math.max(0, cx.active.preview.skip + step), + only_if = self.file.url, + }) + end +end + +return M