From 77c4c4748254cd6087c6cc1e8be72485d278eb48 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Mon, 12 Apr 2021 12:14:43 +0600 Subject: [PATCH] Enhance: Cleanup eval_lua component and document it --- README.md | 18 ++++++++++++++++- doc/lualine.txt | 20 ++++++++++++++++--- .../special/eval_func_component.lua | 15 +++----------- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cddd86f..0d15869 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ sections = {lualine_a = {'FugitiveHead'}}
Using variables as lualine component -You can use variables from vim and lua globals as a lualine component +You can use variables from vim a lualine component Variables from g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: scopes can be used. Scopes ending with o are options usualy accessed with `&` in vimscript @@ -187,6 +187,22 @@ sections = {lualine_a = {'g:coc_status', 'bo:filetype'}}
+
+Using lua expressions as lualine component + +You can use any valid lua expression as a component . This allows global +variables to be used as a component too. Even require statements can be used to access values returned by specific scripts. +One liner functions can be inlined by utilizeing this . + +For exmaple this will show day of the week. +And 2nd one will display current value of global variabke data. + +```lua +sections = {lualine_c = {"os.data('%a')", 'data'}} +``` + +
+
Options for components diff --git a/doc/lualine.txt b/doc/lualine.txt index 6674ce6..c7c28b4 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -193,13 +193,27 @@ You can use vim functions as a lualine component Using variables as lualine component~ -You can use variables from vim and lua globals as a lualine component -Variables from g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: scopes -can be used. Scopes ending with o are options usualy accessed with `&` in vimscript +You can use variables from vim. Variables from g:, v:, t:, +w:, b:, o, go:, vo:, to:, wo:, bo: scopes can be used. +Scopes ending with o are options usualy accessed with `&` in +vimscript > sections = {lualine_a = {'g:coc_status', 'bo:filetype'}} < +Using lua expressions as lualine component~ + +You can use any valid lua expression as a component . This +allows global variables to be used as a component too. Even +require statements can be used to access values returned by +specific scripts. One liner functions can be inlined by +utilizeing this . + +For exmaple this will show day of the week. And 2nd one +will display current value of global variabke data. +> + sections = {lualine_c = {"os.data('%a')", 'data'}} +< -------------------------------------------------------------------------------- CUSTOM OPTIONS FOR COMPONENTS *lualine_custom_options* diff --git a/lua/lualine/components/special/eval_func_component.lua b/lua/lualine/components/special/eval_func_component.lua index 93e1234..0cae2c4 100644 --- a/lua/lualine/components/special/eval_func_component.lua +++ b/lua/lualine/components/special/eval_func_component.lua @@ -2,22 +2,13 @@ local EvalFuncComponent = require('lualine.component'):new() EvalFuncComponent.update_status = function(self) local component = self.options[1] - local ok, status = EvalFuncComponent.evallua(component) + local ok, status = pcall(EvalFuncComponent.eval_lua, component) if not ok then status = EvalFuncComponent.vim_function(component) end return status end -EvalFuncComponent.evallua = function(code) - if loadstring(string.format('return %s ~= nil', code)) and - loadstring(string.format([[return %s ~= nil]], code))() then - -- lua veriable component - return true, loadstring(string.format( - [[ - local ok, return_val = pcall(tostring, %s) - if ok then return return_val end - return '']], code))() - end - return false, '' +EvalFuncComponent.eval_lua = function(code) + return tostring(loadstring('return '..code)()) end EvalFuncComponent.vim_function = function(name)