-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
75 lines (67 loc) · 1.63 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local M = {}
function M:peek()
width = tonumber(self.area.w)
local child = Command("rich")
:args({
"-j",
"--left",
"--line-numbers",
"--force-terminal",
"--panel=rounded",
"--guides",
"--max-width",
tostring(self.area.w),
tostring(self.file.url),
})
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:spawn()
if not child then
return self:fallback_to_builtin()
end
local limit = self.area.h
local i, lines = 0, ""
repeat
local next, event = child:read_line()
if event == 1 then
return self:fallback_to_builtin()
elseif event ~= 0 then
break
end
i = i + 1
if i > self.skip then
lines = lines .. next
end
until i >= self.skip + limit
child:start_kill()
if self.skip > 0 and i < self.skip + limit then
ya.manager_emit(
"peek",
{ tostring(math.max(0, i - limit)), only_if = tostring(self.file.url), upper_bound = "" }
)
else
lines = lines:gsub("\t", string.rep(" ", PREVIEW.tab_size))
ya.preview_widgets(self, { ui.Paragraph.parse(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", {
tostring(math.max(0, cx.active.preview.skip + step)),
only_if = tostring(self.file.url),
})
end
end
function M:fallback_to_builtin()
local err, bound = ya.preview_code(self)
if bound then
ya.manager_emit("peek", { bound, only_if = self.file.url, upper_bound = true })
elseif err and not err:find("cancelled", 1, true) then
ya.preview_widgets(self, {
ui.Paragraph(self.area, { ui.Line(err):reverse() }),
})
end
end
return M