From c8750a35e089bab7707e2431a1ac1709f78becb1 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Sun, 10 Oct 2021 22:43:00 +0600 Subject: [PATCH] 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. --- lua/lualine/components/encoding.lua | 6 ++---- lua/lualine/components/filesize.lua | 6 ++---- lua/lualine/components/hostname.lua | 8 ++++---- lua/lualine/components/location.lua | 6 ++---- lua/lualine/components/mode.lua | 8 +------- lua/lualine/components/progress.lua | 6 ++---- lua/lualine/utils/loader.lua | 7 ++++++- lua/tests/helpers.lua | 10 +++++++++- 8 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lua/lualine/components/encoding.lua b/lua/lualine/components/encoding.lua index d191395..f40aecc 100644 --- a/lua/lualine/components/encoding.lua +++ b/lua/lualine/components/encoding.lua @@ -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 diff --git a/lua/lualine/components/filesize.lua b/lua/lualine/components/filesize.lua index 4336d21..cc95da8 100644 --- a/lua/lualine/components/filesize.lua +++ b/lua/lualine/components/filesize.lua @@ -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 diff --git a/lua/lualine/components/hostname.lua b/lua/lualine/components/hostname.lua index 4b04d3a..9c43b14 100644 --- a/lua/lualine/components/hostname.lua +++ b/lua/lualine/components/hostname.lua @@ -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 diff --git a/lua/lualine/components/location.lua b/lua/lualine/components/location.lua index 91aaac5..1ffff54 100644 --- a/lua/lualine/components/location.lua +++ b/lua/lualine/components/location.lua @@ -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 diff --git a/lua/lualine/components/mode.lua b/lua/lualine/components/mode.lua index 092a674..721d6ca 100644 --- a/lua/lualine/components/mode.lua +++ b/lua/lualine/components/mode.lua @@ -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 diff --git a/lua/lualine/components/progress.lua b/lua/lualine/components/progress.lua index 0b6aec8..e4a7054 100644 --- a/lua/lualine/components/progress.lua +++ b/lua/lualine/components/progress.lua @@ -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 diff --git a/lua/lualine/utils/loader.lua b/lua/lualine/utils/loader.lua index 0e05682..ed0788a 100644 --- a/lua/lualine/utils/loader.lua +++ b/lua/lualine/utils/loader.lua @@ -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, diff --git a/lua/tests/helpers.lua b/lua/tests/helpers.lua index 6a3516e..74e1b86 100644 --- a/lua/tests/helpers.lua +++ b/lua/tests/helpers.lua @@ -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