From 71f925e4b7d6d2fa3e1e7822422d755ea709eb69 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 21 Apr 2025 17:21:55 +0800 Subject: [PATCH] feat: output the `stderr` of `sh` execution to the preview pane to make debugging easier, closes #106 --- piper.yazi/README.md | 2 ++ piper.yazi/main.lua | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/piper.yazi/README.md b/piper.yazi/README.md index c86c278..63b66e0 100644 --- a/piper.yazi/README.md +++ b/piper.yazi/README.md @@ -53,6 +53,8 @@ name = "*.csv" run = 'piper -- bat -p --color=always "$1"' ``` +Note that certain distributions might use a different name for `bat`, like Debian and Ubuntu uses `batcat` instead, so please adjust accordingly. + ### Preview Markdown with [`glow`](https://github.com/charmbracelet/glow) ```toml diff --git a/piper.yazi/main.lua b/piper.yazi/main.lua index 22d26fb..409f313 100644 --- a/piper.yazi/main.lua +++ b/piper.yazi/main.lua @@ -4,7 +4,7 @@ local M = {} local function fail(job, s) ya.preview_widgets(job, { - ui.Text(string.format("`piper` plugin error: %s", s)):area(job.area):wrap(ui.Text.WRAP), + ui.Text.parse(s):area(job.area):wrap(ui.Text.WRAP), }) end @@ -16,36 +16,38 @@ function M:peek(job) }) :env("w", job.area.w) :env("h", job.area.h) - :args({ "", tostring(job.file.url) }) + :args({ "sh", tostring(job.file.url) }) :stdout(Command.PIPED) :stderr(Command.PIPED) :spawn() if not child then - return fail(job, err) + return fail(job, "sh: " .. err) end local limit = job.area.h - local i, lines = 0, {} + local i, outs, errs = 0, {}, {} repeat local next, event = child:read_line() if event == 1 then - return fail(job, "error occurred in stderr") + errs[#errs + 1] = next elseif event ~= 0 then break end i = i + 1 if i > job.skip then - lines[#lines + 1] = next + outs[#outs + 1] = next end until i >= job.skip + limit child:start_kill() - if job.skip > 0 and i < job.skip + limit then + if #errs > 0 then + fail(job, table.concat(errs, "")) + elseif 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 - ya.preview_widgets(job, { M.format(job, lines) }) + ya.preview_widgets(job, { M.format(job, outs) }) end end