fix: % not properly escaped in other components too.

Now following components escapes results of external souces
- buffers
- tabs
- hostname
- branch
- filetype

fixes #579
This commit is contained in:
shadmansaleh 2022-02-15 23:17:05 +06:00
parent a69251d52b
commit 40849728b6
6 changed files with 36 additions and 13 deletions

View File

@ -1,8 +1,11 @@
-- Copyright (c) 2020-2021 shadmansaleh -- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local M = require('lualine.component'):extend() local M = require('lualine.component'):extend()
local require = require('lualine_require').require local modules = require('lualine_require').lazy_require {
local git_branch = require('lualine.components.branch.git_branch') git_branch = 'lualine.components.branch.git_branch',
highlight = 'lualine.highlight',
utils = 'lualine.utils.utils',
}
-- Initilizer -- Initilizer
M.init = function(self, options) M.init = function(self, options)
@ -10,11 +13,13 @@ M.init = function(self, options)
if not self.options.icon then if not self.options.icon then
self.options.icon = '' -- e0a0 self.options.icon = '' -- e0a0
end end
git_branch.init() modules.git_branch.init()
end end
M.update_status = function(_, is_focused) M.update_status = function(_, is_focused)
return git_branch.get_branch((not is_focused and vim.api.nvim_get_current_buf())) local buf = (not is_focused and vim.api.nvim_get_current_buf())
local branch = modules.git_branch.get_branch(buf)
return modules.utils.stl_escape(branch)
end end
return M return M

View File

@ -1,6 +1,10 @@
local highlight = require('lualine.highlight')
local Buffer = require('lualine.utils.class'):extend() local Buffer = require('lualine.utils.class'):extend()
local modules = require('lualine_require').lazy_require {
highlight = 'lualine.highlight',
utils = 'lualine.utils.utils',
}
---intialize a new buffer from opts ---intialize a new buffer from opts
---@param opts table ---@param opts table
function Buffer:init(opts) function Buffer:init(opts)
@ -13,7 +17,7 @@ end
---setup icons, modified status for buffer ---setup icons, modified status for buffer
function Buffer:get_props() function Buffer:get_props()
self.file = vim.api.nvim_buf_get_name(self.bufnr) self.file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(self.bufnr))
self.buftype = vim.api.nvim_buf_get_option(self.bufnr, 'buftype') self.buftype = vim.api.nvim_buf_get_option(self.bufnr, 'buftype')
self.filetype = vim.api.nvim_buf_get_option(self.bufnr, 'filetype') self.filetype = vim.api.nvim_buf_get_option(self.bufnr, 'filetype')
local modified = self.options.show_modified_status and vim.api.nvim_buf_get_option(self.bufnr, 'modified') local modified = self.options.show_modified_status and vim.api.nvim_buf_get_option(self.bufnr, 'modified')
@ -69,7 +73,8 @@ function Buffer:render()
-- setup for mouse clicks -- setup for mouse clicks
local line = string.format('%%%s@LualineSwitchBuffer@%s%%T', self.bufnr, name) local line = string.format('%%%s@LualineSwitchBuffer@%s%%T', self.bufnr, name)
-- apply highlight -- apply highlight
line = highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')]) .. line line = modules.highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')])
.. line
-- apply separators -- apply separators
if self.options.self.section < 'lualine_x' and not self.first then if self.options.self.section < 'lualine_x' and not self.first then

View File

@ -18,7 +18,8 @@ function M:init(options)
end end
function M.update_status() function M.update_status()
return vim.bo.filetype or '' local ft = vim.bo.filetype or ''
return modules.utils.stl_escape(ft)
end end
function M:apply_icon() function M:apply_icon()

View File

@ -1,7 +1,11 @@
-- Copyright (c) 2020-2021 hoob3rt -- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local modules = require('lualine_require').lazy_require {
utils = 'lualine.utils.utils',
}
local function hostname() local function hostname()
return vim.loop.os_gethostname() return modules.utils.stl_escape(vim.loop.os_gethostname())
end end
return hostname return hostname

View File

@ -1,6 +1,10 @@
local highlight = require('lualine.highlight')
local Tab = require('lualine.utils.class'):extend() local Tab = require('lualine.utils.class'):extend()
local modules = require('lualine_require').lazy_require {
highlight = 'lualine.highlight',
utils = 'lualine.utils.utils',
}
---intialize a new tab from opts ---intialize a new tab from opts
---@param opts table ---@param opts table
function Tab:init(opts) function Tab:init(opts)
@ -17,12 +21,12 @@ end
function Tab:label() function Tab:label()
local custom_tabname = vim.t[self.tabId].tabname local custom_tabname = vim.t[self.tabId].tabname
if custom_tabname and custom_tabname ~= '' then if custom_tabname and custom_tabname ~= '' then
return custom_tabname return modules.utils.stl_escape(custom_tabname)
end end
local buflist = vim.fn.tabpagebuflist(self.tabnr) local buflist = vim.fn.tabpagebuflist(self.tabnr)
local winnr = vim.fn.tabpagewinnr(self.tabnr) local winnr = vim.fn.tabpagewinnr(self.tabnr)
local bufnr = buflist[winnr] local bufnr = buflist[winnr]
local file = vim.api.nvim_buf_get_name(bufnr) local file = modules.utils.stl_escape(vim.api.nvim_buf_get_name(bufnr))
local buftype = vim.fn.getbufvar(bufnr, '&buftype') local buftype = vim.fn.getbufvar(bufnr, '&buftype')
if buftype == 'help' then if buftype == 'help' then
return 'help:' .. vim.fn.fnamemodify(file, ':t:r') return 'help:' .. vim.fn.fnamemodify(file, ':t:r')
@ -62,7 +66,8 @@ function Tab:render()
-- setup for mouse clicks -- setup for mouse clicks
local line = string.format('%%%s@LualineSwitchTab@%s%%T', self.tabnr, name) local line = string.format('%%%s@LualineSwitchTab@%s%%T', self.tabnr, name)
-- apply highlight -- apply highlight
line = highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')]) .. line line = modules.highlight.component_format_highlight(self.highlights[(self.current and 'active' or 'inactive')])
.. line
-- apply separators -- apply separators
if self.options.self.section < 'lualine_x' and not self.first then if self.options.self.section < 'lualine_x' and not self.first then

View File

@ -174,6 +174,9 @@ end
---@param str string ---@param str string
---@return string ---@return string
function M.stl_escape(str) function M.stl_escape(str)
if type(str) ~= 'string' then
return str
end
return str:gsub('%%', '%%%%') return str:gsub('%%', '%%%%')
end end