feat: output the stderr of sh execution to the preview pane to make debugging easier, closes #106

This commit is contained in:
sxyazi
2025-04-21 17:21:55 +08:00
parent 690e3092bc
commit 71f925e4b7
2 changed files with 12 additions and 8 deletions

View File

@@ -53,6 +53,8 @@ name = "*.csv"
run = 'piper -- bat -p --color=always "$1"' 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) ### Preview Markdown with [`glow`](https://github.com/charmbracelet/glow)
```toml ```toml

View File

@@ -4,7 +4,7 @@ local M = {}
local function fail(job, s) local function fail(job, s)
ya.preview_widgets(job, { 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 end
@@ -16,36 +16,38 @@ function M:peek(job)
}) })
:env("w", job.area.w) :env("w", job.area.w)
:env("h", job.area.h) :env("h", job.area.h)
:args({ "", tostring(job.file.url) }) :args({ "sh", tostring(job.file.url) })
:stdout(Command.PIPED) :stdout(Command.PIPED)
:stderr(Command.PIPED) :stderr(Command.PIPED)
:spawn() :spawn()
if not child then if not child then
return fail(job, err) return fail(job, "sh: " .. err)
end end
local limit = job.area.h local limit = job.area.h
local i, lines = 0, {} local i, outs, errs = 0, {}, {}
repeat repeat
local next, event = child:read_line() local next, event = child:read_line()
if event == 1 then if event == 1 then
return fail(job, "error occurred in stderr") errs[#errs + 1] = next
elseif event ~= 0 then elseif event ~= 0 then
break break
end end
i = i + 1 i = i + 1
if i > job.skip then if i > job.skip then
lines[#lines + 1] = next outs[#outs + 1] = next
end end
until i >= job.skip + limit until i >= job.skip + limit
child:start_kill() 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 }) ya.mgr_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true })
else else
ya.preview_widgets(job, { M.format(job, lines) }) ya.preview_widgets(job, { M.format(job, outs) })
end end
end end