From 778b6b12198726a3d0c3e9f79722e92862e5b801 Mon Sep 17 00:00:00 2001 From: Olivier Roques Date: Tue, 22 Dec 2020 17:50:10 +0100 Subject: [PATCH] Add some components: git file and mode --- lua/hardline.lua | 71 +++++++++++++++++++++++++++++++++++++++++++ lua/hardline/file.lua | 19 ++++++++++++ lua/hardline/git.lua | 28 +++++++++++++++++ lua/hardline/mode.lua | 46 ++++++++++++++++++++++++++++ plugin/hardline.vim | 2 ++ 5 files changed, 166 insertions(+) create mode 100644 lua/hardline/file.lua create mode 100644 lua/hardline/git.lua create mode 100644 lua/hardline/mode.lua diff --git a/lua/hardline.lua b/lua/hardline.lua index 86db801..96be2a7 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -1,3 +1,74 @@ -- nvim-hardline -- By Olivier Roques -- github.com/ojroques + +-------------------- VARIABLES ----------------------------- +local cmd, vim = vim.cmd, vim +local wo = vim.wo +local M = {} + +M.events = { + reload = { + 'BufEnter', + 'BufReadPost', + 'BufWinEnter', + 'BufWritePost', + 'FileChangedShellPost', + 'FileType', + 'VimResized', + 'WinEnter', + }, + unload = { + 'WinLeave', + }, +} + +M.sections = { + require('hardline.mode'), ' ', + require('hardline.git'), ' ', + require('hardline.file'), ' ', +} + +-------------------- HELPERS ------------------------------- +local function echo(hlgroup, msg) + cmd(string.format('echohl %s', hlgroup)) + cmd(string.format('echo "[hardline] %s"', msg)) + cmd('echohl None') +end + +local function load_section(section) + if not section then return end + if type(section) == 'function' then + return section() + elseif type(section) == 'string' then + return section + elseif type(section) == 'table' then + return load_section(section.get_component) + end + echo('WarningMsg', 'Section must be a function or a string!') + return '' +end + +-------------------- INTERFACE ----------------------------- +function M.unload() + wo.statusline = table.concat(vim.tbl_map(load_section, M.sections)) +end + +function M.reload() + wo.statusline = table.concat(vim.tbl_map(load_section, M.sections)) +end + +function M.autocommands() + 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)) + end + cmd 'augroup END' +end + +------------------------------------------------------------ +return M diff --git a/lua/hardline/file.lua b/lua/hardline/file.lua new file mode 100644 index 0000000..6caa21c --- /dev/null +++ b/lua/hardline/file.lua @@ -0,0 +1,19 @@ +local fn, vim = vim.fn, vim +local M = {} + +function M.get_mode() + return ' %h%r' +end + +function M.get_name() + return fn.expand('%:~:.') +end + +function M.get_component() + return table.concat({ + [[%{luaeval('require("hardline.file").get_name()')}]], + M.get_mode(), + }) +end + +return M diff --git a/lua/hardline/git.lua b/lua/hardline/git.lua new file mode 100644 index 0000000..bdeeec1 --- /dev/null +++ b/lua/hardline/git.lua @@ -0,0 +1,28 @@ +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() + return '' +end + +function M.get_component() + return table.concat({ + [[%{luaeval('require("hardline.git").get_hunks()')}]], + [[%{luaeval('require("hardline.git").get_branch()')}]], + }) +end + +return M diff --git a/lua/hardline/mode.lua b/lua/hardline/mode.lua new file mode 100644 index 0000000..de00151 --- /dev/null +++ b/lua/hardline/mode.lua @@ -0,0 +1,46 @@ +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_component() + 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/plugin/hardline.vim b/plugin/hardline.vim index c24cddc..1b79394 100644 --- a/plugin/hardline.vim +++ b/plugin/hardline.vim @@ -6,4 +6,6 @@ if exists('g:loaded_hardline') finish endif +lua require('hardline').autocommands() + let g:loaded_hardline = 1