small logger changes + spec

This commit is contained in:
mpaulson 2023-12-05 19:18:06 -07:00
parent e9d18fca95
commit f4265232bb
3 changed files with 68 additions and 3 deletions

View File

@ -1,5 +1,8 @@
local utils = require("harpoon.utils")
---@class HarpoonLog
---@field lines string[]
---@field enabled boolean not used yet, but if we get reports of slow, we will use this
local HarpoonLog = {}
HarpoonLog.__index = HarpoonLog
@ -8,20 +11,42 @@ HarpoonLog.__index = HarpoonLog
function HarpoonLog:new()
local logger = setmetatable({
lines = {},
enabled = true,
}, self)
return logger
end
function HarpoonLog:disable()
self.enabled = false
end
function HarpoonLog:enable()
self.enabled = true
end
---@vararg any
function HarpoonLog:log(...)
local msg = {}
local processed = {}
for i = 1, select("#", ...) do
local item = select(i, ...)
table.insert(msg, vim.inspect(item))
if type(item) == "table" then
item = vim.inspect(item)
end
table.insert(processed, item)
end
table.insert(self.lines, table.concat(msg, " "))
local lines = {}
for _, line in ipairs(processed) do
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)))
end
end
end
table.insert(self.lines, table.concat(lines, " "))
end
function HarpoonLog:clear()

View File

@ -0,0 +1,22 @@
local utils = require("harpoon.test.utils")
local Logger = require("harpoon.logger")
local eq = assert.are.same
describe("harpoon", function()
before_each(function()
Logger:clear()
end)
it("new lines are removed. every log call is one line", function()
Logger:log("hello\nworld")
eq(Logger.lines, { "hello world" })
end)
it("new lines with vim.inspect get removed too", function()
Logger:log({hello = "world", world = "hello"})
eq({ "{ hello = \"world\", world = \"hello\" }" }, Logger.lines)
end)
end)

View File

@ -1,5 +1,23 @@
local M = {}
function M.trim(str)
return str:gsub("^%s+", ""):gsub("%s+$", "")
end
function M.remove_duplicate_whitespace(str)
return str:gsub("%s+", " ")
end
function M.split(str, sep)
if sep == nil then
sep = "%s"
end
local t={}
for s in string.gmatch(str, "([^"..sep.."]+)") do
table.insert(t, s)
end
return t
end
function M.is_white_space(str)
return str:gsub("%s", "") == ""
end