MarkdownPreviewer/lua/MarkdownPreviewer/app.lua
2025-08-18 23:02:07 +02:00

89 lines
1.6 KiB
Lua

local msgpack = require"MarkdownPreviewer.msgpack"
chansend = vim.fn.chansend
module = {}
local function ToUint32(data)
local len = data
t = {}
for i=4,1,-1 do
t[i] = math.fmod(len, 256)
len = math.floor(len / 256)
end
return string.char(unpack(t))
end
local function Message(data)
local internal = msgpack.ToMsgPack(data)
return ToUint32(#internal) .. internal
end
app = {
cmd = nil,
channel = nil
}
function app:init(on_exit)
if self.channel then
return
end
local cwd = debug.getinfo(1, 'S').source:sub(2):match('(.*[/\\])')
self.channel = vim.fn.jobstart(self.cmd, {
cwd = cwd,
stderr_buffered = true,
on_exit = function()
vim.fn.chanclose(self.channel)
self.channel = nil
on_exit()
end,
on_stderr = function(channel, data, name)
print(vim.inspect(data))
end,
on_stdout = function(channel, data, name)
print(vim.inspect(data))
end
})
end
function app:show(content)
if self.channel == nil then
return
end
chansend(self.channel, Message({show = content}))
end
function app:scroll(content)
if self.channel == nil then
return
end
chansend(self.channel, Message({scroll = content}))
end
function app:stop()
if self.channel then
vim.fn.jobstop(self.channel)
end
end
function module.setup()
o = {}
setmetatable(o, app)
app.__index = app
o.cmd = {
"../../Server/run.sh",
"../../Server/venv",
'python3',
'../../Server/server.py'
}
return o
end
return module