forked from mgumz/yazi-plugin-bat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
75 lines (63 loc) · 1.86 KB
/
Copy pathmain.lua
File metadata and controls
75 lines (63 loc) · 1.86 KB
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
--- @since 25.4.8
local M = {}
function M:peek(job)
-- Check if file exists
if not job.file then
return
end
local child = Command("bat")
:arg({
"--style",
"plain",
"--color",
"always",
tostring(job.file.url),
})
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:spawn()
local max_lines = job.area.h
local collected_lines = ""
local i = 0
local last_line = 0
local is_wrap = rt and rt.preview and rt.preview.wrap == "yes"
repeat
local next, event = child:read_line()
if event == 1 then
ya.err(tostring(event))
elseif event ~= 0 then
break
end
-- Process line
i = i + 1
if i > job.skip then
-- Add the line directly to our collected text without adding extra newlines
collected_lines = collected_lines .. next
last_line = last_line + 1
end
until last_line >= max_lines
child:start_kill()
-- Handle pagination and upper bounds
if job.skip > 0 and #collected_lines == 0 then
ya.emit("peek", { math.max(0, job.skip - max_lines), only_if = job.file.url, upper_bound = false })
return
end
-- Process tabs
local processed_text = collected_lines:gsub("\t", string.rep(" ", (rt and rt.preview or PREVIEW).tab_size))
-- Create text widget with proper wrapping
local widget = ui.Text.parse(processed_text):area(job.area)
if is_wrap then
widget = widget:wrap(ui.Text.WRAP)
end
ya.preview_widget(job, widget)
end
function M:seek(job)
local h = cx.active.current.hovered
if h and h.url == job.file.url then
ya.emit("peek", {
math.max(0, cx.active.preview.skip + job.units),
only_if = job.file.url,
})
end
end
return M