From a4b30c9ee40ff0aba71fbad8e59926f19d231209 Mon Sep 17 00:00:00 2001 From: Olivier Roques Date: Wed, 23 Dec 2020 14:59:27 +0100 Subject: [PATCH] Add color support --- lua/hardline.lua | 66 +++++++++++++++++++++++++++++---------- lua/hardline/filename.lua | 4 +-- lua/hardline/git.lua | 4 +-- lua/hardline/line.lua | 4 +-- lua/hardline/mode.lua | 18 +++++------ plugin/hardline.vim | 2 +- 6 files changed, 64 insertions(+), 34 deletions(-) diff --git a/lua/hardline.lua b/lua/hardline.lua index 001196f..0bd62cd 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -7,8 +7,9 @@ local cmd, vim = vim.cmd, vim local wo = vim.wo local M = {} +-------------------- OPTIONS ------------------------------- M.events = { - reload = { + active = { 'BufEnter', 'BufReadPost', 'BufWinEnter', @@ -18,7 +19,7 @@ M.events = { 'VimResized', 'WinEnter', }, - unload = { + inactive = { 'WinLeave', }, } @@ -36,6 +37,15 @@ M.sections = { {class = 'Z', item = require('hardline.line').get_item()}, } +M.colors = { + active = { + A = {ctermfg='170',guifg='#C678DD'}, + }, + inactive = { + A = {ctermfg='170',guifg='#C678DD'}, + }, +} + -------------------- HELPERS ------------------------------- local function echo(hlgroup, msg) cmd(string.format('echohl %s', hlgroup)) @@ -43,39 +53,63 @@ local function echo(hlgroup, msg) cmd('echohl None') end -local function load_section(section) - if not section then return end +-------------------- STATUSLINE ---------------------------- +local function color_item(item, class, active) + local mode = active and 'Active' or 'Inactive' + if not M.colors[string.lower(mode)][class] then return item end + return string.format('%%#Hardline%s%s#%s%%##', mode, class, item) +end + +local function reload_section(section, active) if type(section) == 'function' then return section() elseif type(section) == 'string' then return section elseif type(section) == 'table' then - return load_section(section.item) + return color_item(section.item, section.class, active) end echo('WarningMsg', 'Invalid section.') return '' end --------------------- INTERFACE ----------------------------- -function M.unload() - wo.statusline = table.concat(vim.tbl_map(load_section, M.sections)) +function M.reload(active) + local items = {} + for _, section in ipairs(M.sections) do + table.insert(items, reload_section(section, active)) + end + wo.statusline = table.concat(items) end -function M.reload() - wo.statusline = table.concat(vim.tbl_map(load_section, M.sections)) +-------------------- SETUP ----------------------------- +local function set_hlgroups() + for _, mode in ipairs({'Active', 'Inactive'}) do + local m = string.lower(mode) + for class, args in pairs(M.colors[m]) do + local a = {} + for k, v in pairs(args) do + table.insert(a, string.format('%s=%s', k, v)) + end + cmd(string.format('hi Hardline%s%s %s', mode, class, table.concat(a, ' '))) + end + end end -function M.autocommands() +local function set_autocmds() cmd 'augroup hardline' cmd 'autocmd!' - for _, event in ipairs(M.events.reload) do - cmd(string.format('autocmd %s * lua require("hardline").reload()', event)) - end - for _, event in ipairs(M.events.unload) do - cmd(string.format('autocmd %s * lua require("hardline").unload()', event)) + for mode, events in pairs(M.events) do + for _, event in ipairs(events) do + local raw_str = 'autocmd %s * lua require("hardline").reload(%s)' + cmd(string.format(raw_str, event, mode == 'active')) + end end cmd 'augroup END' end +function M.setup(user_opts) + set_hlgroups() + set_autocmds() +end + ------------------------------------------------------------ return M diff --git a/lua/hardline/filename.lua b/lua/hardline/filename.lua index e1b021c..ab40581 100644 --- a/lua/hardline/filename.lua +++ b/lua/hardline/filename.lua @@ -2,7 +2,7 @@ local fn, vim = vim.fn, vim local M = {} function M.get_mode() - return '%h%r' + return ' %h%r' end function M.get_name() @@ -12,7 +12,7 @@ end function M.get_item() return table.concat({ '%<', - [[%{luaeval('require("hardline.filename").get_name()')}]], ' ', + [[%{luaeval('require("hardline.filename").get_name()')}]], M.get_mode(), }) end diff --git a/lua/hardline/git.lua b/lua/hardline/git.lua index c45a5cc..32c7d02 100644 --- a/lua/hardline/git.lua +++ b/lua/hardline/git.lua @@ -18,12 +18,12 @@ function M.get_branch() if not g.loaded_gitgutter then return '' end - return string.format('%s', fn.FugitiveHead()) + return string.format(' %s', fn.FugitiveHead()) end function M.get_item() return table.concat({ - [[%{luaeval('require("hardline.git").get_hunks()')}]], ' ', + [[%{luaeval('require("hardline.git").get_hunks()')}]], [[%{luaeval('require("hardline.git").get_branch()')}]], }) end diff --git a/lua/hardline/line.lua b/lua/hardline/line.lua index 154eaa0..710392e 100644 --- a/lua/hardline/line.lua +++ b/lua/hardline/line.lua @@ -10,13 +10,13 @@ function M.get_column() end function M.get_percent() - return '%03p%%' + return ' %03p%%' end function M.get_item() return table.concat({ [[%{luaeval('require("hardline.line").get_line()')}]], ':', - [[%{luaeval('require("hardline.line").get_column()')}]], ' ', + [[%{luaeval('require("hardline.line").get_column()')}]], M.get_percent(), }) end diff --git a/lua/hardline/mode.lua b/lua/hardline/mode.lua index 0e33701..1f0d1c4 100644 --- a/lua/hardline/mode.lua +++ b/lua/hardline/mode.lua @@ -22,24 +22,20 @@ function M.get_mode() end function M.get_paste() - if not o.paste then - return '' - end - return 'PASTE' + if not o.paste then return '' end + return ' PASTE' end function M.get_spell() - if not wo.spell then - return '' - end - return string.format('SPELL [%s]', string.upper(bo.spelllang)) + if not wo.spell then return '' end + return string.format(' SPELL [%s]', string.upper(bo.spelllang)) end function M.get_item() return table.concat({ - [[%{luaeval('require("hardline.mode").get_mode()')}]], ' ', - [[%{luaeval('require("hardline.mode").get_paste()')}]], ' ', - [[%{luaeval('require("hardline.mode").get_spell()')}]], ' ' + [[%{luaeval('require("hardline.mode").get_mode()')}]], + [[%{luaeval('require("hardline.mode").get_paste()')}]], + [[%{luaeval('require("hardline.mode").get_spell()')}]], }) end diff --git a/plugin/hardline.vim b/plugin/hardline.vim index 1b79394..5b3cc1c 100644 --- a/plugin/hardline.vim +++ b/plugin/hardline.vim @@ -6,6 +6,6 @@ if exists('g:loaded_hardline') finish endif -lua require('hardline').autocommands() +lua require('hardline').setup() let g:loaded_hardline = 1