Enhance: Cleanup eval_lua component and document it

This commit is contained in:
shadmansaleh 2021-04-12 12:14:43 +06:00
parent 287e5def31
commit 77c4c47482
3 changed files with 37 additions and 16 deletions

View File

@ -177,7 +177,7 @@ sections = {lualine_a = {'FugitiveHead'}}
<details> <details>
<summary><b>Using variables as lualine component</b></summary> <summary><b>Using variables as lualine component</b></summary>
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 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 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'}}
</details> </details>
<details>
<summary><b>Using lua expressions as lualine component</b></summary>
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'}}
```
</details>
<details> <details>
<summary><b>Options for components</b></summary> <summary><b>Options for components</b></summary>

View File

@ -193,13 +193,27 @@ You can use vim functions as a lualine component
Using variables as lualine component~ Using variables as lualine component~
You can use variables from vim and lua globals as a lualine component You can use variables from vim. Variables from g:, v:, t:,
Variables from g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: scopes w:, b:, o, go:, vo:, to:, wo:, bo: scopes can be used.
can be used. Scopes ending with o are options usualy accessed with `&` in vimscript Scopes ending with o are options usualy accessed with `&` in
vimscript
> >
sections = {lualine_a = {'g:coc_status', 'bo:filetype'}} 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* CUSTOM OPTIONS FOR COMPONENTS *lualine_custom_options*

View File

@ -2,22 +2,13 @@ local EvalFuncComponent = require('lualine.component'):new()
EvalFuncComponent.update_status = function(self) EvalFuncComponent.update_status = function(self)
local component = self.options[1] 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 if not ok then status = EvalFuncComponent.vim_function(component) end
return status return status
end end
EvalFuncComponent.evallua = function(code) EvalFuncComponent.eval_lua = function(code)
if loadstring(string.format('return %s ~= nil', code)) and return tostring(loadstring('return '..code)())
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, ''
end end
EvalFuncComponent.vim_function = function(name) EvalFuncComponent.vim_function = function(name)