From 57e115339d00a2a8afe09dba6f83009cc5a02e99 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Mon, 10 Apr 2023 16:38:26 -0300 Subject: nvim update, semi-refactor --- .config/nvim/lua/statusline.lua | 178 +++++++++++----------------------------- 1 file changed, 49 insertions(+), 129 deletions(-) (limited to '.config/nvim/lua/statusline.lua') diff --git a/.config/nvim/lua/statusline.lua b/.config/nvim/lua/statusline.lua index b7b6ed9..e91ac49 100644 --- a/.config/nvim/lua/statusline.lua +++ b/.config/nvim/lua/statusline.lua @@ -1,14 +1,8 @@ -local fn = vim.fn local api = vim.api --- possible values are 'arrow' | 'rounded' | 'blank' -local active_sep = 'blank' - --- change them if you want to different separator -local separators = { - arrow = { '', '' }, - rounded = { '', '' }, - blank = { '', '' }, +Statusline = { + active = '', + inactive = '', } -- highlight groups @@ -25,74 +19,7 @@ local colors = { line_col_alt = '%#LineColAlt#', } -local trunc_width = setmetatable({ - mode = 80, - git_status = 90, - filename = 140, - line_col = 60, -}, { - __index = function() - return 80 - end -}) - -local is_truncated = function(_, width) - local current_width = api.nvim_win_get_width(0) - return current_width < width -end - ---local modes = setmetatable({ - --['n'] = {'Normal', 'N'}; - --['no'] = {'N·Pending', 'N·P'} ; - --['v'] = {'Visual', 'V' }; - --['V'] = {'V·Line', 'V·L' }; - --[''] = {'V·Block', 'V·B'}; -- this is not ^V, but it's , they're different - --['s'] = {'Select', 'S'}; - --['S'] = {'S·Line', 'S·L'}; - --[''] = {'S·Block', 'S·B'}; -- same with this one, it's not ^S but it's  - --['i'] = {'Insert', 'I'}; - --['ic'] = {'Insert', 'I'}; - --['R'] = {'Replace', 'R'}; - --['Rv'] = {'V·Replace', 'V·R'}; - --['c'] = {'Command', 'C'}; - --['cv'] = {'Vim·Ex ', 'V·E'}; - --['ce'] = {'Ex ', 'E'}; - --['r'] = {'Prompt ', 'P'}; - --['rm'] = {'More ', 'M'}; - --['r?'] = {'Confirm ', 'C'}; - --['!'] = {'Shell ', 'S'}; - --['t'] = {'Terminal ', 'T'}; ---}, { - --__index = function() - --return {'Unknown', 'U'} -- handle edge cases - --end ---}) - -local get_current_mode = function() - return string.format('[%s]', api.nvim_get_mode().mode ) -end - -local get_git_status = function() - -- use fallback because it doesn't set this variable on the initial `BufEnter` - local signs = vim.b.gitsigns_status_dict or {head = '', added = 0, changed = 0, removed = 0} - local is_head_empty = signs.head ~= '' - - if is_truncated(trunc_width.git_status) then - return is_head_empty and string.format('  %s ', signs.head or '') or '' - end - - return is_head_empty and string.format( - ' +%s ~%s -%s |  %s ', - signs.added, signs.changed, signs.removed, signs.head - ) or '' -end - -local get_filename = function() - --if is_truncated(trunc_width.filename) then return "%<%f" end - return "%<%F" -end - -local get_filetype = function() +Statusline.get_filetype = function() local filetype = vim.bo.filetype if filetype == '' then return '' @@ -100,72 +27,50 @@ local get_filetype = function() return string.format('%s', filetype):lower() end -local get_line_col = function() - return '%l:%c' +local add_active = function(text) + Statusline.active = Statusline.active .. text end -Statusline = {} - -Statusline.active = function() - local navic = require('nvim-navic') - - --local mode = colors.mode .. self:get_current_mode() - --local mode_alt = colors.mode_alt .. self.separators[active_sep][1] - --local git = colors.git .. self:get_git_status() - --local git_alt = colors.git_alt .. self.separators[active_sep][1] - --local filename = colors.inactive .. self:get_filename() .. colors.git_alt - --local location = "" - --if navic.is_available() then - --location = navic.get_location() - --end - --local filetype_alt = colors.filetype_alt .. self.separators[active_sep][2] - --local filetype = colors.filetype .. self:get_filetype() - --local line_col = colors.line_col .. self:get_line_col() - --local line_col_alt = colors.line_col_alt .. self.separators[active_sep][2] - - local mode = get_current_mode() - --local git = get_git_status() - local filename = get_filename() .. colors.git_alt - local location = "" - if navic.is_available() then - location = navic.get_location() - end - local filetype = get_filetype() - local line_col = get_line_col() - - return table.concat({ - mode, " ", filename, " ", "%m%r%q", " ", filetype, - "%=", - location, " ", line_col, " " - }) +local add_inactive = function(text) + Statusline.inactive = Statusline.inactive .. text end -Statusline.inactive = function() - return colors.inactive .. '%F' +local addl_active = function(func) + Statusline.active = Statusline.active .. '%{v:lua.' .. func .. '}' end -Statusline.set_explorer = function() - local title = colors.mode .. '  ' - local title_alt = colors.mode_alt .. Statusline.separators[active_sep][2] - - return table.concat({ colors.active, title, title_alt }) +local addl_inactive = function(func) + Statusline.inactive = Statusline.inactive .. '%{v:lua.' .. func .. '}' end ---Statusline = setmetatable(M, { - --__call = function(statusline, mode) - --if mode == "active" then return statusline:set_active() end - --if mode == "inactive" then return statusline:set_inactive() end - --if mode == "explorer" then return statusline:set_explorer() end - --end ---}) +add_active('[') +addl_active('vim.api.nvim_get_mode().mode') +add_active(']') +add_active(' ') +add_active('%<%f') +add_active(' ') +add_active('%m%r%q') +add_active(' ') +addl_active('Statusline.get_filetype()') +add_active('%=') +addl_active('require("nvim-navic").get_location()') +add_active(' ') +add_active('%l:%c') +add_active(' ') + +add_inactive(colors.inactive .. '%F') local augroup = api.nvim_create_augroup('Statusline', { clear = true}) api.nvim_create_autocmd({"WinEnter", "BufEnter"}, { - command = "setlocal statusline=%!v:lua.Statusline.active()", + callback = function() + vim.wo.statusline = Statusline.active + end, group = augroup }) api.nvim_create_autocmd({"WinLeave", "BufLeave"}, { - command = "setlocal statusline=%!v:lua.Statusline.inactive()", + callback = function() + vim.wo.statusline = Statusline.inactive + end, group = augroup }) @@ -200,3 +105,18 @@ api.nvim_create_autocmd({"WinLeave", "BufLeave"}, { -- ) -- end -- end +-- +--local get_git_status = function() + ---- use fallback because it doesn't set this variable on the initial `BufEnter` + --local signs = vim.b.gitsigns_status_dict or {head = '', added = 0, changed = 0, removed = 0} + --local is_head_empty = signs.head ~= '' +-- + --if is_truncated(trunc_width.git_status) then + --return is_head_empty and string.format('  %s ', signs.head or '') or '' + --end +-- + --return is_head_empty and string.format( + --' +%s ~%s -%s |  %s ', + --signs.added, signs.changed, signs.removed, signs.head + --) or '' +--end -- cgit v1.2.3