diff --git a/lua/hardline.lua b/lua/hardline.lua index ea2bc97..a09d665 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -3,36 +3,38 @@ -- github.com/ojroques -------------------- VARIABLES ----------------------------- -local api, cmd, fn, vim = vim.api, vim.cmd, vim.fn, vim -local wo = vim.wo +local cmd, fn, vim = vim.cmd, vim.fn, vim +local o, wo = vim.wo, vim.o local common = require('hardline.common') local M = {} -------------------- OPTIONS ------------------------------- M.options = { theme = 'one', - events = { - active = { - 'WinEnter', - }, - inactive = { - 'WinLeave', - }, - }, sections = { {class = 'mode', item = require('hardline.parts.mode').get_item}, {class = 'high', item = require('hardline.parts.git').get_item}, {class = 'med', item = require('hardline.parts.filename').get_item}, - '%=', + {class = 'med', item ='%='}, {class = 'high', item = require('hardline.parts.filetype').get_item}, {class = 'mode', item = require('hardline.parts.line').get_item}, }, } -------------------- STATUSLINE ---------------------------- -local function color_item(class, mode, item) - if not class or not mode then return item end - local hlgroup = string.format('Hardline_%s_%s', class, mode) +local function get_state(class) + if class == 'mode' and common.is_active() then + local mode = common.modes[vim.fn.mode()] + if not mode then return common.modes['?'].color end + return mode.color + end + return common.is_active() and 'active' or 'inactive' +end + +local function color_item(class, item) + if not class then return item end + local state = get_state(class) + local hlgroup = string.format('Hardline_%s_%s', class, state) if fn.hlexists(hlgroup) == 0 then return item end return string.format('%%#%s#%s%%*', hlgroup, item) end @@ -42,9 +44,8 @@ local function update_section(section) return section() elseif type(section) == 'string' then return section - elseif type(section) == 'table' and type(section.item) == 'function' then - local item = section.item() - return color_item(section.class, item.mode, item.text) + elseif type(section) == 'table' then + return color_item(section.class, update_section(section.item)) end common.echo('WarningMsg', 'Invalid section.') return '' @@ -55,49 +56,35 @@ function M.update() end -------------------- SETUP ----------------------------- -local function set_autocmds() - cmd 'augroup hardline' - cmd 'autocmd!' - for mode, events in pairs(M.options.events) do - for _, event in ipairs(events) do - local raw_str = 'autocmd %s * lua require("hardline").set_statusline()' - cmd(string.format(raw_str, event)) - end - end - cmd 'augroup END' +local function set_theme() + if type(M.options.theme) ~= 'string' then return end + local theme = string.format('hardline.themes.%s', M.options.theme) + M.options.theme = require(theme) end local function set_hlgroups() for class, attr in pairs(M.options.theme) do - for mode, args in pairs(attr) do + for state, args in pairs(attr) do local a = {} for k, v in pairs(args) do table.insert(a, string.format('%s=%s', k, v)) end a = table.concat(a, ' ') - cmd(string.format('hi Hardline_%s_%s %s', class, mode, a)) + cmd(string.format('hi Hardline_%s_%s %s', class, state, a)) end end end -local function set_theme() - if type(M.options.theme) == 'string' then - local theme = require(string.format('hardline.themes.%s', M.options.theme)) - M.options.theme = theme - end -end - -function M.set_statusline() - common.set_active('active') - wo.statusline = [[%!luaeval('require("hardline").update()')]] +local function set_statusline() + o.statusline = [[%!luaeval('require("hardline").update()')]] + wo.statusline = o.statusline end function M.setup(user_options) M.options = vim.tbl_extend('force', M.options, user_options) set_theme() set_hlgroups() - set_autocmds() - M.set_statusline() + set_statusline() end ------------------------------------------------------------ diff --git a/lua/hardline/common.lua b/lua/hardline/common.lua index 973c802..60f9f39 100644 --- a/lua/hardline/common.lua +++ b/lua/hardline/common.lua @@ -1,18 +1,18 @@ -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 M = { + 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'}, + } } function M.echo(hlgroup, msg) @@ -21,21 +21,8 @@ function M.echo(hlgroup, msg) vim.cmd('echohl None') end -function M.set_active(active) - vim.api.nvim_win_set_var(0, 'hardline_active', active) -end - -function M.get_active() - return vim.api.nvim_win_get_var(0, 'hardline_active') -end - function M.is_active() - return M.get_active() == 'active' -end - -function M.get_mode() - if not modes[vim.fn.mode()] then return modes['?'] end - return modes[vim.fn.mode()] + return vim.g.statusline_winid == vim.fn.win_getid() end return M diff --git a/lua/hardline/parts/filename.lua b/lua/hardline/parts/filename.lua index 11aa4e2..7d44e31 100644 --- a/lua/hardline/parts/filename.lua +++ b/lua/hardline/parts/filename.lua @@ -1,16 +1,8 @@ -local common = require('hardline.common') - -local function get_name() - return vim.fn.expand('%:~:.') -end - local function get_item() - return { - text = table.concat({' %<', get_name()}), - mode = common.get_active(), - } + local name = vim.fn.expand('%:~:.') + return table.concat({' ', '%<', name, ' '}) end return { - get_item = get_item, + get_item = get_item(), } diff --git a/lua/hardline/parts/filetype.lua b/lua/hardline/parts/filetype.lua index 6162579..427c1ba 100644 --- a/lua/hardline/parts/filetype.lua +++ b/lua/hardline/parts/filetype.lua @@ -1,14 +1,9 @@ -local common = require('hardline.common') - local function get_filetype() - return string.format(' %s ', vim.bo.filetype) + return string.format('%s', vim.bo.filetype) end local function get_item() - return { - text = get_filetype(), - mode = common.get_active(), - } + return table.concat({' ', get_filetype(), ' '}) end return { diff --git a/lua/hardline/parts/git.lua b/lua/hardline/parts/git.lua index f6d3fe0..fcd2252 100644 --- a/lua/hardline/parts/git.lua +++ b/lua/hardline/parts/git.lua @@ -1,10 +1,7 @@ -local common = require('hardline.common') - local function 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]), ' ', @@ -13,14 +10,11 @@ end local function get_branch() if not vim.g.loaded_gitgutter then return '' end - return string.format('(%s) ', vim.fn.FugitiveHead()) + return string.format('(%s)', vim.fn.FugitiveHead()) end local function get_item() - return { - text = table.concat({get_hunks(), get_branch()}), - mode = common.get_active(), - } + return table.concat({' ', get_hunks(), get_branch(), ' '}) end return { diff --git a/lua/hardline/parts/line.lua b/lua/hardline/parts/line.lua index e101b2d..160829e 100644 --- a/lua/hardline/parts/line.lua +++ b/lua/hardline/parts/line.lua @@ -8,33 +8,29 @@ end local function get_line() local nb_lines = vim.fn.line('$') - local current_line = vim.fn.line('.') - local dots = get_dots(current_line, nb_lines) - return string.format(' %s%d/%d', dots, current_line, nb_lines) + local line = vim.fn.line('.') + local dots = get_dots(line, nb_lines) + return string.format('%s%d/%d', dots, line, nb_lines) end local function get_column() local nb_columns = vim.fn.col('$') - local current_column = vim.fn.col('.') - local dots = get_dots(current_column, 999) + local column = vim.fn.col('.') local max_dots = get_dots(nb_columns, 999) - return string.format('|%s%d/%s%d', dots, current_column, max_dots, nb_columns) + local dots = get_dots(column, 999) + return string.format('|%s%d/%s%d', dots, column, max_dots, nb_columns) end local function get_percent() local nb_lines = vim.fn.line('$') - local current_line = vim.fn.line('.') - local percent = math.floor(current_line * 100 / nb_lines) + local line = vim.fn.line('.') + local percent = math.floor(line * 100 / nb_lines) local dots = get_dots(percent, 100) - return string.format('|%s%d%%%% ', dots, percent) + return string.format('|%s%d%%%%', dots, percent) end local function get_item() - local mode = common.get_mode() - return { - text = table.concat({get_line(), get_column(), get_percent()}), - mode = mode.color, - } + return table.concat({' ', get_line(), get_column(), get_percent(), ' '}) end return { diff --git a/lua/hardline/parts/mode.lua b/lua/hardline/parts/mode.lua index b3567aa..0eee386 100644 --- a/lua/hardline/parts/mode.lua +++ b/lua/hardline/parts/mode.lua @@ -1,5 +1,9 @@ local common = require('hardline.common') +local function get_mode() + return common.modes[vim.fn.mode()].text +end + local function get_paste() if not vim.o.paste then return '' end return '|PASTE' @@ -11,17 +15,7 @@ local function get_spell() end local function get_item() - local mode = common.get_mode() - return { - text = table.concat({ - ' ', - string.format('%s', mode.text), - get_paste(), - get_spell(), - ' ' - }), - mode = mode.color, - } + return table.concat({' ', get_mode(), get_paste(), get_spell(), ' ' }) end return { diff --git a/lua/hardline/themes/one.lua b/lua/hardline/themes/one.lua index 46fe935..0200480 100644 --- a/lua/hardline/themes/one.lua +++ b/lua/hardline/themes/one.lua @@ -6,18 +6,21 @@ local colors = { purple = {gui = '#C678DD', cterm = '170', cterm16 = '5'}, white = {gui = '#ABB2BF', cterm = '145', cterm16 = '7'}, black = {gui = '#282C34', cterm = '235', cterm16 = '0'}, - visual_grey = {gui = "#3E4452", cterm = "237", cterm16 = "15"}, + comment_grey = {gui = "#5C6370", cterm = "59", cterm16 = "15"}, cursor_grey = {gui = "#2C323C", cterm = "236", cterm16 = "8"}, + menu_grey = {gui = "#3E4452", cterm = "237", cterm16 = "8"}, +} + +local inactive = { + guifg = colors.comment_grey.gui, + guibg = colors.cursor_grey.gui, + ctermfg = colors.comment_grey.cterm, + ctermbg = colors.cursor_grey.cterm, } return { mode = { - inactive = { - guifg = colors.white.gui, - guibg = colors.visual_grey.gui, - ctermfg = colors.white.cterm, - ctermbg = colors.visual_grey.cterm, - }, + inactive = inactive, normal = { guifg = colors.black.gui, guibg = colors.green.gui, @@ -44,18 +47,8 @@ return { }, }, low = { - active = { - guifg = colors.white.gui, - guibg = colors.cursor_grey.gui, - ctermfg = colors.white.cterm, - ctermbg = colors.cursor_grey.cterm, - }, - inactive = { - guifg = colors.visual_grey.gui, - guibg = colors.black.gui, - ctermfg = colors.visual_grey.cterm, - ctermbg = colors.black.cterm, - }, + active = inactive, + inactive = inactive, }, med = { active = { @@ -64,26 +57,16 @@ return { ctermfg = colors.white.cterm, ctermbg = colors.cursor_grey.cterm, }, - inactive = { - guifg = colors.visual_grey.gui, - guibg = colors.black.gui, - ctermfg = colors.visual_grey.cterm, - ctermbg = colors.black.cterm, - }, + inactive = inactive, }, high = { active = { guifg = colors.white.gui, - guibg = colors.visual_grey.gui, + guibg = colors.menu_grey.gui, ctermfg = colors.white.cterm, - ctermbg = colors.visual_grey.cterm, - }, - inactive = { - guifg = colors.visual_grey.gui, - guibg = colors.black.gui, - ctermfg = colors.visual_grey.cterm, - ctermbg = colors.black.cterm, + ctermbg = colors.menu_grey.cterm, }, + inactive = inactive, }, error = { active = { @@ -92,12 +75,7 @@ return { ctermfg = colors.black.cterm, ctermbg = colors.red.cterm, }, - inactive = { - guifg = colors.black.gui, - guibg = colors.white.gui, - ctermfg = colors.black.cterm, - ctermbg = colors.white.cterm, - }, + inactive = inactive, }, warning = { active = { @@ -106,11 +84,6 @@ return { ctermfg = colors.black.cterm, ctermbg = colors.yellow.cterm, }, - inactive = { - guifg = colors.visual_grey.gui, - guibg = colors.white.gui, - ctermfg = colors.visual_grey.cterm, - ctermbg = colors.white.cterm, - }, + inactive = inactive, }, }