Skip to content

Commit

Permalink
Adding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
luxluth committed Mar 28, 2024
1 parent 22a258a commit 9019f48
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
15 changes: 13 additions & 2 deletions lua/oz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ local M = {}
M.opts = opts

---@param args Options?
-- you can define your setup function here. Usually configurations can be merged, accepting outside params and
-- you can also put some validation here for those.
function M.setup(args)
M.opts = vim.tbl_deep_extend("force", M.opts, args or {})
end
Expand Down Expand Up @@ -94,4 +92,17 @@ function M.restart_engine()
engine.restart(M)
end

----- LOGS
function M.open_log()
engine.openlogs()
end

function M.close_log()
engine.closelogs()
end

function M.toogle_log()
engine.tooglelogs()
end

return M
21 changes: 18 additions & 3 deletions lua/oz/engine.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local LogBuf = require("oz.log")

---@class OzEngine
local M = {}

Expand All @@ -18,11 +20,12 @@ local EC = {
---@param port string
function EC:spinsup_compiler(port)
local command = { "socat", "-", string.format("TCP:localhost:%s", port) }
-- vim.notify(string.format("TCP:localhost:%s", port), vim.log.levels.WARN, { title = "oz.nvim" })

self.compiler.pid = vim.fn.jobstart(command, {
---@param data string
on_stdout = function(_, data, _) end,
---@param data string[]
on_stdout = function(_, data, _)
LogBuf:push(data)
end,
on_stderr = function(_, data, _)
vim.notify(table.concat(data, "\n"), vim.log.levels.WARN, { title = "oz.nvim" })
end,
Expand Down Expand Up @@ -151,4 +154,16 @@ function M.restart(instance)
EC:start(instance)
end

function M.openlogs()
LogBuf:open_log()
end

function M.closelogs()
LogBuf:close_log()
end

function M.tooglelogs()
LogBuf:toogle_log()
end

return M
73 changes: 73 additions & 0 deletions lua/oz/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---@class LogBuffer
local LogBuf = {
---@type integer
nr = vim.api.nvim_create_buf(true, false),
---@type string[]
content = {},
---@type number
max_content_lines = 500,
win = {
open = false,
---@type number
id = nil,
},
}

function LogBuf:clear_buf()
vim.api.nvim_buf_set_lines(self.nr, 0, -1, false, {})
end

---@param lines string[]
function LogBuf:set_buf_lines(lines)
self:clear_buf()
vim.api.nvim_buf_set_lines(self.nr, 0, -1, false, lines)
end

---@param lines string[]
function LogBuf:push(lines)
local total_lines = #self.content + #lines
if total_lines <= self.max_content_lines then
for _, line in ipairs(lines) do
table.insert(self.content, line)
end
else
local excess_lines = total_lines - self.max_content_lines
for i = 1, excess_lines do
table.remove(self.content, 1)
end
for _, line in ipairs(lines) do
table.insert(self.content, line)
end
end

self:set_buf_lines(self.content)
end

function LogBuf:open_log()
if self.win.open ~= true then
self.win.id = vim.api.nvim_open_win(self.nr, false, {
split = "left",
title = "[oz.nvim::Logger]",
anchor = "SW",
})
self.win.open = true
end
end

function LogBuf:close_log()
if self.win.open ~= false then
vim.api.nvim_win_close(self.win.id, true)
self.win.id = nil
self.win.open = false
end
end

function LogBuf:toogle_log()
if self.win.open then
self:close_log()
else
self:open_log()
end
end

return LogBuf
12 changes: 12 additions & 0 deletions plugin/oz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ vim.api.nvim_create_autocmd({ "BufEnter" }, {
oz.restart_engine()
end, {})

vim.api.nvim_create_user_command("OzOpenLog", function()
oz.open_log()
end, {})

vim.api.nvim_create_user_command("OzCloseLog", function()
oz.open_log()
end, {})

vim.api.nvim_create_user_command("OzToggleLog", function()
oz.toogle_log()
end, {})

vim.keymap.set("v", oz.opts.keymaps.feed_selection_mapping, function()
oz.feed_selection(args.buf)
end, { desc = "Feed the current selection into the oz engine" })
Expand Down

0 comments on commit 9019f48

Please sign in to comment.