Skip to content

Commit

Permalink
fix: crashes when selection indicator exceeds the visible area while …
Browse files Browse the repository at this point in the history
…a wrong `folder_offset` specified (#416)
  • Loading branch information
sxyazi authored Dec 1, 2023
1 parent d549831 commit 638a360
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
21 changes: 11 additions & 10 deletions yazi-plugin/preset/components/folder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,31 @@ function Folder:linemode(area)
end

function Folder:markers(area, markers)
if #markers == 0 then
if #markers == 0 or area.w * area.h == 0 then
return {}
end

local elements = {}
local append = function(last)
local p = ui.Bar(
local y = math.min(area.y + last[1], area.y + area.h) - 1
local bar = ui.Bar(
ui.Rect {
x = math.max(1, area.x) - 1,
y = area.y + last[1] - 1,
x = math.max(0, area.x - 1),
y = y,
w = 1,
h = 1 + last[2] - last[1],
h = 1 + math.min(last[2] - last[1], area.h - y),
},
ui.Position.LEFT
)

if last[3] == 1 then
p = p:style(THEME.manager.marker_copied)
bar = bar:style(THEME.manager.marker_copied)
elseif last[3] == 2 then
p = p:style(THEME.manager.marker_cut)
bar = bar:style(THEME.manager.marker_cut)
elseif last[3] == 3 then
p = p:style(THEME.manager.marker_selected)
bar = bar:style(THEME.manager.marker_selected)
end
elements[#elements + 1] = p
elements[#elements + 1] = bar
end

local last = { markers[1][1], markers[1][1], markers[1][2] } -- start, end, type
Expand Down Expand Up @@ -153,7 +154,7 @@ function Folder:current(area)
markers[#markers + 1] = { i, 3 }
end
end
return utils.flat { ui.List(area, items), self:linemode(area), table.unpack(self:markers(area, markers)) }
return utils.flat { ui.List(area, items), self:linemode(area), self:markers(area, markers) }
end

function Folder:preview(area)
Expand Down
10 changes: 10 additions & 0 deletions yazi-plugin/preset/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ table.unpack = table.unpack or unpack

utils = utils or {}

function utils.clamp(min, x, max)
if x < min then
return min
elseif x > max then
return max
else
return x
end
end

function utils.flat(t)
local r = {}
for _, v in ipairs(t) do
Expand Down

0 comments on commit 638a360

Please sign in to comment.