Skip to content

Commit

Permalink
terminal/tools/yazi: 0.4.0 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Dec 10, 2024
1 parent 84a3d6f commit 30e9543
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 136 deletions.
10 changes: 5 additions & 5 deletions modules/home/programs/terminal/tools/yazi/configs/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ Status:children_add(function()
local formatted_created = nil
local formatted_modified = nil

if h.cha.created then
formatted_created = tostring(os.date("%Y-%m-%d %H:%M:%S", math.floor(h.cha.created)))
if h.cha.ctime then
formatted_created = tostring(os.date("%Y-%m-%d %H:%M:%S", math.floor(h.cha.ctime)))
end

if h.cha.modified then
formatted_modified = tostring(os.date("%Y-%m-%d %H:%M:%S", math.floor(h.cha.modified)))
if h.cha.mtime then
formatted_modified = tostring(os.date("%Y-%m-%d %H:%M:%S", math.floor(h.cha.mtime)))
end

if formatted_created and formatted_modified then
Expand All @@ -79,7 +79,7 @@ end, 400, Status.RIGHT)

function Linemode:custom()
local year = os.date("%Y")
local time = (self._file.cha.modified or 0) // 1
local time = (self._file.cha.mtime or 0) // 1

if time > 0 and os.date("%Y", time) == year then
time = os.date("%b %d %H:%M", time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@ return {

local urls = selected_or_hovered()
if #urls == 0 then
return ya.notify { title = "Chmod", content = "No file selected", level = "warn", timeout = 5 }
return ya.notify({ title = "Chmod", content = "No file selected", level = "warn", timeout = 5 })
end

local value, event = ya.input {
local value, event = ya.input({
title = "Chmod:",
position = { "top-center", y = 3, w = 40 },
}
})
if event ~= 1 then
return
end

local status, err = Command("chmod"):arg(value):args(urls):spawn():wait()
if not status or not status.success then
ya.notify {
ya.notify({
title = "Chmod",
content = string.format("Chmod with selected files failed, exit code %s", status and status.code or err),
content = string.format(
"Chmod with selected files failed, exit code %s",
status and status.code or err
),
level = "error",
timeout = 5,
}
})
end
end,
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local function info(content)
return ya.notify {
return ya.notify({
title = "Diff",
content = content,
timeout = 5,
}
})
end

local selected_url = ya.sync(function()
Expand All @@ -29,6 +29,8 @@ return {
local output, err = Command("diff"):arg("-Naur"):arg(tostring(a)):arg(tostring(b)):output()
if not output then
return info("Failed to run diff, error: " .. err)
elseif output.stdout == "" then
return info("No differences found")
end

ya.clipboard(output.stdout)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
-- TODO: remove this once v0.4 is released
local v4 = function(typ, area, ...)
if typ == "bar" then
return ui.Table and ui.Bar(...):area(area) or ui.Bar(area, ...)
else
return ui.Table and ui.Border(...):area(area) or ui.Border(area, ...)
end
end

local function setup(_, opts)
local type = opts and opts.type or ui.Border.ROUNDED
local old_build = Tab.build

Tab.build = function(self, ...)
local bar = function(c, x, y)
if x <= 0 or x == self._area.w - 1 then
return ui.Bar(ui.Rect.default, ui.Bar.TOP)
return v4("bar", ui.Rect.default, ui.Bar.TOP)
end

return ui.Bar(
return v4(
"bar",
ui.Rect({
x = x,
y = math.max(0, y),
Expand All @@ -28,9 +38,9 @@ local function setup(_, opts)

local style = THEME.manager.border_style
self._base = ya.list_merge(self._base or {}, {
ui.Border(self._area, ui.Border.ALL):type(type):style(style),
ui.Bar(self._chunks[1], ui.Bar.RIGHT):style(style),
ui.Bar(self._chunks[3], ui.Bar.LEFT):style(style),
v4("border", self._area, ui.Border.ALL):type(type):style(style),
v4("bar", self._chunks[1], ui.Bar.RIGHT):style(style),
v4("bar", self._chunks[3], ui.Bar.LEFT):style(style),

bar("", c[1].right - 1, c[1].y),
bar("", c[1].right - 1, c[1].bottom - 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function M:peek()
)
else
lines = lines:gsub("\t", string.rep(" ", PREVIEW.tab_size))
ya.preview_widgets(self, { ui.Paragraph.parse(self.area, lines) })
ya.preview_widgets(self, { ui.Text.parse(self.area, lines) })
end
end

Expand All @@ -66,7 +66,7 @@ function M:fallback_to_builtin()
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() }),
ui.Text(self.area, { ui.Line(err):reverse() }),
})
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- @sync entry

local function entry(st)
if st.old then
Tab.layout, st.old = st.old, nil
Expand All @@ -18,4 +20,8 @@ local function entry(st)
ya.app_emit("resize", {})
end

return { entry = entry }
local function enabled(st)
return st.old ~= nil
end

return { entry = entry, enabled = enabled }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ local changed = ya.sync(function(st, new)
return b or not cx.active.finder
end)

local escape = function(s) return s == "." and "\\." or s end
local escape = function(s)
return s == "." and "\\." or s
end

return {
entry = function()
Expand All @@ -15,7 +17,7 @@ return {
cands[#cands + 1] = { on = AVAILABLE_CHARS:sub(i, i) }
end

local idx = ya.which { cands = cands, silent = true }
local idx = ya.which({ cands = cands, silent = true })
if not idx then
return
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- @sync entry

local function entry(st)
if st.old then
Tab.layout, st.old = st.old, nil
Expand All @@ -17,4 +19,8 @@ local function entry(st)
ya.app_emit("resize", {})
end

return { entry = entry }
local function enabled(st)
return st.old ~= nil
end

return { entry = entry, enabled = enabled }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

local M = {}

function M:peek()
function M:peek(job)
local child = Command("mlr")
:args({
"--icsv",
Expand All @@ -16,60 +16,43 @@ function M:peek()
"--value-color",
"lightsteelblue1",
"cat",
tostring(self.file.url),
tostring(job.file.url),
})
:stdout(Command.PIPED)
:stderr(Command.PIPED)
:spawn()

if not child then
return self:fallback_to_builtin()
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 self:fallback_to_builtin()
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",
{ tostring(math.max(0, i - limit)), only_if = tostring(self.file.url), upper_bound = "" }
)
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.Paragraph.parse(self.area, lines) })
ya.preview_widgets(job, { ui.Text.parse(lines):area(job.area) })
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 _, bound = ya.preview_code(self)
if bound then
ya.manager_emit("peek", { tostring(bound), only_if = tostring(self.file.url), upper_bound = "" })
end
function M:seek(job)
require("code"):seek(job)
end

return M
Loading

0 comments on commit 30e9543

Please sign in to comment.