From c9c5e3f9d1c3fd9ff968d15d4e809039821502d0 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Fri, 20 Aug 2021 18:32:56 +0600 Subject: [PATCH] feat: Add type option to hint what type of component is the string String values for component are way too overloaded . This means lualine has to make lots of checks to understand how to load a string value as a component . With type option users can specify exactly what they want. --- README.md | 9 ++- doc/lualine.txt | 10 ++- .../special/eval_func_component.lua | 22 +++--- .../components/special/vim_var_component.lua | 4 +- lua/lualine/utils/loader.lua | 69 ++++++++++++++----- 5 files changed, 84 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index cddb2c8..f9c6ff6 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,14 @@ sections = { -- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'} -- or highlight group -- color = "WarningMsg" - color = nil + color = nil, + -- Type option specifies what type a component is. + -- When type is omitted lualine will guess it. + -- Available types [format: type_name(example)] + -- mod(branch/filename), stl(%f/%m), var(g:coc_status/bo:modifiable), + -- luae(lua expressions), vimf(viml function name) + -- luae is short for lua-expression and vimf is short fror vim-function + type = nil, } } } diff --git a/doc/lualine.txt b/doc/lualine.txt index b624ffc..0da330d 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -220,7 +220,15 @@ Local options~ -- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'} -- or highlight group -- color = "WarningMsg" - color = nil + color = nil, + -- Type option specifies what type a component is. + -- When type is omitted lualine will guess it. + -- Available types [format: type_name(example)] + -- mod(branch/filename), stl(%f/%m), var(g:coc_status/bo:modifiable), + -- luae(lua expressions), vimf(viml function name) + -- luae is short for lua-expression and vimf is short fror vim-function + -- vim_function(viml function name) + type = nil, } } } diff --git a/lua/lualine/components/special/eval_func_component.lua b/lua/lualine/components/special/eval_func_component.lua index 3bad302..7c82ad0 100644 --- a/lua/lualine/components/special/eval_func_component.lua +++ b/lua/lualine/components/special/eval_func_component.lua @@ -2,12 +2,22 @@ local EvalFuncComponent = require('lualine.component'):new() EvalFuncComponent.update_status = function(self) local component = self.options[1] - local ok, status = pcall(EvalFuncComponent.eval_lua, component) - if not ok then status = EvalFuncComponent.vim_function(component) end + local ok, status + if self.options.type == nil then + ok, status = pcall(EvalFuncComponent.lua_eval, component) + if not ok then status = EvalFuncComponent.vim_function(component) end + else + if self.options.type == 'luae' then + ok, status = pcall(EvalFuncComponent.lua_eval, component) + if not ok then status = nil end + elseif self.options.type == 'vimf' then + status = EvalFuncComponent.vim_function(component) + end + end return status end -EvalFuncComponent.eval_lua = function(code) +EvalFuncComponent.lua_eval = function(code) local result = loadstring('return ' .. code)() assert(result, 'String expected got nil') return tostring(result) @@ -18,11 +28,7 @@ EvalFuncComponent.vim_function = function(name) local ok, return_val = pcall(vim.fn[name]) if not ok then return '' end -- function call failed ok, return_val = pcall(tostring, return_val) - if ok then - return return_val - else - return '' - end + return ok and return_val or '' end return EvalFuncComponent diff --git a/lua/lualine/components/special/vim_var_component.lua b/lua/lualine/components/special/vim_var_component.lua index 245d130..53e3dbd 100644 --- a/lua/lualine/components/special/vim_var_component.lua +++ b/lua/lualine/components/special/vim_var_component.lua @@ -8,12 +8,12 @@ VarComponent.update_status = function(self) -- filters var portion from g:var local var_name = component:sub(#scope + 2, #component) -- Displays nothing when veriable aren't present + if not (scope and var_name) then return '' end local return_val = vim[scope][var_name] if return_val == nil then return '' end local ok ok, return_val = pcall(tostring, return_val) - if ok then return return_val end - return '' + return ok and return_val or '' end return VarComponent diff --git a/lua/lualine/utils/loader.lua b/lua/lualine/utils/loader.lua index 049d09e..77aeaf6 100644 --- a/lua/lualine/utils/loader.lua +++ b/lua/lualine/utils/loader.lua @@ -60,31 +60,64 @@ local function lualine_load(patern, use_cache) return retval end -local function component_loader(component) - if type(component[1]) == 'function' then +local component_types = { + luaf = function(component) return lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component) - end - if type(component[1]) == 'string' then - -- load the component + end, + mod = function(component) local ok, loaded_component = pcall(lualine_load, - {'lua', 'lualine', 'components', component[1]}, true) + {'lua', 'lualine', 'components', component[1]}, true) if ok then component.component_name = component[1] loaded_component = loaded_component:new(component) - elseif string.char(component[1]:byte(1)) == '%' then - local stl_expr = component[1] -- Vim's %p %l statusline elements - component[1] = function() return stl_expr end - loaded_component = - lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component) - elseif component[1]:find('[gvtwb]?o?:') == 1 then - loaded_component = - lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component) - else - loaded_component = - lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component) + return loaded_component + end + end, + stl = function(component) + local stl_expr = component[1] -- Vim's %p %l statusline elements + component[1] = function() return stl_expr end + return + lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component) + end, + var = function(component) + return + lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component) + end, + ['_'] = function(component) + return + lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component) + end +} + +local function component_loader(component) + if type(component[1]) == 'function' then + return component_types.luaf(component) + end + if type(component[1]) == 'string' then + -- load the component + if component.type ~= nil then + if component_types[component.type] and component.type ~= 'luaf' then + return component_types[component.type](component) + elseif component.type == 'vimf' or component.type == 'luae' then + return component_types['_'](component) + else + notice.add_notice(string.format([[ +### component.type + +component type '%s' isn't recognised. Check if spelling is correct.]], component.type)) + end + end + local loaded_component = component_types.mod(component) + if loaded_component then + return loaded_component + elseif string.char(component[1]:byte(1)) == '%' then + return component_types.stl(component) + elseif component[1]:find('[gvtwb]?o?:') == 1 then + return component_types.var(component) + else + return component_types['_'](component) end - return loaded_component end end