nvim: after/plugin/statusline: Write our own status line plugin

Drop lualine and use our own simple status line. We do not need any
of the fancy stuff, so this works just fine.
This commit is contained in:
Sanchayan Maity 2022-01-22 12:33:26 +05:30
parent 42a3680d87
commit 73dd1939bd
3 changed files with 216 additions and 156 deletions

View file

@ -1,149 +0,0 @@
local colors = {
line_bg = '#21242b',
bg = '#000000',
fg = '#dobfa1',
yellow = '#fabd2f',
cyan = '#008080',
darkblue = '#081633',
green = '#afd700',
orange = '#FF8800',
purple = '#5d4d7a',
magenta = '#c678dd',
blue = '#51afef',
red = '#ec5f67'
}
local conditions = {
buffer_not_empty = function()
return vim.fn.empty(vim.fn.expand '%:t') ~= 1
end,
hide_in_width = function()
return vim.fn.winwidth(0) > 80
end,
check_git_workspace = function()
local filepath = vim.fn.expand '%:p:h'
local gitdir = vim.fn.finddir('.git', filepath .. ';')
return gitdir and #gitdir > 0 and #gitdir < #filepath
end,
check_active_lsp = function()
local clients = vim.lsp.get_active_clients()
return next(clients) ~= nil
end
}
local window = {
function()
return vim.api.nvim_win_get_number(0)
end,
color = { fg = colors.red },
}
local function diffsigns_source()
local gitsigns = vim.b.gitsigns_status_dict
if gitsigns then
return {
added = gitsigns.added,
modified = gitsigns.changed,
removed = gitsigns.removed
}
end
end
local config = {
options = {
component_separators = '',
section_separators = '',
theme = {
normal = { c = { fg = colors.fg , bg = colors.bg } },
inactive = { c = { fg = colors.yellow, bg = colors.bg } },
},
},
sections = {
-- these are to remove the defaults
lualine_a = { window },
lualine_b = {},
lualine_y = {},
lualine_z = {},
-- These will be filled later
lualine_c = {},
lualine_x = {},
},
inactive_sections = {
-- these are to remove the defaults
lualine_a = {},
lualine_b = {},
lualine_y = {},
lualine_z = {},
lualine_c = { window, 'filename' },
lualine_x = { 'location', 'progress' },
},
extensions = { 'fugitive', 'fzf', 'quickfix', 'toggleterm' },
}
local function ins_left(component)
table.insert(config.sections.lualine_c, component)
end
local function ins_right(component)
table.insert(config.sections.lualine_x, component)
end
ins_left {
'filename',
cond = conditions.buffer_not_empty,
color = { fg = colors.blue, gui = 'bold' },
}
ins_left {
'FugitiveHead',
cond = conditions.check_git_workspace,
color = { fg = colors.orange, gui = 'bold' },
}
ins_left {
'diff',
source = diffsigns_source,
cond = conditions.check_git_workspace,
symbols = { added = '+', modified = '~', removed = '-' },
diff_color = {
added = { fg = colors.green },
modified = { fg = colors.orange },
removed = { fg = colors.red },
},
}
ins_left {
function()
return '%='
end,
}
ins_right {
'lsp_progress',
cond = conditions.check_active_lsp and conditions.hide_in_width,
color = { fg = colors.cyan, gui = 'bold' }
}
ins_right {
'diagnostics',
sources = { 'nvim_diagnostic' },
symbols = { error = 'E:', warn = 'W:', info = 'I:', hint = 'H:' },
diagnostics_color = {
color_error = { fg = colors.red },
color_warn = { fg = colors.yellow },
color_info = { fg = colors.cyan },
color_hint = { fg = colors.orange },
},
}
ins_right {
'location',
color = { fg = colors.green, gui = 'bold' }
}
ins_right {
'progress',
color = { fg = colors.magenta, gui = 'bold' }
}
require'lualine'.setup(config)

View file

@ -0,0 +1,216 @@
local fn = vim.fn
local api = vim.api
local cmd = vim.cmd
local M = {}
local colors = {
line_bg = '#21242b',
bg = '#000000',
fg = '#dobfa1',
yellow = '#fabd2f',
cyan = '#008080',
darkblue = '#081633',
green = '#afd700',
orange = '#FF8800',
purple = '#5d4d7a',
magenta = '#c678dd',
blue = '#51afef',
red = '#ec5f67'
}
local function highlight(group, fg, bg)
cmd(string.format("%s %s %s %s %s %s", "highlight ", group, "guifg=", fg, "guibg=", bg))
end
highlight("Window" , colors.red , colors.bg)
highlight("Mode" , colors.green , colors.bg)
highlight("Filename" , colors.blue , colors.bg)
highlight("Fileformat" , colors.purple , colors.bg)
highlight("GitBranch" , colors.orange , colors.bg)
highlight("DiffAdded" , colors.green , colors.bg)
highlight("DiffChanged" , colors.orange , colors.bg)
highlight("DiffRemoved" , colors.red , colors.bg)
highlight("DiagnosticsE", colors.red , colors.bg)
highlight("DiagnosticsW", colors.yellow , colors.bg)
highlight("DiagnosticsI", colors.cyan , colors.bg)
highlight("DiagnosticsH", colors.orange , colors.bg)
highlight("Location" , colors.yellow , colors.bg)
highlight("Progress" , colors.magenta, colors.bg)
M.trunc_width = setmetatable({
git_status = 90,
filename = 140,
}, {
__index = function()
return 80
end,
})
M.is_truncated = function(_, width)
return api.nvim_win_get_width(0) < width
end
M.get_window_number = function()
return string.format(" %s%d", "%#Window#", api.nvim_win_get_number(0))
end
M.modes = setmetatable({
["n" ] = "N" ,
["no"] = "N·P",
["v" ] = "V" ,
["V" ] = "V·L",
[""] = "V·B", -- this is not ^V, but it's , they're different
["s" ] = "S" ,
["S" ] = "S·L",
[""] = "S·B", -- same with this one, it's not ^S but it's 
["i" ] = "I" ,
["ic"] = "I" ,
["R" ] = "R" ,
["Rv"] = "V·R",
["c" ] = "C" ,
["cv"] = "V·E",
["ce"] = "E" ,
["r" ] = "P" ,
["rm"] = "RM" ,
["r?"] = "C" ,
["!" ] = "S" ,
["t" ] = "T" ,
}, {
__index = function()
return "U"
end,
})
M.get_current_mode = function(self)
local current_mode = api.nvim_get_mode().mode
return string.format(" %s%s", "%#Mode#", self.modes[current_mode])
end
M.get_git_branch = function(self)
if self:is_truncated(self.trunc_width.git_status) then
return ""
end
return string.format(" %s %s", "%#GitBranch#", fn.FugitiveHead())
end
M.get_git_status = function(self)
local signs = vim.b.gitsigns_status_dict
or { head = "", added = 0, changed = 0, removed = 0 }
local is_head_empty = signs.head ~= ""
return is_head_empty
and string.format(
" %s+%s %s~%s %s-%s",
"%#DiffAdded#",
signs.added or 0,
"%#DiffChanged#",
signs.changed or 0,
"%#DiffRemoved#",
signs.removed or 0
)
or ""
end
M.get_filename = function(self)
local filetype = vim.bo.filetype
if filetype == "qf" then
return string.format(" %s%s", "%#Filename#", "QF")
else
return string.format(" %s%s", "%#Filename#", '%t%m%r')
end
end
M.get_fileformat = function()
return string.format("%s%s", "%#Fileformat#", vim.o.fileformat):lower()
end
M.diagnostic_status = function()
local diagnostic = ''
local num_errors = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
if num_errors > 0 then
diagnostic = string.format("%s%s%d", diagnostic, ' %#DiagnosticsE#E:', num_errors)
end
local num_warnings = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN })
if num_warnings > 0 then
diagnostic = string.format("%s%s%d", diagnostic, ' %#DiagnosticsW#W:', num_warnings)
end
local num_infos = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.INFO })
if num_infos > 0 then
diagnostic = string.format("%s%s%d", diagnostic, ' %#DiagnosticsI#I:', num_infos)
end
local num_hints = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.HINT })
if num_hints > 0 then
diagnostic = string.format("%s%s%d", diagnostic, ' %#DiagnosticsH#H:', num_hints)
end
return diagnostic
end
M.get_line_col = function()
return string.format(" %s%s", "%#Location#", "%l:%c")
end
M.progress = function()
return string.format(" %s%s", "%#Progress#", "%3p%% ")
end
M.set_active = function(self)
return table.concat {
self:get_current_mode(),
self:get_filename(),
self:get_git_status(),
"%=",
self:diagnostic_status(),
self:get_line_col(),
self:progress(),
}
end
M.set_inactive = function(self)
return table.concat {
"%#Window#",
-- Putting this in a function like the others seems to not work. And takes
-- the window number of the active window. Same for filename.
" %{winnr()}",
"%#Filename#",
' %t%m%r',
self:get_git_branch(),
"%=",
self:get_fileformat(),
self:get_line_col(),
self:progress(),
}
end
_G.Statusline = setmetatable(M, {
__call = function(self, mode)
return self["set_" .. mode](self)
end,
})
cmd [[
augroup Statusline
au!
au WinEnter,BufEnter * setlocal statusline=%!v:lua.Statusline('active')
au WinLeave,BufLeave * setlocal statusline=%!v:lua.Statusline('inactive')
augroup END
]]
cmd [[
augroup HideStatusline
au!
au! FileType fzf,toggleterm
au FileType fzf,toggleterm set laststatus=0 noshowmode noruler
\| au BufLeave <buffer> set laststatus=2 showmode ruler
augroup END
]]

View file

@ -95,13 +95,6 @@ local init = function ()
use 'lervag/vimtex'
-- Dhall
use 'vmchale/dhall-vim'
-- For statusline
use {
'nvim-lualine/lualine.nvim',
requires = {
'arkav/lualine-lsp-progress'
}
}
-- Marks and registers
use {
'chentau/marks.nvim',