From b12a9ab085a8c2fe2b921e1547ee667b714185f9 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 13 Apr 2025 09:16:41 +0800 Subject: [PATCH] docs: new example for previewing tarballs with `tar` --- piper.yazi/README.md | 10 ++++++++++ piper.yazi/main.lua | 31 +++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/piper.yazi/README.md b/piper.yazi/README.md index 13e008e..c86c278 100644 --- a/piper.yazi/README.md +++ b/piper.yazi/README.md @@ -35,6 +35,16 @@ Available variables: Here are some configuration examples: +### Preview tarballs with [`tar`](https://man7.org/linux/man-pages/man1/tar.1.html) + +```toml +[[plugin.prepend_previewers]] +name = "*.tar*" +run = 'piper --format=url -- tar tf "$1"' +``` + +In this example, `--format=url` tells `piper` to parse the `tar` output as file URLs, so you'll be able to get a list of files with icons. + ### Preview CSV with [`bat`](https://github.com/sharkdp/bat) ```toml diff --git a/piper.yazi/main.lua b/piper.yazi/main.lua index dd85ba4..22d26fb 100644 --- a/piper.yazi/main.lua +++ b/piper.yazi/main.lua @@ -26,7 +26,7 @@ function M:peek(job) end local limit = job.area.h - local i, lines = 0, "" + local i, lines = 0, {} repeat local next, event = child:read_line() if event == 1 then @@ -37,7 +37,7 @@ function M:peek(job) i = i + 1 if i > job.skip then - lines = lines .. next + lines[#lines + 1] = next end until i >= job.skip + limit @@ -45,13 +45,32 @@ function M:peek(job) if job.skip > 0 and i < job.skip + limit then ya.mgr_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true }) else - lines = lines:gsub("\t", string.rep(" ", rt.preview.tab_size)) - ya.preview_widgets(job, { - ui.Text.parse(lines):area(job.area), - }) + ya.preview_widgets(job, { M.format(job, lines) }) end end function M:seek(job) require("code"):seek(job) end +function M.format(job, lines) + local format = job.args.format + if format ~= "url" then + local s = table.concat(lines, ""):gsub("\t", string.rep(" ", rt.preview.tab_size)) + return ui.Text.parse(s):area(job.area) + end + + for i = 1, #lines do + lines[i] = lines[i]:gsub("[\r\n]+$", "") + + local icon = File({ + url = Url(lines[i]), + cha = Cha { kind = lines[i]:sub(-1) == "/" and 1 or 0 }, + }):icon() + + if icon then + lines[i] = ui.Line { ui.Span(" " .. icon.text .. " "):style(icon.style), lines[i] } + end + end + return ui.Text(lines):area(job.area) +end + return M