mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-16 11:00:23 +00:00
small logger changes + spec
This commit is contained in:
parent
e9d18fca95
commit
f4265232bb
@ -1,5 +1,8 @@
|
|||||||
|
local utils = require("harpoon.utils")
|
||||||
|
|
||||||
---@class HarpoonLog
|
---@class HarpoonLog
|
||||||
---@field lines string[]
|
---@field lines string[]
|
||||||
|
---@field enabled boolean not used yet, but if we get reports of slow, we will use this
|
||||||
local HarpoonLog = {}
|
local HarpoonLog = {}
|
||||||
|
|
||||||
HarpoonLog.__index = HarpoonLog
|
HarpoonLog.__index = HarpoonLog
|
||||||
@ -8,20 +11,42 @@ HarpoonLog.__index = HarpoonLog
|
|||||||
function HarpoonLog:new()
|
function HarpoonLog:new()
|
||||||
local logger = setmetatable({
|
local logger = setmetatable({
|
||||||
lines = {},
|
lines = {},
|
||||||
|
enabled = true,
|
||||||
}, self)
|
}, self)
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function HarpoonLog:disable()
|
||||||
|
self.enabled = false
|
||||||
|
end
|
||||||
|
|
||||||
|
function HarpoonLog:enable()
|
||||||
|
self.enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
---@vararg any
|
---@vararg any
|
||||||
function HarpoonLog:log(...)
|
function HarpoonLog:log(...)
|
||||||
local msg = {}
|
local processed = {}
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
local item = select(i, ...)
|
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
|
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
|
end
|
||||||
|
|
||||||
function HarpoonLog:clear()
|
function HarpoonLog:clear()
|
||||||
|
22
lua/harpoon/test/logger_spec.lua
Normal file
22
lua/harpoon/test/logger_spec.lua
Normal 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)
|
||||||
|
|
@ -1,5 +1,23 @@
|
|||||||
local M = {}
|
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)
|
function M.is_white_space(str)
|
||||||
return str:gsub("%s", "") == ""
|
return str:gsub("%s", "") == ""
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user