2021-09-20 11:46:38 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
|
|
|
|
2022-02-20 08:52:29 +00:00
|
|
|
assert = require('luassert')
|
2021-05-09 21:11:18 +00:00
|
|
|
local eq = assert.are.same
|
|
|
|
|
2021-05-05 18:04:16 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.meths = setmetatable({}, {
|
2021-09-03 18:28:20 +00:00
|
|
|
__index = function(_, key)
|
|
|
|
return vim.api['nvim_' .. key]
|
|
|
|
end,
|
2021-05-05 18:04:16 +00:00
|
|
|
})
|
|
|
|
|
2021-11-22 13:40:18 +00:00
|
|
|
function M.init_component(component, opts)
|
2021-09-03 18:28:20 +00:00
|
|
|
if component == nil then
|
|
|
|
component = 'special.function_component'
|
2021-10-10 16:43:00 +00:00
|
|
|
else
|
|
|
|
opts.component_name = component
|
|
|
|
end
|
|
|
|
local comp = require('lualine.components.' .. component)
|
|
|
|
if type(comp) == 'table' then
|
|
|
|
comp = comp(opts)
|
|
|
|
elseif type(comp) == 'function' then
|
|
|
|
opts[1] = comp
|
2022-01-02 11:38:39 +00:00
|
|
|
comp = require('lualine.components.special.function_component')(opts)
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2021-11-22 13:40:18 +00:00
|
|
|
return comp
|
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
-- Checks output of a component
|
2022-03-02 13:37:08 +00:00
|
|
|
M.assert_component = function(component, opts, result, is_active)
|
2021-11-22 13:40:18 +00:00
|
|
|
local comp = M.init_component(component, opts)
|
|
|
|
-- for testing global options
|
2022-03-02 13:37:08 +00:00
|
|
|
eq(result, comp:draw(opts.hl, is_active or true))
|
2021-05-05 18:04:16 +00:00
|
|
|
end
|
|
|
|
|
2023-03-30 04:12:37 +00:00
|
|
|
function M.assert_component_instance(comp, result)
|
2021-11-22 13:40:18 +00:00
|
|
|
eq(result, comp:draw(comp.options.hl))
|
|
|
|
end
|
2021-05-05 18:04:16 +00:00
|
|
|
-- sets defaults for component options
|
|
|
|
M.build_component_opts = function(opts)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not opts then
|
|
|
|
opts = {}
|
|
|
|
end
|
|
|
|
if opts[1] == nil then
|
|
|
|
opts[1] = function()
|
|
|
|
return 'test'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not opts.self then
|
2022-03-02 13:37:08 +00:00
|
|
|
opts.self = { section = 'c' }
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
|
|
|
if not opts.theme then
|
|
|
|
opts.theme = 'gruvbox'
|
|
|
|
end
|
|
|
|
if not opts.hl then
|
|
|
|
opts.hl = ''
|
|
|
|
end
|
|
|
|
if opts.icons_enabled == nil then
|
|
|
|
opts.icons_enabled = true
|
|
|
|
end
|
2021-05-05 18:04:16 +00:00
|
|
|
if not opts.component_separators then
|
2021-09-14 15:14:23 +00:00
|
|
|
opts.component_separators = { left = '', right = '' }
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
|
|
|
if not opts.section_separators then
|
2021-09-14 15:14:23 +00:00
|
|
|
opts.section_separators = { left = '', right = '' }
|
2021-05-05 18:04:16 +00:00
|
|
|
end
|
|
|
|
return opts
|
|
|
|
end
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
M.P = function(t)
|
|
|
|
print(vim.inspect(t))
|
|
|
|
end
|
2021-05-05 18:04:16 +00:00
|
|
|
|
2022-03-05 13:40:36 +00:00
|
|
|
function M.dedent(str, leave_indent)
|
|
|
|
-- find minimum common indent across lines
|
|
|
|
local indent = nil
|
|
|
|
for line in str:gmatch('[^\n]+') do
|
|
|
|
local line_indent = line:match('^%s+') or ''
|
|
|
|
if indent == nil or #line_indent < #indent then
|
|
|
|
indent = line_indent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if indent == nil or #indent == 0 then
|
|
|
|
-- no minimum common indent
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
local left_indent = (' '):rep(leave_indent or 0)
|
|
|
|
-- create a pattern for the indent
|
|
|
|
indent = indent:gsub('%s', '[ \t]')
|
|
|
|
-- strip it from the first line
|
|
|
|
str = str:gsub('^' .. indent, left_indent)
|
|
|
|
-- strip it from the remaining lines
|
|
|
|
str = str:gsub('[\n]' .. indent, '\n' .. left_indent)
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
2021-05-05 18:04:16 +00:00
|
|
|
return M
|