Add color support

This commit is contained in:
Olivier Roques
2020-12-23 14:59:27 +01:00
parent 1d80803552
commit a4b30c9ee4
6 changed files with 64 additions and 34 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -6,6 +6,6 @@ if exists('g:loaded_hardline')
finish
endif
lua require('hardline').autocommands()
lua require('hardline').setup()
let g:loaded_hardline = 1