From 8a4baa804b7b2906eb8b9c325546dceabffdfcfd Mon Sep 17 00:00:00 2001 From: Shadman Date: Wed, 10 Feb 2021 16:40:29 +0600 Subject: [PATCH] Added other sources for component configuration (#53) --- README.md | 24 ++++++++++++++++++++++++ doc/lualine.txt | 30 ++++++++++++++++++++++++++---- doc/tags | 2 ++ lua/lualine.lua | 42 +++++++++++++++++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4c7b7f9..dac6b22 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,30 @@ lualine.sections.lualine_a = { hello } +
+Using vim functions as lualine component + +You can use vim functions as a lualine component + +``` +lualine.sections.lualine_b = { 'FugitiveHead' } +``` + +
+ +
+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 + +``` +lualine.sections.lualine_b = { 'g:coc_status', 'bo:filetype' } +``` + +
+ ### Loading plugin extensions Lualine extensions change statusline appearance for a window/buffer with a plugin loaded e.g. [junegunn/fzf.vim](https://github.com/junegunn/fzf.vim) diff --git a/doc/lualine.txt b/doc/lualine.txt index 2a0ff4d..133cbb6 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -34,10 +34,11 @@ CONTENTS *lualline-contents* 1.3.2. Setting a theme.........................|lualine-setting_a_theme| 1.3.3. Changing separator...................|lualine-changing_separator| 1.3.4. Changing components.................|lualine-changing_components| - 1.3.5. Loading plugin extensions.....|lualine-loading_plugin_extensions| - 1.3.6 Config examples.........................|lualine-config_examples| - 1.3.6.1. Packer.nvim......|lualine-config_example_using_packer.nvim| - 1.3.6.2 VIML example.......|lualine-full_config_example_inside_viml| + 1.3.5. Building Custom components............|lualine-custom_components| + 1.3.6. Loading plugin extensions.....|lualine-loading_plugin_extensions| + 1.3.7 Config examples.........................|lualine-config_examples| + 1.3.7.1. Packer.nvim......|lualine-config_example_using_packer.nvim| + 1.3.7.2 VIML example.......|lualine-full_config_example_inside_viml| 1.4. Contributing.....................................|lualine-contributing| 1.5. Screenshots.......................................|lualine-screenshots| @@ -162,6 +163,8 @@ Available components~ * signify +-------------------------------------------------------------------------------- +BUILDING YOUR COMPONENTS *lualine-custom_components* Using custom functions as lualine component~ @@ -173,6 +176,25 @@ You can define a custom function as a lualine component end lualine.sections.lualine_a = { hello } < +Using vim functions as lualine component~ + +You can use vim functions as a lualine component + +> + lualine.sections.lualine_b = { 'FugitiveHead' } +< + +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 + +> + lualine.sections.lualine_b = { 'g:coc_status', 'bo:filetype' } + lualine.sections.lualine_x = { 'vim.bo.fileencoding' } +< + -------------------------------------------------------------------------------- LOADING PLUGIN EXTENSIONS *lualine-loading_plugin_extensions* diff --git a/doc/tags b/doc/tags index 4df762e..3eb8940 100644 --- a/doc/tags +++ b/doc/tags @@ -4,10 +4,12 @@ lualine-changing_separator lualine.txt /*lualine-changing_separator* lualine-config_example_using_packer.nvim lualine.txt /*lualine-config_example_using_packer.nvim* lualine-config_examples lualine.txt /*lualine-config_examples* lualine-contributing lualine.txt /*lualine-contributing* +lualine-custom_components lualine.txt /*lualine-custom_components* lualine-full_config_example_inside_viml lualine.txt /*lualine-full_config_example_inside_viml* lualine-installation lualine.txt /*lualine-installation* lualine-loading_plugin_extensions lualine.txt /*lualine-loading_plugin_extensions* lualine-lualine.nvim lualine.txt /*lualine-lualine.nvim* +lualine-nvim lualine.txt /*lualine-nvim* lualine-packer.nvim lualine.txt /*lualine-packer.nvim* lualine-performance_comparism lualine.txt /*lualine-performance_comparism* lualine-screenshots lualine.txt /*lualine-screenshots* diff --git a/lua/lualine.lua b/lua/lualine.lua index 3547564..1fd77f6 100644 --- a/lua/lualine.lua +++ b/lua/lualine.lua @@ -29,12 +29,52 @@ M.inactive_sections = { M.extensions = { } +local function load_special_components(component) + return function() + -- precedence lualine_component > vim_var > lua_var > vim_function + if component:find('[gvtwb]?o?:') == 1 then + -- vim veriable component + -- accepts g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo: + -- filters g portion from g:var + local scope = component:match('[gvtwb]?o?') + -- filters var portion from g:var + -- For some reason overwriting component var from outer scope causes the + -- component not to work . So creating a new local name component to use:/ + local component = component:sub(#scope + 2, #component) + -- Displays nothing when veriable aren't present + local return_val = vim[scope][component] + if return_val == nil then return '' end + local ok + ok, return_val = pcall(tostring, return_val) + if ok then return return_val end + return '' + elseif loadstring(string.format('return %s ~= nil', component)) and + loadstring(string.format([[return %s ~= nil]], component))() then + -- lua veriable component + return loadstring(string.format([[ + local ok, return_val = pcall(tostring, %s) + if ok then return return_val end + return '']], component))() + else + -- vim function component + local ok, return_val = pcall(vim.fn[component]) + 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 + end + end +end + local function load_components() local function load_sections(sections) for _, section in pairs(sections) do for index, component in pairs(section) do if type(component) == 'string' then - section[index] = require('lualine.components.' .. component) + local ok,loaded_component = pcall(require, 'lualine.components.' .. component) + if not ok then + loaded_component = load_special_components(component) + end + section[index] = loaded_component end end end