refactor: split into config/status/component modules

This commit is contained in:
Kristofers Solo 2025-05-21 11:55:50 +03:00
parent 0f0b8e52f2
commit ad96b304cf
Signed by: kristoferssolo
GPG Key ID: 74FF8144483D82C8
7 changed files with 103 additions and 53 deletions

View File

@ -3,3 +3,4 @@
"unused-local"
]
}

View File

@ -0,0 +1,32 @@
local status = require("lualine-harpoon.status")
local cfg = require("lualine-harpoon.config")
local req = require("lualine_require")
local Component = req.require("lualine.component")
local M = Component:extend()
function M:init(opts)
M.super.init(self, opts)
self.options = vim.tbl_deep_extend("force", cfg, self.options or {})
end
function M:update_status()
local st = status.get_status()
if st.total == 0 then
return ""
end
local s = self.options.symbol
local n = st.current and tostring(st.current) or s.unknown
return string.format("%s%s%s%d%s", s.open, n, s.separator, st.total, s.close)
end
function M:draw_status()
local text = self:update_status()
if self.color_hl and text ~= "" then
return self.color_hl(text)
end
return text
end
return M

View File

@ -0,0 +1,10 @@
local M = {
symbol = {
open = "[",
close = "]",
separator = "/",
unknown = "?",
},
}
return M

View File

@ -0,0 +1,9 @@
local component = require("lualine-harpoon.component")
local M = {
component = component,
}
function M.setup(_opts) end
return M

View File

@ -0,0 +1,30 @@
local ok, harpoon = pcall(require, "harpoon")
local utils = require("lualine-harpoon.utils")
local M = {}
--- Get current 1-based index and total Harpoon marks
---@return { current: integer?, total: integer }
function M.get_status()
if not ok then
return { current = nil, total = 0 }
end
local list = harpoon:list()
local total = list:length()
if total == 0 then
return { current = nil, total = 0 }
end
local root = list.config.get_root_dir()
local buf = vim.api.nvim_buf_get_name(0)
local rel_buf = utils.normalize(buf, root)
local _, idx = list:get_by_value(rel_buf)
if type(idx) ~= "number" or idx < 1 or idx > total then
idx = nil
end
return { current = idx, total = total }
end
return M

View File

@ -0,0 +1,16 @@
local ok_path, Path = pcall(require, "plenary.path")
local M = {}
--- Normalize `buf` relative to `root`, if plenary.path is available.
---@param buf string
---@param root string
---@return string
function M.normalize(buf, root)
if ok_path then
return Path:new(buf):make_relative(root)
else
return buf
end
end
return M

View File

@ -1,49 +1 @@
local ok, harpoon = pcall(require, "harpoon")
local Path = require("plenary.path")
local function normalize_path(buf_name, root)
return Path:new(buf_name):make_relative(root)
end
--- [TODO:description]
---@return table {current}
local function get_status()
if not ok then
return { current = nil, total = 0 }
end
local list = harpoon:list()
local total = list:length()
local bufname = normalize_path(vim.api.nvim_buf_get_name(0), list.config.get_root_dir())
local _, idx = list:get_by_value(bufname)
if type(idx) ~= "number" or idx < 1 or idx > total then
return { current = nil, total = total }
end
return { current = idx, total = total }
end
--- Draw function called by lualine
---@return string
local function draw()
local st = get_status()
if st.total == 0 then
return ""
end
local idx = st.current and tostring(st.current) or "?"
return string.format("[%s/%d]", idx, st.total)
end
--- Only show when there is at least one mark
---@return boolean
local function cond()
return get_status().total > 0
end
return function(opts)
return draw()
-- return {
-- draw = draw,
-- cond = opts.cond or cond,
-- color = opts.color or { fg = "#89b4fa", gui = "bold" },
-- }
end
return require("lualine-harpoon.component")