diff --git a/lua/hardline.lua b/lua/hardline.lua index 0bd62cd..dfcf44a 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -3,11 +3,13 @@ -- github.com/ojroques -------------------- VARIABLES ----------------------------- -local cmd, vim = vim.cmd, vim +local api, cmd, vim = vim.api, vim.cmd, vim local wo = vim.wo +local common = require('hardline.common') local M = {} -------------------- OPTIONS ------------------------------- +M.theme = 'default' M.events = { active = { 'BufEnter', @@ -23,75 +25,83 @@ M.events = { 'WinLeave', }, } - M.sections = { - {class = 'A', item = require('hardline.mode').get_item()}, ' ', - {class = 'B', item = require('hardline.git').get_item()}, ' ', - {class = 'C', item = require('hardline.filename').get_item()}, ' ', + { + class = nil, + item = require('hardline.parts.mode').get_item() + }, ' ', + { + class = 'B', + item = require('hardline.parts.git').get_item() + }, ' ', + { + class = 'C', + item = require('hardline.parts.filename').get_item() + }, ' ', '%=', - -- {class = 'error', item = require('hardline.lsp').get_error()}, ' ', - -- {class = 'warning', item = require('hardline.lsp').get_warning()}, ' ', - -- {class = 'warning', item = require('hardline.whitespace').get_item()}, ' ', - -- {class = 'X', item = require('hardline.wordcount').get_item()}, ' ', - {class = 'Y', item = require('hardline.filetype').get_item()}, ' ', - {class = 'Z', item = require('hardline.line').get_item()}, -} - -M.colors = { - active = { - A = {ctermfg='170',guifg='#C678DD'}, - }, - inactive = { - A = {ctermfg='170',guifg='#C678DD'}, + -- { + -- class = 'Error', + -- item = require('hardline.parts.lsp').get_errors() + -- }, ' ', + -- { + -- class = 'Warning', + -- item = require('hardline.parts.lsp').get_warnings() + -- }, ' ', + -- { + -- class = 'Warning', + -- item = require('hardline.parts.whitespace').get_item() + -- }, ' ', + -- { + -- class = 'X', + -- item = require('hardline.parts.wordcount').get_item() + -- }, ' ', + { + class = 'Y', + item = require('hardline.parts.filetype').get_item() + }, ' ', + { + class = 'A', + item = require('hardline.parts.line').get_item() }, } --------------------- HELPERS ------------------------------- -local function echo(hlgroup, msg) - cmd(string.format('echohl %s', hlgroup)) - cmd(string.format('echo "[hardline] %s"', msg)) - cmd('echohl None') -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) +local function color_item(item, class) + local mode = common.is_active() and 'active' or 'inactive' + if not class or not common.theme.colors[class] then return item end + return string.format('%%#Hardline%s%s#%s%%##', class, mode, item) end -local function reload_section(section, active) +local function reload_section(section) if type(section) == 'function' then return section() elseif type(section) == 'string' then return section elseif type(section) == 'table' then - return color_item(section.item, section.class, active) + return color_item(reload_section(section.item), section.class) end - echo('WarningMsg', 'Invalid section.') + common.echo('WarningMsg', 'Invalid section.') return '' end 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) + common.set_active(active) + wo.statusline = table.concat(vim.tbl_map(reload_section, M.sections)) end -------------------- 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, ' '))) + for class, attr in pairs(common.theme.colors) do + for mode, args in pairs(attr) do + local a = common.build_args(args) + cmd(string.format('hi Hardline%s%s %s', class, mode, a)) end end + for mode, args in pairs(common.theme.mode_colors) do + local a = common.build_args(args) + cmd(string.format('hi HardlineMode%s %s', mode, a)) + end end local function set_autocmds() @@ -107,6 +117,7 @@ local function set_autocmds() end function M.setup(user_opts) + common.theme = require(string.format('hardline.themes.%s', M.theme)) set_hlgroups() set_autocmds() end diff --git a/lua/hardline/common.lua b/lua/hardline/common.lua new file mode 100644 index 0000000..af75c4e --- /dev/null +++ b/lua/hardline/common.lua @@ -0,0 +1,27 @@ +local M = {} + +M.theme = require('hardline.themes.default') + +function M.echo(hlgroup, msg) + vim.cmd(string.format('echohl %s', hlgroup)) + vim.cmd(string.format('echo "[hardline] %s"', msg)) + vim.cmd('echohl None') +end + +function M.build_args(tbl_args) + local args = {} + for k, v in pairs(tbl_args) do + table.insert(args, string.format('%s=%s', k, v)) + end + return table.concat(args, ' ') +end + +function M.set_active(active) + vim.api.nvim_win_set_var(0, 'hardline_active', active) +end + +function M.is_active() + return vim.api.nvim_win_get_var(0, 'hardline_active') +end + +return M diff --git a/lua/hardline/filetype.lua b/lua/hardline/filetype.lua deleted file mode 100644 index c5daab7..0000000 --- a/lua/hardline/filetype.lua +++ /dev/null @@ -1,15 +0,0 @@ -local vim = vim -local bo = vim.bo -local M = {} - -function M.get_filetype() - return bo.filetype -end - -function M.get_item() - return table.concat({ - [[%{luaeval('require("hardline.filetype").get_filetype()')}]], - }) -end - -return M diff --git a/lua/hardline/git.lua b/lua/hardline/git.lua deleted file mode 100644 index 32c7d02..0000000 --- a/lua/hardline/git.lua +++ /dev/null @@ -1,31 +0,0 @@ -local fn, vim = vim.fn, vim -local g = vim.g -local M = {} - -function M.get_hunks() - if not g.loaded_gitgutter then - return '' - end - local summary = fn.GitGutterGetHunkSummary() - return table.concat({ - string.format('+%d', summary[1]), - string.format('~%d', summary[2]), - string.format('-%d', summary[3]), - }, ' ') -end - -function M.get_branch() - if not g.loaded_gitgutter then - return '' - end - 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_branch()')}]], - }) -end - -return M diff --git a/lua/hardline/line.lua b/lua/hardline/line.lua deleted file mode 100644 index 710392e..0000000 --- a/lua/hardline/line.lua +++ /dev/null @@ -1,24 +0,0 @@ -local cmd, fn, vim = vim.cmd, vim.fn, vim -local M = {} - -function M.get_line() - return string.format('%03d/%03d', fn.line('.'), fn.line('$')) -end - -function M.get_column() - return string.format('%03d/%03d', fn.col('.'), fn.col('$') - 1) -end - -function M.get_percent() - return ' %03p%%' -end - -function M.get_item() - return table.concat({ - [[%{luaeval('require("hardline.line").get_line()')}]], ':', - [[%{luaeval('require("hardline.line").get_column()')}]], - M.get_percent(), - }) -end - -return M diff --git a/lua/hardline/lsp.lua b/lua/hardline/lsp.lua deleted file mode 100644 index 7b0a748..0000000 --- a/lua/hardline/lsp.lua +++ /dev/null @@ -1,8 +0,0 @@ -local fn, vim = vim.fn, vim -local M = {} - -function M.get_item() - return table.concat({}) -end - -return M diff --git a/lua/hardline/mode.lua b/lua/hardline/mode.lua deleted file mode 100644 index 1f0d1c4..0000000 --- a/lua/hardline/mode.lua +++ /dev/null @@ -1,42 +0,0 @@ -local fn, vim = vim.fn, vim -local o, bo, wo = vim.o, vim.bo, vim.wo -local modes = { - ['?'] = '???', - ['n'] = 'NORMAL', - ['i'] = 'INSERT', - ['R'] = 'REPLACE', - ['v'] = 'VISUAL', - ['V'] = 'V-LINE', - [''] = 'V-BLOCK', - ['c'] = 'COMMAND', - ['s'] = 'SELECT', - ['S'] = 'S-LINE', - [''] = 'S-BLOCK', - ['t'] = 'TERMINAL', -} -local M = {} - -function M.get_mode() - if not modes[fn.mode()] then return modes['?'] end - return modes[fn.mode()] -end - -function M.get_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)) -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()')}]], - }) -end - -return M diff --git a/lua/hardline/filename.lua b/lua/hardline/parts/filename.lua similarity index 59% rename from lua/hardline/filename.lua rename to lua/hardline/parts/filename.lua index ab40581..336f160 100644 --- a/lua/hardline/filename.lua +++ b/lua/hardline/parts/filename.lua @@ -1,18 +1,17 @@ -local fn, vim = vim.fn, vim local M = {} +function M.get_name() + return vim.fn.expand('%:~:.') +end + function M.get_mode() return ' %h%r' end -function M.get_name() - return fn.expand('%:~:.') -end - function M.get_item() return table.concat({ '%<', - [[%{luaeval('require("hardline.filename").get_name()')}]], + [[%{luaeval('require("hardline.parts.filename").get_name()')}]], M.get_mode(), }) end diff --git a/lua/hardline/parts/filetype.lua b/lua/hardline/parts/filetype.lua new file mode 100644 index 0000000..6584f67 --- /dev/null +++ b/lua/hardline/parts/filetype.lua @@ -0,0 +1,13 @@ +local M = {} + +function M.get_filetype() + return vim.bo.filetype +end + +function M.get_item() + return table.concat({ + [[%{luaeval('require("hardline.parts.filetype").get_filetype()')}]], + }) +end + +return M diff --git a/lua/hardline/parts/git.lua b/lua/hardline/parts/git.lua new file mode 100644 index 0000000..6251c36 --- /dev/null +++ b/lua/hardline/parts/git.lua @@ -0,0 +1,25 @@ +local M = {} + +function M.get_hunks() + if not vim.g.loaded_gitgutter then return '' end + local summary = vim.fn.GitGutterGetHunkSummary() + return table.concat({ + string.format('+%d', summary[1]), + string.format('~%d', summary[2]), + string.format('-%d', summary[3]), + }, ' ') +end + +function M.get_branch() + if not vim.g.loaded_gitgutter then return '' end + return string.format(' %s', vim.fn.FugitiveHead()) +end + +function M.get_item() + return table.concat({ + [[%{luaeval('require("hardline.parts.git").get_hunks()')}]], + [[%{luaeval('require("hardline.parts.git").get_branch()')}]], + }) +end + +return M diff --git a/lua/hardline/parts/line.lua b/lua/hardline/parts/line.lua new file mode 100644 index 0000000..8c346fe --- /dev/null +++ b/lua/hardline/parts/line.lua @@ -0,0 +1,23 @@ +local M = {} + +function M.get_line() + return string.format('%03d/%03d', vim.fn.line('.'), vim.fn.line('$')) +end + +function M.get_column() + return string.format('%03d/%03d', vim.fn.col('.'), vim.fn.col('$') - 1) +end + +function M.get_percent() + return ' %03p%%' +end + +function M.get_item() + return table.concat({ + [[%{luaeval('require("hardline.parts.line").get_line()')}]], ':', + [[%{luaeval('require("hardline.parts.line").get_column()')}]], + M.get_percent(), + }) +end + +return M diff --git a/lua/hardline/parts/lsp.lua b/lua/hardline/parts/lsp.lua new file mode 100644 index 0000000..ce84ef3 --- /dev/null +++ b/lua/hardline/parts/lsp.lua @@ -0,0 +1,8 @@ +local M = {} + +function M.get_item() + return table.concat({ + }) +end + +return M diff --git a/lua/hardline/parts/mode.lua b/lua/hardline/parts/mode.lua new file mode 100644 index 0000000..c93afa3 --- /dev/null +++ b/lua/hardline/parts/mode.lua @@ -0,0 +1,47 @@ +local M = {} +local modes = { + ['?'] = {text = '???', color = 'inactive'}, + ['n'] = {text = 'NORMAL', color = 'normal'}, + ['c'] = {text = 'COMMAND', color = 'normal'}, + ['i'] = {text = 'INSERT', color = 'insert'}, + ['R'] = {text = 'REPLACE', color = 'replace'}, + ['v'] = {text = 'VISUAL', color = 'visual'}, + ['V'] = {text = 'V-LINE', color = 'visual'}, + [''] = {text = 'V-BLOCK', color = 'visual'}, + ['s'] = {text = 'SELECT', color = 'visual'}, + ['S'] = {text = 'S-LINE', color = 'visual'}, + [''] = {text = 'S-BLOCK', color = 'visual'}, + ['t'] = {text = 'TERMINAL', color = 'normal'}, +} + +local function color_item(item) + local color = modes[vim.fn.mode()].color + return string.format('%%#HardlineMode%s#%s%%##', color, item) +end + +function M.get_mode() + if not modes[vim.fn.mode()] then + return modes['?'].text + end + return modes[vim.fn.mode()].text +end + +function M.get_paste() + if not vim.o.paste then return '' end + return ' | PASTE' +end + +function M.get_spell() + if not vim.wo.spell then return '' end + return string.format(' | SPELL [%s]', string.upper(vim.bo.spelllang)) +end + +function M.get_item() + return color_item(table.concat({ + [[%{luaeval('require("hardline.parts.mode").get_mode()')}]], + [[%{luaeval('require("hardline.parts.mode").get_paste()')}]], + [[%{luaeval('require("hardline.parts.mode").get_spell()')}]], + })) +end + +return M diff --git a/lua/hardline/parts/whitespace.lua b/lua/hardline/parts/whitespace.lua new file mode 100644 index 0000000..ce84ef3 --- /dev/null +++ b/lua/hardline/parts/whitespace.lua @@ -0,0 +1,8 @@ +local M = {} + +function M.get_item() + return table.concat({ + }) +end + +return M diff --git a/lua/hardline/parts/wordcount.lua b/lua/hardline/parts/wordcount.lua new file mode 100644 index 0000000..ce84ef3 --- /dev/null +++ b/lua/hardline/parts/wordcount.lua @@ -0,0 +1,8 @@ +local M = {} + +function M.get_item() + return table.concat({ + }) +end + +return M diff --git a/lua/hardline/themes/default.lua b/lua/hardline/themes/default.lua new file mode 100644 index 0000000..cd01b16 --- /dev/null +++ b/lua/hardline/themes/default.lua @@ -0,0 +1,18 @@ +local M = {} + +M.colors = { + A = { + active = {ctermfg='170',guifg='#C678DD'}, + inactive = {ctermfg='172',guifg='#C6080D'}, + } +} + +M.mode_colors = { + inactive = {ctermfg='170',guifg='#C678DD'}, + normal = {ctermfg='170',guifg='#C678DD'}, + insert = {ctermfg='170',guifg='#C678DD'}, + replace = {ctermfg='170',guifg='#C678DD'}, + visual = {ctermfg='170',guifg='#C678DD'}, +} + +return M diff --git a/lua/hardline/themes/one.lua b/lua/hardline/themes/one.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/hardline/whitespace.lua b/lua/hardline/whitespace.lua deleted file mode 100644 index 7b0a748..0000000 --- a/lua/hardline/whitespace.lua +++ /dev/null @@ -1,8 +0,0 @@ -local fn, vim = vim.fn, vim -local M = {} - -function M.get_item() - return table.concat({}) -end - -return M diff --git a/lua/hardline/wordcount.lua b/lua/hardline/wordcount.lua deleted file mode 100644 index 7b0a748..0000000 --- a/lua/hardline/wordcount.lua +++ /dev/null @@ -1,8 +0,0 @@ -local fn, vim = vim.fn, vim -local M = {} - -function M.get_item() - return table.concat({}) -end - -return M