Skip to content

Commit

Permalink
logger: max lines to prevent things taking too much memory
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Dec 6, 2023
1 parent f426523 commit d8fa264
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lua/harpoon/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local utils = require("harpoon.utils")

---@class HarpoonLog
---@field lines string[]
---@field max_lines number
---@field enabled boolean not used yet, but if we get reports of slow, we will use this
local HarpoonLog = {}

Expand All @@ -12,6 +13,7 @@ function HarpoonLog:new()
local logger = setmetatable({
lines = {},
enabled = true,
max_lines = 50,
}, self)

return logger
Expand Down Expand Up @@ -41,12 +43,17 @@ function HarpoonLog:log(...)
local split = utils.split(line, "\n")
for _, l in ipairs(split) do
if not utils.is_white_space(l) then
table.insert(lines, utils.trim(utils.remove_duplicate_whitespace(l)))
local ll = utils.trim(utils.remove_duplicate_whitespace(l))
table.insert(lines, ll)
end
end
end

table.insert(self.lines, table.concat(lines, " "))

while #self.lines > self.max_lines do
table.remove(self.lines, 1)
end
end

function HarpoonLog:clear()
Expand All @@ -55,7 +62,6 @@ end

function HarpoonLog:show()
local bufnr = vim.api.nvim_create_buf(false, true)
print(vim.inspect(self.lines))
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, self.lines)
vim.api.nvim_win_set_buf(0, bufnr)
end
Expand Down
8 changes: 8 additions & 0 deletions lua/harpoon/test/logger_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ describe("harpoon", function()
eq({ "{ hello = \"world\", world = \"hello\" }" }, Logger.lines)
end)

it("max lines", function()
Logger.max_lines = 1
Logger:log("one")
eq({ "one" }, Logger.lines)
Logger:log("two")
eq({ "two" }, Logger.lines)
end)

end)

0 comments on commit d8fa264

Please sign in to comment.