Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Nov 28, 2024
1 parent 73d7969 commit 1bb7bff
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 109 deletions.
22 changes: 11 additions & 11 deletions yazi-plugin/preset/plugins/archive.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local M = {}

function M:peek()
local limit = self.area.h
function M:peek(job)
local limit = job.area.h
local paths, sizes = {}, {}

local files, bound, code = self.list_files({ "-p", tostring(self.file.url) }, self.skip, limit)
local files, bound, code = self.list_files({ "-p", tostring(job.file.url) }, job.skip, limit)
if code ~= 0 then
return ya.preview_widgets(self, {
return ya.preview_widgets(job, {
ui.Text(
code == 2 and "File list in this archive is encrypted"
or "Failed to start both `7z` and `7zz`. Do you have 7-zip installed?"
):area(self.area),
):area(job.area),
})
end

Expand All @@ -33,17 +33,17 @@ function M:peek()
end
end

if self.skip > 0 and bound < self.skip + limit then
ya.manager_emit("peek", { math.max(0, bound - limit), only_if = self.file.url, upper_bound = true })
if job.skip > 0 and bound < job.skip + limit then
ya.manager_emit("peek", { math.max(0, bound - limit), only_if = job.file.url, upper_bound = true })
else
ya.preview_widgets(self, {
ui.Text(paths):area(self.area),
ui.Text(sizes):area(self.area):align(ui.Text.RIGHT),
ya.preview_widgets(job, {
ui.Text(paths):area(job.area),
ui.Text(sizes):area(job.area):align(ui.Text.RIGHT),
})
end
end

function M:seek(units) require("code").seek(self, units) end
function M:seek(job) require("code"):seek(job) end

function M.spawn_7z(args)
local last_err = nil
Expand Down
16 changes: 8 additions & 8 deletions yazi-plugin/preset/plugins/code.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
local M = {}

function M:peek()
local err, bound = ya.preview_code(self)
function M:peek(job)
local err, bound = ya.preview_code(job)
if bound then
ya.manager_emit("peek", { bound, only_if = self.file.url, upper_bound = true })
ya.manager_emit("peek", { bound, only_if = job.file.url, upper_bound = true })
elseif err and not err:find("cancelled", 1, true) then
ya.preview_widgets(self, {
ui.Text(err):area(self.area):reverse(),
ya.preview_widgets(job, {
ui.Text(err):area(job.area):reverse(),
})
end
end

function M:seek(units)
local h = cx.active.current.hovered
if not h or h.url ~= self.file.url then
if not h or h.url ~= job.file.url then
return
end

local step = math.floor(units * self.area.h / 10)
local step = math.floor(units * job.area.h / 10)
step = step == 0 and ya.clamp(-1, units, 1) or step

ya.manager_emit("peek", {
math.max(0, cx.active.preview.skip + step),
only_if = self.file.url,
only_if = job.file.url,
})
end

Expand Down
24 changes: 12 additions & 12 deletions yazi-plugin/preset/plugins/empty.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
local M = {}

function M:msg(s) ya.preview_widgets(self, { ui.Text(s):area(self.area):reverse():wrap(ui.Text.WRAP) }) end
function M:msg(job, s) ya.preview_widgets(self, { ui.Text(s):area(job.area):reverse():wrap(ui.Text.WRAP) }) end

function M:peek()
local path = tostring(self.file.url)
function M:peek(job)
local path = tostring(job.file.url)
if path:sub(1, 6) ~= "/proc/" then
return self:msg("Empty file")
return self:msg(job, "Empty file")
end

local limit = self.area.h
local limit = job.area.h
local i, lines = 0, {}
local ok, err = pcall(function()
for line in io.lines(path) do
i = i + 1
if i > self.skip + limit then
if i > job.skip + limit then
break
elseif i > self.skip then
elseif i > job.skip then
lines[#lines + 1] = line
end
end
end)

if not ok then
self:msg(err)
elseif 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 })
self:msg(job, err)
elseif job.skip > 0 and i < job.skip + limit then
ya.manager_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true })
else
ya.preview_widgets(self, { ui.Text(lines):area(self.area) })
ya.preview_widgets(job, { ui.Text(lines):area(job.area) })
end
end

function M:seek(units) require("code").seek(self, units) end
function M:seek(job) require("code"):seek(job) end

return M
6 changes: 3 additions & 3 deletions yazi-plugin/preset/plugins/file.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local M = {}

function M:peek()
function M:peek(job)
local cmd = os.getenv("YAZI_FILE_ONE") or "file"
local output, err = Command(cmd):args({ "-bL", "--", tostring(self.file.url) }):stdout(Command.PIPED):output()
local output, err = Command(cmd):args({ "-bL", "--", tostring(job.file.url) }):stdout(Command.PIPED):output()

local text
if output then
Expand All @@ -11,7 +11,7 @@ function M:peek()
text = ui.Text(string.format("Failed to start `%s`, error: %s", cmd, err))
end

ya.preview_widgets(self, { text:area(self.area):wrap(ui.Text.WRAP) })
ya.preview_widgets(job, { text:area(job.area):wrap(ui.Text.WRAP) })
end

function M:seek() end
Expand Down
30 changes: 15 additions & 15 deletions yazi-plugin/preset/plugins/folder.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
local M = {}

function M:peek()
function M:peek(job)
local folder = cx.active.preview.folder
if not folder or folder.cwd ~= self.file.url then
if not folder or folder.cwd ~= job.file.url then
return
end

local bound = math.max(0, #folder.files - self.area.h)
if self.skip > bound then
return ya.manager_emit("peek", { bound, only_if = self.file.url, upper_bound = true })
local bound = math.max(0, #folder.files - job.area.h)
if job.skip > bound then
return ya.manager_emit("peek", { bound, only_if = job.file.url, upper_bound = true })
end

if #folder.files == 0 then
return ya.preview_widgets(self, {
ui.Text(folder.stage.is_loading and "Loading..." or "No items"):area(self.area):align(ui.Text.CENTER),
return ya.preview_widgets(job, {
ui.Text(folder.stage.is_loading and "Loading..." or "No items"):area(job.area):align(ui.Text.CENTER),
})
end

Expand All @@ -22,20 +22,20 @@ function M:peek()
entities[#entities + 1] = Entity:new(f):redraw()
end

ya.preview_widgets(self, {
ui.List(entities):area(self.area),
table.unpack(Marker:new(self.area, folder):redraw()),
ya.preview_widgets(job, {
ui.List(entities):area(job.area),
table.unpack(Marker:new(job.area, folder):redraw()),
})
end

function M:seek(units)
function M:seek(job)
local folder = cx.active.preview.folder
if folder and folder.cwd == self.file.url then
local step = math.floor(units * self.area.h / 10)
local bound = math.max(0, #folder.files - self.area.h)
if folder and folder.cwd == job.file.url then
local step = math.floor(job.units * job.area.h / 10)
local bound = math.max(0, #folder.files - job.area.h)
ya.manager_emit("peek", {
ya.clamp(0, cx.active.preview.skip + step, bound),
only_if = self.file.url,
only_if = job.file.url,
})
end
end
Expand Down
16 changes: 8 additions & 8 deletions yazi-plugin/preset/plugins/font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ local TEXT = "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n123456

local M = {}

function M:peek()
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
function M:peek(job)
local start, cache = os.clock(), ya.file_cache(job)
if not cache or job:preload() ~= 1 then
return
end

ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
ya.image_show(cache, job.area)
ya.preview_widgets(job, {})
end

function M:seek() end

function M:preload()
local cache = ya.file_cache(self)
function M:preload(job)
local cache = ya.file_cache(job)
if not cache or fs.cha(cache) then
return 1
end
Expand All @@ -27,7 +27,7 @@ function M:preload()
"-gravity",
"center",
"-font",
tostring(self.file.url),
tostring(job.file.url),
"-pointsize",
"64",
"xc:white",
Expand Down
16 changes: 8 additions & 8 deletions yazi-plugin/preset/plugins/image.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
local M = {}

function M:peek()
local start, url = os.clock(), ya.file_cache(self)
function M:peek(job)
local start, url = os.clock(), ya.file_cache(job)
if not url or not fs.cha(url) then
url = self.file.url
url = job.file.url
end

ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(url, self.area)
ya.preview_widgets(self, {})
ya.image_show(url, job.area)
ya.preview_widgets(job, {})
end

function M:seek() end

function M:preload()
local cache = ya.file_cache(self)
function M:preload(job)
local cache = ya.file_cache(job)
if not cache or fs.cha(cache) then
return 1
end

return ya.image_precache(self.file.url, cache) and 1 or 2
return ya.image_precache(job.file.url, cache) and 1 or 2
end

function M:spot(job)
Expand Down
22 changes: 11 additions & 11 deletions yazi-plugin/preset/plugins/json.lua
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
local M = {}

function M:peek()
function M:peek(job)
local child = Command("jq")
:args({
"-C",
"--tab",
".",
tostring(self.file.url),
tostring(job.file.url),
})
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:spawn()

if not child then
return require("code").peek(self)
return require("code"):peek(job)
end

local limit = self.area.h
local limit = job.area.h
local i, lines = 0, ""
repeat
local next, event = child:read_line()
if event == 1 then
return require("code").peek(self)
return require("code"):peek(job)
elseif event ~= 0 then
break
end

i = i + 1
if i > self.skip then
if i > job.skip then
lines = lines .. next
end
until i >= self.skip + limit
until i >= job.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 })
if job.skip > 0 and i < job.skip + limit then
ya.manager_emit("peek", { math.max(0, i - limit), only_if = job.file.url, upper_bound = true })
else
lines = lines:gsub("\t", string.rep(" ", PREVIEW.tab_size))
ya.preview_widgets(self, { ui.Text.parse(lines):area(self.area) })
ya.preview_widgets(job, { ui.Text.parse(lines):area(job.area) })
end
end

function M:seek(units) require("code").seek(self, units) end
function M:seek(job) require("code"):seek(job) end

return M
16 changes: 8 additions & 8 deletions yazi-plugin/preset/plugins/magick.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
local M = {}

function M:peek()
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
function M:peek(job)
local start, cache = os.clock(), ya.file_cache(job)
if not cache or self:preload(job) ~= 1 then
return
end

ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
ya.image_show(cache, job.area)
ya.preview_widgets(job, {})
end

function M:seek() end

function M:preload()
local cache = ya.file_cache(self)
function M:preload(job)
local cache = ya.file_cache(job)
if not cache or fs.cha(cache) then
return 1
end

local status, err = Command("magick"):args({
"-density",
"200",
tostring(self.file.url),
tostring(job.file.url),
"-flatten",
"-resize",
string.format("%dx%d^", PREVIEW.max_width, PREVIEW.max_height),
Expand Down
Loading

0 comments on commit 1bb7bff

Please sign in to comment.