From d6a489b221a4c015d262b7c0361ad9230ad25c4e Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Thu, 25 Jul 2024 23:30:34 +0200 Subject: [PATCH] nvim: better lualine custom components --- config/nvim/lua/fschauen/plugins/lualine.lua | 64 +++++++------------ config/nvim/lua/fschauen/util/lualine.lua | 41 ++++++++++++ .../lualine/components/fschauen/filename.lua | 9 ++- .../lua/lualine/components/fschauen/mode.lua | 4 +- .../components/fschauen/searchcount.lua | 11 +++- .../lua/lualine/components/fschauen/spell.lua | 14 ++++ .../lualine/components/fschauen/status.lua | 9 +-- .../components/fschauen/whitespace.lua | 14 ++-- .../lua/lualine/components/fschauen/wrap.lua | 14 ++++ .../nvim/lua/lualine/fschauen/component.lua | 25 -------- 10 files changed, 117 insertions(+), 88 deletions(-) create mode 100644 config/nvim/lua/fschauen/util/lualine.lua create mode 100644 config/nvim/lua/lualine/components/fschauen/spell.lua create mode 100644 config/nvim/lua/lualine/components/fschauen/wrap.lua delete mode 100644 config/nvim/lua/lualine/fschauen/component.lua diff --git a/config/nvim/lua/fschauen/plugins/lualine.lua b/config/nvim/lua/fschauen/plugins/lualine.lua index bcef571..d3a52a9 100644 --- a/config/nvim/lua/fschauen/plugins/lualine.lua +++ b/config/nvim/lua/fschauen/plugins/lualine.lua @@ -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 { diff --git a/config/nvim/lua/fschauen/util/lualine.lua b/config/nvim/lua/fschauen/util/lualine.lua new file mode 100644 index 0000000..c232806 --- /dev/null +++ b/config/nvim/lua/fschauen/util/lualine.lua @@ -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 diff --git a/config/nvim/lua/lualine/components/fschauen/filename.lua b/config/nvim/lua/lualine/components/fschauen/filename.lua index 8017faf..97780a9 100644 --- a/config/nvim/lua/lualine/components/fschauen/filename.lua +++ b/config/nvim/lua/lualine/components/fschauen/filename.lua @@ -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 diff --git a/config/nvim/lua/lualine/components/fschauen/mode.lua b/config/nvim/lua/lualine/components/fschauen/mode.lua index e00bb40..828ddcf 100644 --- a/config/nvim/lua/lualine/components/fschauen/mode.lua +++ b/config/nvim/lua/lualine/components/fschauen/mode.lua @@ -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 diff --git a/config/nvim/lua/lualine/components/fschauen/searchcount.lua b/config/nvim/lua/lualine/components/fschauen/searchcount.lua index c66632c..d4bb0b7 100644 --- a/config/nvim/lua/lualine/components/fschauen/searchcount.lua +++ b/config/nvim/lua/lualine/components/fschauen/searchcount.lua @@ -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() diff --git a/config/nvim/lua/lualine/components/fschauen/spell.lua b/config/nvim/lua/lualine/components/fschauen/spell.lua new file mode 100644 index 0000000..0ccf69a --- /dev/null +++ b/config/nvim/lua/lualine/components/fschauen/spell.lua @@ -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 diff --git a/config/nvim/lua/lualine/components/fschauen/status.lua b/config/nvim/lua/lualine/components/fschauen/status.lua index e1a6460..a99550f 100644 --- a/config/nvim/lua/lualine/components/fschauen/status.lua +++ b/config/nvim/lua/lualine/components/fschauen/status.lua @@ -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 diff --git a/config/nvim/lua/lualine/components/fschauen/whitespace.lua b/config/nvim/lua/lualine/components/fschauen/whitespace.lua index 70ffeaa..9d0c5bb 100644 --- a/config/nvim/lua/lualine/components/fschauen/whitespace.lua +++ b/config/nvim/lua/lualine/components/fschauen/whitespace.lua @@ -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]]_) diff --git a/config/nvim/lua/lualine/components/fschauen/wrap.lua b/config/nvim/lua/lualine/components/fschauen/wrap.lua new file mode 100644 index 0000000..2fd2bc7 --- /dev/null +++ b/config/nvim/lua/lualine/components/fschauen/wrap.lua @@ -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 diff --git a/config/nvim/lua/lualine/fschauen/component.lua b/config/nvim/lua/lualine/fschauen/component.lua deleted file mode 100644 index bfb9501..0000000 --- a/config/nvim/lua/lualine/fschauen/component.lua +++ /dev/null @@ -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