enhance: allow components to be functions too (#81)

Some components like hostname, progress are so simple that
the component class setup is just unnecessary boilerplate.
Allowing them to be function simplifies things.
With this you can put your regular component functions in
~/.config/nvim/lua/lualine/components/ folder and treat then as
regular lualine components just like 'mode' or 'branch'.
Hopefully this will help lualine plugins grow.
This commit is contained in:
shadmansaleh 2021-10-10 22:43:00 +06:00
parent 61ac665774
commit c8750a35e0
8 changed files with 28 additions and 29 deletions

View File

@ -1,9 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
M.update_status = function()
local function encoding()
return [[%{strlen(&fenc)?&fenc:&enc}]]
end
return M
return encoding

View File

@ -1,8 +1,6 @@
-- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
M.update_status = function()
local function filesize()
local file = vim.fn.expand '%:p'
if file == nil or #file == 0 then
return ''
@ -24,4 +22,4 @@ M.update_status = function()
return string.format('%.1f%s', size, sufixes[i])
end
return M
return filesize

View File

@ -1,7 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
local function hostname()
return vim.loop.os_gethostname()
end
M.update_status = vim.loop.os_gethostname
return M
return hostname

View File

@ -1,9 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
M.update_status = function()
local function location()
return [[%3l:%-2c]]
end
return M
return location

View File

@ -1,10 +1,4 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local require = require('lualine_require').require
local get_mode = require('lualine.utils.mode').get_mode
local M = require('lualine.component'):extend()
M.update_status = get_mode
return M
return get_mode

View File

@ -1,9 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend()
M.update_status = function()
local function progress()
return [[%3P]]
end
return M
return progress

View File

@ -18,7 +18,12 @@ local component_types = {
local ok, loaded_component = pcall(require, 'lualine.components.' .. component[1])
if ok then
component.component_name = component[1]
loaded_component = loaded_component(component)
if type(loaded_component) == 'table' then
loaded_component = loaded_component(component)
elseif type(loaded_component) == 'function' then
component[1] = loaded_component
loaded_component = require 'lualine.components.special.function_component'(component)
end
return loaded_component
end
end,

View File

@ -16,8 +16,16 @@ M.assert_component = function(component, opts, result)
-- for testing global options
if component == nil then
component = 'special.function_component'
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
comp = require 'lualine.components.special.function_component'(opts)
end
local comp = require('lualine.components.' .. component)(opts)
eq(result, comp:draw(opts.hl))
end