Skip to content

Commit

Permalink
WIP: work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mikavilpas committed Jul 6, 2024
1 parent 9fb48fa commit 95fbb19
Showing 1 changed file with 86 additions and 43 deletions.
129 changes: 86 additions & 43 deletions lua/yazi/yazi_process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,89 @@
local Log = require('yazi.log')
local utils = require('yazi.utils')

-- Represents a yazi process that has been started.
---@class YaziProcess
---@class YaProcess
---@field public events YaziEvent[] "The events that have been received from yazi"
---@field private ya_process vim.SystemObj
---@field public yazi_job_id integer
local YaziProcess = {}
---@field private retries integer
local YaProcess = {}
YaProcess.__index = YaProcess

---@diagnostic disable-next-line: inject-field
YaziProcess.__index = YaziProcess
function YaProcess:new()
self.events = {}
self.retries = 0

-- Starts a new yazi process. Uses `ya` to read events from yazi.
---@param config YaziConfig
---@param path Path
---@param on_exit fun(code: integer, selected_files: string[], events: YaziEvent[])
function YaziProcess:start(config, path, on_exit)
os.remove(config.chosen_file_path)
local yazi_cmd = string.format(
'yazi %s --chooser-file "%s"',
vim.fn.shellescape(path.filename),
config.chosen_file_path
)

---@type YaziEvent[]
local events = {}

Log:debug(string.format('Opening yazi with the command: (%s)', yazi_cmd))
self.yazi_job_id = vim.fn.termopen(yazi_cmd, {
---@diagnostic disable-next-line: unused-local
on_exit = function(_job_id, code, _event)
pcall(self.ya_process.kill, self.ya_process, 'sigterm')
return self
end

self.ya_process:wait(1000)
function YaProcess:kill()
Log:debug('Killing ya process')
pcall(self.ya_process.kill, self.ya_process, 'sigterm')
end

local chosen_files = {}
if utils.file_exists(config.chosen_file_path) == true then
chosen_files = vim.fn.readfile(config.chosen_file_path)
end
on_exit(code, chosen_files, events)
end,
})
function YaProcess:wait(timeout)
Log:debug('Waiting for ya process to exit')
self.ya_process:wait(timeout)
end

function YaProcess:start()
local ya_command = { 'ya', 'sub', 'rename,delete,trash,move,cd' }
Log:debug(
string.format(
'Opening ya with the command: (%s)',
table.concat(ya_command, ' ')
)
)

self.ya_process = vim.system(ya_command, {
-- • text: (boolean) Handle stdout and stderr as text.
-- Replaces `\r\n` with `\n`.
text = true,
stderr = function(err, data)
if err then
Log:debug(string.format("ya stderr error: '%s'", data))
end

if data == nil then
-- weird event, ignore
return
end
if err then
Log:debug(string.format("ya stderr error: '%s'", data))
end

Log:debug(string.format("ya stderr: '%s'", data))

if data:find('No running Yazi instance found') then
if self.retries < 5 then
Log:debug(
'Looks like starting ya failed because yazi had not started yet. Retrying to open ya...'
)
self.retries = self.retries + 1
vim.defer_fn(function()
self:start()
end, 50)
else
Log:debug('Failed to open ya after 5 retries')
end
end
end,

stdout = function(err, data)
if err then
Log:debug(string.format("ya stdout error: '%s'", data))
end

if data == nil then
-- weird event, ignore
return
end

if err then
Log:debug(string.format("ya stdout error: '%s'", data))
end

-- remove final newline character becauze it's annoying in the logs
-- remove the final newline character becauze it's annoying in the logs
if data:sub(-1) == '\n' then
data = data:sub(1, -2)
end

Log:debug(string.format("ya stdout: '%s'", data))
local parsed = utils.safe_parse_events({ data })
for _, event in ipairs(parsed) do
events[#events + 1] = event
self.events[#self.events + 1] = event
end
end,

Expand All @@ -97,4 +98,46 @@ function YaziProcess:start(config, path, on_exit)
return self
end

-- Represents a yazi process that has been started.
---@class YaziProcess
---@field private ya_process YaProcess
---@field public yazi_job_id integer
local YaziProcess = {}

---@diagnostic disable-next-line: inject-field
YaziProcess.__index = YaziProcess

-- Starts a new yazi process. Uses `ya` to read events from yazi.
---@param config YaziConfig
---@param path Path
---@param on_exit fun(code: integer, selected_files: string[], events: YaziEvent[])
function YaziProcess:start(config, path, on_exit)
os.remove(config.chosen_file_path)
local yazi_cmd = string.format(
'yazi %s --chooser-file "%s"',
vim.fn.shellescape(path.filename),
config.chosen_file_path
)

Log:debug(string.format('Opening yazi with the command: (%s)', yazi_cmd))
self.yazi_job_id = vim.fn.termopen(yazi_cmd, {
---@diagnostic disable-next-line: unused-local
on_exit = function(_job_id, code, _event)
self.ya_process:kill()
self.ya_process:wait(1000)

local chosen_files = {}
if utils.file_exists(config.chosen_file_path) == true then
chosen_files = vim.fn.readfile(config.chosen_file_path)
end
on_exit(code, chosen_files, self.ya_process.events)
end,
})

self.ya_process = YaProcess:new()
self.ya_process:start()

return self
end

return YaziProcess

0 comments on commit 95fbb19

Please sign in to comment.