Sanchayan Maity
6bf989871e
Fixing based on file type was not good enough. Check for error return and act accordingly.
236 lines
5.8 KiB
Lua
236 lines
5.8 KiB
Lua
local gps = require 'nvim-gps'
|
||
local fn = vim.fn
|
||
local api = vim.api
|
||
local cmd = vim.cmd
|
||
|
||
gps.setup({ disable_icons = true })
|
||
|
||
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)
|
||
api.nvim_set_hl(0, group, { fg = fg, bg = 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("TSStatus" , colors.magenta, 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
|
||
local special_ft = { 'diff', 'fugitive', 'git', 'qf' }
|
||
for _, v in pairs(special_ft) do
|
||
if v == filetype then
|
||
return string.format(" %s%s", "%#Filename#", filetype)
|
||
end
|
||
end
|
||
|
||
return string.format(" %s%s", "%#Filename#", '%t%m%r')
|
||
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.treesitter_status = function()
|
||
if gps.is_available then
|
||
local location = gps.get_location()
|
||
if location == 'error' then
|
||
return ''
|
||
else
|
||
return string.format("%s%s", ' %#TSStatus#', location)
|
||
end
|
||
else
|
||
return ''
|
||
end
|
||
end
|
||
|
||
M.set_active = function(self)
|
||
return table.concat {
|
||
self:get_filename(),
|
||
self:get_git_status(),
|
||
self:treesitter_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
|
||
]]
|