From b8ad8a13b29f6bb1195d89aab97f90e250a6be1f Mon Sep 17 00:00:00 2001 From: "Eli W. Hunter" <42009212+elihunter173@users.noreply.github.com> Date: Sun, 3 Jan 2021 14:11:22 -0500 Subject: [PATCH] Use table.concat instead of string concatentation (#7) --- lua/lualine.lua | 30 +++++++++++++++--------------- lua/lualine/highlight.lua | 24 ++++++++++-------------- lua/lualine/utils.lua | 30 +++++++++++------------------- 3 files changed, 36 insertions(+), 48 deletions(-) diff --git a/lua/lualine.lua b/lua/lualine.lua index 8978ac5..6108784 100644 --- a/lua/lualine.lua +++ b/lua/lualine.lua @@ -66,33 +66,33 @@ local function StatusLine(isFocused) M.theme = utils.setTheme(M.theme) highlight.createHighlightGroups(M.theme) end - local status = '' + local status = {} if sections.lualine_a ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_a') - status = status .. utils.drawSection(sections.lualine_a, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_a')) + table.insert(status, utils.drawSection(sections.lualine_a, M.separator)) end if sections.lualine_b ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_b') - status = status .. utils.drawSection(sections.lualine_b, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_b')) + table.insert(status, utils.drawSection(sections.lualine_b, M.separator)) end if sections.lualine_c ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_c') - status = status .. utils.drawSection(sections.lualine_c, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_c')) + table.insert(status, utils.drawSection(sections.lualine_c, M.separator)) end - status = status .. [[%=]] + table.insert(status, "%=") if sections.lualine_x ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_c') - status = status .. utils.drawSection(sections.lualine_x, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_c')) + table.insert(status, utils.drawSection(sections.lualine_x, M.separator)) end if sections.lualine_y ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_b') - status = status .. utils.drawSection(sections.lualine_y, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_b')) + table.insert(status, utils.drawSection(sections.lualine_y, M.separator)) end if sections.lualine_z ~= nil then - status = status .. highlight.formatHighlight(isFocused, 'lualine_a') - status = status .. utils.drawSection(sections.lualine_z, M.separator) + table.insert(status, highlight.formatHighlight(isFocused, 'lualine_a')) + table.insert(status, utils.drawSection(sections.lualine_z, M.separator)) end - return status + return table.concat(status) end function M.status() diff --git a/lua/lualine/highlight.lua b/lua/lualine/highlight.lua index 8ff99b1..bd13437 100644 --- a/lua/lualine/highlight.lua +++ b/lua/lualine/highlight.lua @@ -1,27 +1,23 @@ local M = { } local function highlight (name, foreground, background, special) - if special == nil then - special = 'none' - end - local command = 'highlight ' - command = command .. name .. ' ' - command = command .. 'guifg=' .. foreground .. ' ' - command = command .. 'guibg=' .. background .. ' ' - if special then - command = command .. 'gui=' .. special .. ' ' - end - return command + local command = { + 'highlight', name, + 'guifg=' .. foreground, + 'guibg=' .. background, + 'gui=' .. (special or 'none'), + } + return table.concat(command, ' ') end function M.createHighlightGroups(theme) for mode, sections in pairs(theme) do for section, colorscheme in pairs(sections) do + local special = nil if section == 'a' then - vim.cmd(highlight('lualine_' .. section .. '_' .. mode, colorscheme.fg, colorscheme.bg ,'bold')) - else - vim.cmd(highlight('lualine_' .. section .. '_' .. mode, colorscheme.fg, colorscheme.bg )) + special = 'bold' end + vim.cmd(highlight('lualine_' .. section .. '_' .. mode, colorscheme.fg, colorscheme.bg, special)) end end end diff --git a/lua/lualine/utils.lua b/lua/lualine/utils.lua index 83a8f8b..dd99a4f 100644 --- a/lua/lualine/utils.lua +++ b/lua/lualine/utils.lua @@ -5,29 +5,21 @@ function M.setTheme(theme) end function M.drawSection(section, separator) - local status = '' - for index, statusFunction in pairs(section) do + local status = {} + for _, statusFunction in pairs(section) do local localstatus = statusFunction() - if localstatus:len() > 0 then - if separator:len() > 0 then - if index > 1 then - status = status .. separator .. ' ' - end - status = status .. localstatus - status = status .. ' ' - else - status = status .. localstatus - status = status .. ' ' - end + if #localstatus > 0 then + table.insert(status, localstatus) end end - if status:len() > 0 then - if separator:len() > 0 and table.maxn(section) > 1 then - return ' ' .. status .. ' ' - end - return ' ' .. status + if #status == 0 then + return '' end - return '' + local sep = ' ' + if #separator > 0 then + sep = ' ' .. separator .. ' ' + end + return ' ' .. table.concat(status, sep) .. ' ' end return M