rewrote statusline to be compartmentalized into named components and easier to understand/customize as a basis

This commit is contained in:
zbirenbaum 2022-01-10 23:39:37 -05:00 committed by siduck
parent 8c2bbd2143
commit fa9f9aad0b
1 changed files with 357 additions and 314 deletions

View File

@ -3,44 +3,44 @@ local lsp = require "feline.providers.lsp"
local lsp_severity = vim.diagnostic.severity local lsp_severity = vim.diagnostic.severity
local icon_styles = { local icon_styles = {
default = { default = {
left = "", left = "",
right = "", right = "",
main_icon = "", main_icon = "",
vi_mode_icon = "", vi_mode_icon = "",
position_icon = "", position_icon = "",
}, },
arrow = { arrow = {
left = "", left = "",
right = "", right = "",
main_icon = "", main_icon = "",
vi_mode_icon = "", vi_mode_icon = "",
position_icon = "", position_icon = "",
}, },
block = { block = {
left = " ", left = " ",
right = " ", right = " ",
main_icon = "", main_icon = "",
vi_mode_icon = "", vi_mode_icon = "",
position_icon = "", position_icon = "",
}, },
round = { round = {
left = "", left = "",
right = "", right = "",
main_icon = "", main_icon = "",
vi_mode_icon = "", vi_mode_icon = "",
position_icon = "", position_icon = "",
}, },
slant = { slant = {
left = "", left = "",
right = "", right = "",
main_icon = "", main_icon = "",
vi_mode_icon = "", vi_mode_icon = "",
position_icon = "", position_icon = "",
}, },
} }
local config = require("core.utils").load_config().plugins.options.statusline local config = require("core.utils").load_config().plugins.options.statusline
@ -54,327 +54,370 @@ local shortline = config.shortline == false and true
-- Initialize the components table -- Initialize the components table
local components = { local components = {
active = {}, active = {},
inactive = {}, inactive = {},
} }
table.insert(components.active, {}) table.insert(components.active, {})
table.insert(components.active, {}) table.insert(components.active, {})
table.insert(components.active, {}) table.insert(components.active, {})
components.active[1][1] = { local get_components = function()
provider = statusline_style.main_icon, local M = {}
hl = { M.main_icon = {
fg = colors.statusline_bg, provider = statusline_style.main_icon,
bg = colors.nord_blue,
},
right_sep = { str = statusline_style.right, hl = { hl = {
fg = colors.nord_blue, fg = colors.statusline_bg,
bg = colors.lightbg, bg = colors.nord_blue,
} }, },
}
components.active[1][2] = { right_sep = { str = statusline_style.right, hl = {
provider = function() fg = colors.nord_blue,
local filename = vim.fn.expand "%:t" bg = colors.lightbg,
local extension = vim.fn.expand "%:e" } },
local icon = require("nvim-web-devicons").get_icon(filename, extension) }
if icon == nil then
icon = ""
return icon
end
return " " .. icon .. " " .. filename .. " "
end,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
end,
hl = {
fg = colors.white,
bg = colors.lightbg,
},
right_sep = { str = statusline_style.right, hl = { fg = colors.lightbg, bg = colors.lightbg2 } }, M.file = {
} provider = function()
local filename = vim.fn.expand "%:t"
local extension = vim.fn.expand "%:e"
local icon = require("nvim-web-devicons").get_icon(filename, extension)
if icon == nil then
icon = ""
return icon
end
return " " .. icon .. " " .. filename .. " "
end,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
end,
hl = {
fg = colors.white,
bg = colors.lightbg,
},
components.active[1][3] = { right_sep = { str = statusline_style.right, hl = { fg = colors.lightbg, bg = colors.lightbg2 } },
provider = function() }
local dir_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
return "" .. dir_name .. " "
end,
enabled = shortline or function(winid) M.dir = {
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 80 provider = function()
end, local dir_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
return "" .. dir_name .. " "
end,
hl = { enabled = shortline or function(winid)
fg = colors.grey_fg2, return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 80
bg = colors.lightbg2, end,
},
right_sep = {
str = statusline_style.right,
hi = {
fg = colors.lightbg2,
bg = colors.statusline_bg,
},
},
}
components.active[1][4] = { hl = {
provider = "git_diff_added", fg = colors.grey_fg2,
hl = { bg = colors.lightbg2,
fg = colors.grey_fg2, },
bg = colors.statusline_bg, right_sep = {
}, str = statusline_style.right,
icon = "", hi = {
} fg = colors.lightbg2,
-- diffModfified bg = colors.statusline_bg,
components.active[1][5] = { },
provider = "git_diff_changed", },
hl = { }
fg = colors.grey_fg2,
bg = colors.statusline_bg,
},
icon = "",
}
-- diffRemove
components.active[1][6] = {
provider = "git_diff_removed",
hl = {
fg = colors.grey_fg2,
bg = colors.statusline_bg,
},
icon = "",
}
components.active[1][7] = { M.git_added = {
provider = "diagnostic_errors", provider = "git_diff_added",
enabled = function() hl = {
return lsp.diagnostics_exist(lsp_severity.ERROR) fg = colors.grey_fg2,
end, bg = colors.statusline_bg,
},
icon = "",
}
hl = { fg = colors.red }, M.git_modified = {
icon = "", provider = "git_diff_changed",
} hl = {
fg = colors.grey_fg2,
bg = colors.statusline_bg,
},
icon = "",
}
components.active[1][8] = { M.git_removed = {
provider = "diagnostic_warnings", provider = "git_diff_removed",
enabled = function() hl = {
return lsp.diagnostics_exist(lsp_severity.WARN) fg = colors.grey_fg2,
end, bg = colors.statusline_bg,
hl = { fg = colors.yellow }, },
icon = " ", icon = " ",
} }
components.active[1][9] = { M.diagnostic_errors = {
provider = "diagnostic_hints", provider = "diagnostic_errors",
enabled = function() enabled = function()
return lsp.diagnostics_exist(lsp_severity.HINT) return lsp.diagnostics_exist(lsp_severity.ERROR)
end, end,
hl = { fg = colors.grey_fg2 },
icon = "",
}
components.active[1][10] = { hl = { fg = colors.red },
provider = "diagnostic_info", icon = "",
enabled = function() }
return lsp.diagnostics_exist(lsp_severity.INFO)
end,
hl = { fg = colors.green },
icon = "",
}
components.active[2][1] = { M.diagnostic_warnings= {
provider = function() provider = "diagnostic_warnings",
local Lsp = vim.lsp.util.get_progress_messages()[1] enabled = function()
return lsp.diagnostics_exist(lsp_severity.WARN)
end,
hl = { fg = colors.yellow },
icon = "",
}
if Lsp then M.diagnostic_hints = {
local msg = Lsp.message or "" provider = "diagnostic_hints",
local percentage = Lsp.percentage or 0 enabled = function()
local title = Lsp.title or "" return lsp.diagnostics_exist(lsp_severity.HINT)
local spinners = { end,
"", hl = { fg = colors.grey_fg2 },
"", icon = "",
"", }
}
local success_icon = { M.dianostic_info ={
"", provider = "diagnostic_info",
"", enabled = function()
"", return lsp.diagnostics_exist(lsp_severity.INFO)
} end,
hl = { fg = colors.green },
icon = "",
}
local ms = vim.loop.hrtime() / 1000000 M.lsp_progress = {
local frame = math.floor(ms / 120) % #spinners provider = function()
local Lsp = vim.lsp.util.get_progress_messages()[1]
if percentage >= 70 then if Lsp then
return string.format(" %%<%s %s %s (%s%%%%) ", success_icon[frame + 1], title, msg, percentage) local msg = Lsp.message or ""
end local percentage = Lsp.percentage or 0
local title = Lsp.title or ""
local spinners = {
"",
"",
"",
}
return string.format(" %%<%s %s %s (%s%%%%) ", spinners[frame + 1], title, msg, percentage) local success_icon = {
end "",
"",
"",
}
return "" local ms = vim.loop.hrtime() / 1000000
end, local frame = math.floor(ms / 120) % #spinners
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 80
end,
hl = { fg = colors.green },
}
components.active[3][1] = { if percentage >= 70 then
provider = function() return string.format(" %%<%s %s %s (%s%%%%) ", success_icon[frame + 1], title, msg, percentage)
if next(vim.lsp.buf_get_clients()) ~= nil then end
return " LSP"
else
return ""
end
end,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
end,
hl = { fg = colors.grey_fg2, bg = colors.statusline_bg },
}
components.active[3][2] = { return string.format(" %%<%s %s %s (%s%%%%) ", spinners[frame + 1], title, msg, percentage)
provider = "git_branch", end
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
end,
hl = {
fg = colors.grey_fg2,
bg = colors.statusline_bg,
},
icon = "",
}
components.active[3][3] = { return ""
provider = " " .. statusline_style.left, end,
hl = { enabled = shortline or function(winid)
fg = colors.one_bg2, return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 80
bg = colors.statusline_bg, end,
}, hl = { fg = colors.green },
} }
local mode_colors = { M.lsp = {
["n"] = { "NORMAL", colors.red }, provider = function()
["no"] = { "N-PENDING", colors.red }, if next(vim.lsp.buf_get_clients()) ~= nil then
["i"] = { "INSERT", colors.dark_purple }, return " LSP"
["ic"] = { "INSERT", colors.dark_purple }, else
["t"] = { "TERMINAL", colors.green }, return ""
["v"] = { "VISUAL", colors.cyan }, end
["V"] = { "V-LINE", colors.cyan }, end,
[""] = { "V-BLOCK", colors.cyan }, enabled = shortline or function(winid)
["R"] = { "REPLACE", colors.orange }, return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
["Rv"] = { "V-REPLACE", colors.orange }, end,
["s"] = { "SELECT", colors.nord_blue }, hl = { fg = colors.grey_fg2, bg = colors.statusline_bg },
["S"] = { "S-LINE", colors.nord_blue }, }
[""] = { "S-BLOCK", colors.nord_blue },
["c"] = { "COMMAND", colors.pink },
["cv"] = { "COMMAND", colors.pink },
["ce"] = { "COMMAND", colors.pink },
["r"] = { "PROMPT", colors.teal },
["rm"] = { "MORE", colors.teal },
["r?"] = { "CONFIRM", colors.teal },
["!"] = { "SHELL", colors.green },
}
local chad_mode_hl = function() M.git_branch = {
return { provider = "git_branch",
fg = mode_colors[vim.fn.mode()][2], enabled = shortline or function(winid)
bg = colors.one_bg, return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 70
} end,
hl = {
fg = colors.grey_fg2,
bg = colors.statusline_bg,
},
icon = "",
}
M.git_right_separator = {
provider = " " .. statusline_style.left,
hl = {
fg = colors.one_bg2,
bg = colors.statusline_bg,
},
}
local mode_colors = {
["n"] = { "NORMAL", colors.red },
["no"] = { "N-PENDING", colors.red },
["i"] = { "INSERT", colors.dark_purple },
["ic"] = { "INSERT", colors.dark_purple },
["t"] = { "TERMINAL", colors.green },
["v"] = { "VISUAL", colors.cyan },
["V"] = { "V-LINE", colors.cyan },
[""] = { "V-BLOCK", colors.cyan },
["R"] = { "REPLACE", colors.orange },
["Rv"] = { "V-REPLACE", colors.orange },
["s"] = { "SELECT", colors.nord_blue },
["S"] = { "S-LINE", colors.nord_blue },
[""] = { "S-BLOCK", colors.nord_blue },
["c"] = { "COMMAND", colors.pink },
["cv"] = { "COMMAND", colors.pink },
["ce"] = { "COMMAND", colors.pink },
["r"] = { "PROMPT", colors.teal },
["rm"] = { "MORE", colors.teal },
["r?"] = { "CONFIRM", colors.teal },
["!"] = { "SHELL", colors.green },
}
local chad_mode_hl = function()
return {
fg = mode_colors[vim.fn.mode()][2],
bg = colors.one_bg,
}
end
M.mode_left_separator = {
provider = statusline_style.left,
hl = function()
return {
fg = mode_colors[vim.fn.mode()][2],
bg = colors.one_bg2,
}
end,
}
M.mode_icon = {
provider = statusline_style.vi_mode_icon,
hl = function()
return {
fg = colors.statusline_bg,
bg = mode_colors[vim.fn.mode()][2],
}
end,
}
M.mode_string = {
provider = function()
return " " .. mode_colors[vim.fn.mode()][1] .. " "
end,
hl = chad_mode_hl,
}
M.loc_spacer_left = {
provider = statusline_style.left,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.grey,
bg = colors.one_bg,
}
}
M.loc_separator_left = {
provider = statusline_style.left,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.green,
bg = colors.grey,
},
}
M.loc_position_icon = {
provider = statusline_style.position_icon,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.black,
bg = colors.green,
}
}
M.loc_position_text = {
provider = function()
local current_line = vim.fn.line "."
local total_line = vim.fn.line "$"
if current_line == 1 then
return " Top "
elseif current_line == vim.fn.line "$" then
return " Bot "
end
local result, _ = math.modf((current_line / total_line) * 100)
return " " .. result .. "%% "
end,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.green,
bg = colors.one_bg,
},
}
return M
end end
components.active[3][4] = { local components_list = get_components()
provider = statusline_style.left,
hl = function()
return {
fg = mode_colors[vim.fn.mode()][2],
bg = colors.one_bg2,
}
end,
}
components.active[3][5] = { local left = {}
provider = statusline_style.vi_mode_icon, local right = {}
hl = function() local middle = {}
return {
fg = colors.statusline_bg,
bg = mode_colors[vim.fn.mode()][2],
}
end,
}
components.active[3][6] = { table.insert(left, components_list.main_icon)
provider = function() table.insert(left, components_list.file)
return " " .. mode_colors[vim.fn.mode()][1] .. " " table.insert(left, components_list.dir)
end,
hl = chad_mode_hl,
}
components.active[3][7] = { table.insert(left, components_list.git_added)
provider = statusline_style.left, table.insert(left, components_list.git_modified)
enabled = shortline or function(winid) table.insert(left, components_list.git_removed)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.grey,
bg = colors.one_bg,
},
}
components.active[3][8] = { table.insert(left, components_list.diagnostic_errors)
provider = statusline_style.left, table.insert(left, components_list.diagnostic_warnings)
enabled = shortline or function(winid) table.insert(left, components_list.diagnostic_hints)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90 table.insert(left, components_list.diagnostic_info)
end,
hl = {
fg = colors.green,
bg = colors.grey,
},
}
components.active[3][9] = { table.insert(middle, components_list.lsp_progress)
provider = statusline_style.position_icon,
enabled = shortline or function(winid)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90
end,
hl = {
fg = colors.black,
bg = colors.green,
},
}
components.active[3][10] = { table.insert(right, components_list.lsp)
provider = function() table.insert(right, components_list.git_branch)
local current_line = vim.fn.line "." table.insert(right, components_list.git_right_separator)
local total_line = vim.fn.line "$"
if current_line == 1 then table.insert(right, components_list.mode_left_separator)
return " Top " table.insert(right, components_list.mode_mode_icon)
elseif current_line == vim.fn.line "$" then table.insert(right, components_list.mode_mode_string)
return " Bot "
end
local result, _ = math.modf((current_line / total_line) * 100)
return " " .. result .. "%% "
end,
enabled = shortline or function(winid) table.insert(right, components_list.loc_spacer_left)
return vim.api.nvim_win_get_width(tonumber(winid) or 0) > 90 table.insert(right, components_list.loc_separator_left)
end, table.insert(right, components_list.loc_position_icon)
table.insert(right, components_list.loc_position_text)
hl = { components.active[1] = left
fg = colors.green, components.active[2] = middle
bg = colors.one_bg, components.active[3] = right
},
}
require("feline").setup { require("feline").setup {
theme = { theme = {
bg = colors.statusline_bg, bg = colors.statusline_bg,
fg = colors.fg, fg = colors.fg,
}, },
components = components, components = components,
} }