From 40849728b6c9b3389c7b54739f426f9899b53778 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Tue, 15 Feb 2022 23:17:05 +0600 Subject: [PATCH] fix: % not properly escaped in other components too. Now following components escapes results of external souces - buffers - tabs - hostname - branch - filetype fixes #579 --- lua/lualine/components/branch/init.lua | 13 +++++++++---- lua/lualine/components/buffers/buffer.lua | 11 ++++++++--- lua/lualine/components/filetype.lua | 3 ++- lua/lualine/components/hostname.lua | 6 +++++- lua/lualine/components/tabs/tab.lua | 13 +++++++++---- lua/lualine/utils/utils.lua | 3 +++ 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lua/lualine/components/branch/init.lua b/lua/lualine/components/branch/init.lua index 9496191..8d3970d 100644 --- a/lua/lualine/components/branch/init.lua +++ b/lua/lualine/components/branch/init.lua @@ -1,8 +1,11 @@ -- Copyright (c) 2020-2021 shadmansaleh -- MIT license, see LICENSE for more details. local M = require('lualine.component'):extend() -local require = require('lualine_require').require -local git_branch = require('lualine.components.branch.git_branch') +local modules = require('lualine_require').lazy_require { + git_branch = 'lualine.components.branch.git_branch', + highlight = 'lualine.highlight', + utils = 'lualine.utils.utils', +} -- Initilizer M.init = function(self, options) @@ -10,11 +13,13 @@ M.init = function(self, options) if not self.options.icon then self.options.icon = '' -- e0a0 end - git_branch.init() + modules.git_branch.init() end 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 return M diff --git a/lua/lualine/components/buffers/buffer.lua b/lua/lualine/components/buffers/buffer.lua index 6110f2b..a5e6d78 100644 --- a/lua/lualine/components/buffers/buffer.lua +++ b/lua/lualine/components/buffers/buffer.lua @@ -1,6 +1,10 @@ -local highlight = require('lualine.highlight') 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 ---@param opts table function Buffer:init(opts) @@ -13,7 +17,7 @@ end ---setup icons, modified status for buffer 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.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') @@ -69,7 +73,8 @@ function Buffer:render() -- setup for mouse clicks local line = string.format('%%%s@LualineSwitchBuffer@%s%%T', self.bufnr, name) -- 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 if self.options.self.section < 'lualine_x' and not self.first then diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index dc2f12f..28e947c 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -18,7 +18,8 @@ function M:init(options) end function M.update_status() - return vim.bo.filetype or '' + local ft = vim.bo.filetype or '' + return modules.utils.stl_escape(ft) end function M:apply_icon() diff --git a/lua/lualine/components/hostname.lua b/lua/lualine/components/hostname.lua index 9c43b14..c43121e 100644 --- a/lua/lualine/components/hostname.lua +++ b/lua/lualine/components/hostname.lua @@ -1,7 +1,11 @@ -- Copyright (c) 2020-2021 hoob3rt -- MIT license, see LICENSE for more details. +local modules = require('lualine_require').lazy_require { + utils = 'lualine.utils.utils', +} + local function hostname() - return vim.loop.os_gethostname() + return modules.utils.stl_escape(vim.loop.os_gethostname()) end return hostname diff --git a/lua/lualine/components/tabs/tab.lua b/lua/lualine/components/tabs/tab.lua index e7c9e07..a3236a9 100644 --- a/lua/lualine/components/tabs/tab.lua +++ b/lua/lualine/components/tabs/tab.lua @@ -1,6 +1,10 @@ -local highlight = require('lualine.highlight') 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 ---@param opts table function Tab:init(opts) @@ -17,12 +21,12 @@ end function Tab:label() local custom_tabname = vim.t[self.tabId].tabname if custom_tabname and custom_tabname ~= '' then - return custom_tabname + return modules.utils.stl_escape(custom_tabname) end local buflist = vim.fn.tabpagebuflist(self.tabnr) local winnr = vim.fn.tabpagewinnr(self.tabnr) 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') if buftype == 'help' then return 'help:' .. vim.fn.fnamemodify(file, ':t:r') @@ -62,7 +66,8 @@ function Tab:render() -- setup for mouse clicks local line = string.format('%%%s@LualineSwitchTab@%s%%T', self.tabnr, name) -- 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 if self.options.self.section < 'lualine_x' and not self.first then diff --git a/lua/lualine/utils/utils.lua b/lua/lualine/utils/utils.lua index 061d11e..f6a7015 100644 --- a/lua/lualine/utils/utils.lua +++ b/lua/lualine/utils/utils.lua @@ -174,6 +174,9 @@ end ---@param str string ---@return string function M.stl_escape(str) + if type(str) ~= 'string' then + return str + end return str:gsub('%%', '%%%%') end