Debian Codegraph @

lua/lualine/components/special/vim_var_component.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local require = require('lualine_require').require
4 local M = require('lualine.component'):extend()
5 local utils = require('lualine.utils.utils')
6
7 function M:update_status()
8 local component = self.options[1]
9 -- vim variable component
10 -- accepts g:, v:, t:, w:, b:, o, go:, vo:, to:, wo:, bo:
11 -- filters g portion from g:var
12 local scope = component:match('[gvtwb]?o?')
13 -- filters var portion from g:var
14 local var_name = component:sub(#scope + 2, #component)
15 -- Displays nothing when variable aren't present
16 if not (scope and var_name) then
17 return ''
18 end
19 -- Support accessing keys within dictionary
20 -- https://github.com/nvim-lualine/lualine.nvim/issues/25#issuecomment-907374548
21 local name_chunks = vim.split(var_name, '%.')
22 local return_val = vim[scope][name_chunks[1]]
23 for i = 2, #name_chunks do
24 if return_val == nil then
25 break
26 end
27 return_val = return_val[name_chunks[i]]
28 end
29 if return_val == nil then
30 return ''
31 end
32 local ok
33 ok, return_val = pcall(tostring, return_val)
34 return ok and utils.stl_escape(return_val) or ''
35 end
36
37 return M