From f4265232bbfeef0095d765a1a85ed997d39b85bb Mon Sep 17 00:00:00 2001 From: mpaulson Date: Tue, 5 Dec 2023 19:18:06 -0700 Subject: [PATCH] small logger changes + spec --- lua/harpoon/logger.lua | 31 ++++++++++++++++++++++++++++--- lua/harpoon/test/logger_spec.lua | 22 ++++++++++++++++++++++ lua/harpoon/utils.lua | 18 ++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 lua/harpoon/test/logger_spec.lua diff --git a/lua/harpoon/logger.lua b/lua/harpoon/logger.lua index 6fd6f89..d2576bf 100644 --- a/lua/harpoon/logger.lua +++ b/lua/harpoon/logger.lua @@ -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() diff --git a/lua/harpoon/test/logger_spec.lua b/lua/harpoon/test/logger_spec.lua new file mode 100644 index 0000000..19189ff --- /dev/null +++ b/lua/harpoon/test/logger_spec.lua @@ -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) + diff --git a/lua/harpoon/utils.lua b/lua/harpoon/utils.lua index 5660f63..f99df98 100644 --- a/lua/harpoon/utils.lua +++ b/lua/harpoon/utils.lua @@ -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