From 5785d4b47f0f5c51685474f3d1cc49963c8a8169 Mon Sep 17 00:00:00 2001 From: Glenn Waters Date: Sat, 25 Sep 2021 20:15:22 -0400 Subject: [PATCH] enhance: use table to map mode to hl group (#67) I believe a hash is more efficient than a multiple comparison if. The code is also drier, no replication of the concatenation line of code. --- lua/lualine/highlight.lua | 40 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/lua/lualine/highlight.lua b/lua/lualine/highlight.lua index b441bd7..5e15cfb 100644 --- a/lua/lualine/highlight.lua +++ b/lua/lualine/highlight.lua @@ -15,6 +15,24 @@ local create_cterm_colors = false -- table to store the highlight names created by lualine local loaded_highlights = {} +-- table to map mode to highlight suffixes +local mode_to_highlight = { + ['VISUAL'] = '_visual', + ['V-BLOCK'] = '_visual', + ['V-LINE'] = '_visual', + ['SELECT'] = '_visual', + ['S-LINE'] = '_visual', + ['S-BLOCK'] = '_visual', + ['REPLACE'] = '_replace', + ['V-REPLACE'] = '_replace', + ['INSERT'] = '_insert', + ['COMMAND'] = '_command', + ['EX'] = '_command', + ['MORE'] = '_command', + ['CONFIRM'] = '_command', + ['TERMINAL'] = '_terminal', +} + -- determine if an highlight exist and isn't cleared function M.highlight_exists(highlight_name) return loaded_highlights[highlight_name] or false @@ -98,27 +116,7 @@ function M.append_mode(highlight_group, is_focused) return highlight_group .. '_inactive' end local mode = require('lualine.utils.mode').get_mode() - if - mode == 'VISUAL' - or mode == 'V-BLOCK' - or mode == 'V-LINE' - or mode == 'SELECT' - or mode == 'S-LINE' - or mode == 'S-BLOCK' - then - highlight_group = highlight_group .. '_visual' - elseif mode == 'REPLACE' or mode == 'V-REPLACE' then - highlight_group = highlight_group .. '_replace' - elseif mode == 'INSERT' then - highlight_group = highlight_group .. '_insert' - elseif mode == 'COMMAND' or mode == 'EX' or mode == 'MORE' or mode == 'CONFIRM' then - highlight_group = highlight_group .. '_command' - elseif mode == 'TERMINAL' then - highlight_group = highlight_group .. '_terminal' - else - highlight_group = highlight_group .. '_normal' - end - return highlight_group + return highlight_group .. (mode_to_highlight[mode] or '_normal') end -- Helper function for create component highlight