nvim: better lualine custom components

This commit is contained in:
Fernando Schauenburg 2024-07-25 23:30:34 +02:00
parent de3e6518e8
commit d6a489b221
10 changed files with 117 additions and 88 deletions

View file

@ -4,53 +4,35 @@ return {
dependencies = "nvim-tree/nvim-web-devicons",
opts = function()
local icons = require("fschauen.util.icons")
local dynamic_color = require("fschauen.util.lualine").dynamic_color
local window = require("fschauen.window")
local autoformat = require("fschauen.util.autoformat").lualine()
local component = require("lualine.fschauen.component")
local diagnostics = component.colored_if_focused("diagnostics")
local filetype = component.colored_if_focused("filetype")
local status = component.colored_if_focused("fschauen.status")
local whitespace = component.colored_if_focused("fschauen.whitespace")
local spell = {
function() return "󰓆" end,
cond = function() return vim.o.spell end,
}
local wrap = {
function() return "󰖶" end,
cond = function() return vim.o.wrap end,
local branch = {
"branch",
icon = require("fschauen.util.icons").git.Branch,
cond = window.is_medium,
}
local diagnostics = dynamic_color("diagnostics")
local fileformat = { "fileformat", cond = window.is_medium }
local filename = "fschauen.filename"
local filetype = { dynamic_color("filetype"), cond = window.is_medium }
local mode = "fschauen.mode"
local searchcount = "fschauen.searchcount"
local spell = "fschauen.spell"
local status =
{ dynamic_color("fschauen.status"), color = { fg = "#fe8019" }, padding = 0 }
local whitespace =
{ dynamic_color("fschauen.whitespace"), color = { bg = "#d65d0e" } }
local wrap = "fschauen.wrap"
local sections = {
lualine_a = {
"fschauen.mode",
},
lualine_b = {
{ "branch", icon = icons.git.Branch, cond = window.is_medium },
},
lualine_c = {
"fschauen.filename",
status,
},
lualine_x = {
diagnostics,
"fschauen.searchcount",
{ filetype, cond = window.is_medium },
},
lualine_y = {
spell,
wrap,
autoformat,
{ "fileformat", cond = window.is_medium },
"progress",
},
lualine_z = {
"location",
whitespace,
},
lualine_a = { mode },
lualine_b = { branch },
lualine_c = { filename, status },
lualine_x = { diagnostics, searchcount, filetype },
lualine_y = { spell, wrap, autoformat, fileformat, "progress" },
lualine_z = { "location", whitespace },
}
return {

View file

@ -0,0 +1,41 @@
local M = {}
---Make a lualine component that is colored only when focused.
---@param base table|string Either a lualine component or a component name.
---@return table component
M.dynamic_color = function(base)
local component = {}
if type(base) == "string" then
component = require("lualine.components." .. base):extend()
elseif type(base) == "table" then
component = base:extend()
else
return component
end
function component:init(options)
component.super.init(
self,
vim.tbl_deep_extend("keep", options or {}, {
colored = true,
dynamic_color = true,
})
)
self.saved_highlight = self.options.color_highlight
end
function component:update_status(is_focused)
if self.options.dynamic_color then self.options.colored = is_focused end
return component.super.update_status(self, is_focused)
end
function component:draw(default_highlight, is_focused)
self.options.color_highlight = self.options.colored and self.saved_highlight or nil
return component.super.draw(self, default_highlight, is_focused)
end
return component
end
return M

View file

@ -1,9 +1,9 @@
local M = require("lualine.component"):extend()
function M:init(opts)
M.super.init(self, opts)
function M:init(options)
M.super.init(self, options)
local color = opts.color or {}
local color = self.options.color or {}
local modified = { gui = "italic" }
self.custom_highlights = { -- [is_focused, modified]
@ -18,13 +18,12 @@ function M:init(opts)
}
end
local window = require("fschauen.window")
function M:update_status(is_focused)
self.options.color_highlight = self.custom_highlights[is_focused][vim.bo.modified]
local path = vim.fn.expand("%:~:.")
local window = require("fschauen.window")
if window.is_wide() then
return path
elseif window.is_medium() then

View file

@ -1,7 +1,7 @@
local M = require("lualine.component"):extend()
-- stylua: ignore start
M.map = {
local map = {
["n"] = "", -- "Normal "
["no"] = "", -- "O-Pend "
["ni"] = "", -- "Normal " (normal via i_CTRL-O)
@ -25,7 +25,7 @@ function M:update_status(is_focused)
if not is_focused then return " 󰒲 " end
local code = vim.api.nvim_get_mode().mode:lower()
local symbol = M.map[code:sub(1, 2)] or M.map[code:sub(1, 1)] or code
local symbol = map[code:sub(1, 2)] or map[code:sub(1, 1)] or code
return " " .. symbol .. " "
end

View file

@ -1,8 +1,13 @@
local M = require("lualine.component"):extend()
function M:init(opts)
local default_opts = { maxcount = 999, timeout = 250 }
M.super.init(self, vim.tbl_extend("keep", opts or {}, default_opts))
function M:init(options)
M.super.init(
self,
vim.tbl_extend("keep", options or {}, {
maxcount = 999,
timeout = 250,
})
)
end
function M:update_status()

View file

@ -0,0 +1,14 @@
local M = require("lualine.component"):extend()
function M:init(options)
M.super.init(
self,
vim.tbl_deep_extend("keep", options or {}, {
cond = function() return vim.o.spell end,
})
)
end
function M:update_status(--[[is_focused]]_) return "󰓆" end
return M

View file

@ -1,13 +1,8 @@
local M = require("lualine.fschauen.component"):extend()
function M:init(opts)
opts.color = opts.color or { fg = "#ffffff" } -- alternative: '#f9f5d7'
M.super.init(self, opts)
end
local M = require("lualine.component"):extend()
function M:update_status(--[[is_focused]]_)
local status = ""
if vim.bo.modified then status = status .. "" end
if vim.bo.modified then status = status .. "" end
if vim.bo.readonly or not vim.bo.modifiable then status = status .. "" end
return status
end

View file

@ -1,9 +1,13 @@
local M = require("lualine.fschauen.component"):extend()
local M = require("lualine.component"):extend()
function M:init(opts)
opts.color = opts.color or { bg = "#d65d0e" }
opts.cond = opts.cond or function() return vim.bo.filetype ~= "help" end
M.super.init(self, opts)
function M:init(options)
M.super.init(
self,
vim.tbl_deep_extend("keep", options or {}, {
color = { bg = "#d65d0e" },
cond = function() return vim.bo.filetype ~= "help" end,
})
)
end
function M:update_status(--[[is_focused]]_)

View file

@ -0,0 +1,14 @@
local M = require("lualine.component"):extend()
function M:init(options)
M.super.init(
self,
vim.tbl_deep_extend("keep", options or {}, {
cond = function() return vim.o.wrap end,
})
)
end
function M:update_status(--[[is_focused]]_) return "󰖶" end
return M

View file

@ -1,25 +0,0 @@
local M = require("lualine.component"):extend()
function M:init(opts)
opts.colored = opts.colored ~= false -- colored by default
M.super.init(self, opts)
self.saved_hl = self.options.color_highlight
end
function M:draw(default_highlight, is_focused)
self.options.color_highlight = self.options.colored and self.saved_hl or nil
return M.super.draw(self, default_highlight, is_focused)
end
M.colored_if_focused = function(component)
local C = require("lualine.components." .. component):extend()
function C:update_status(is_focused)
self.options.colored = is_focused
return C.super.update_status(self, is_focused)
end
return C
end
return M