diff --git a/config/nvim/lua/fschauen/plugins/lualine.lua b/config/nvim/lua/fschauen/plugins/lualine.lua index aed512e..7f152c1 100644 --- a/config/nvim/lua/fschauen/plugins/lualine.lua +++ b/config/nvim/lua/fschauen/plugins/lualine.lua @@ -4,7 +4,9 @@ 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 indicator = require("fschauen.util.lualine").indicator local window = require("fschauen.util.window") local orange = "#d65d0e" @@ -12,24 +14,59 @@ return { return vim.diagnostic.is_enabled { bufnr = bufnr or 0 } end - local autoformat = require("fschauen.util.autoformat").lualine() + -- + -- Components + -- + + local autoformat = indicator { + icon = icons.ui.Format, + cond = require("fschauen.util.autoformat").is_enabled, + } local branch = { "branch", - icon = require("fschauen.util.icons").git.Branch, + icon = icons.git.Branch, + cond = window.is_medium, + } + local diagnostics = { + dynamic_color("diagnostics"), + cond = is_diagnostics_enabled, + } + local diag_status = indicator { + icon = icons.ui.Diagnostic, + cond = is_diagnostics_enabled, + } + local fileformat = { + "fileformat", cond = window.is_medium, } - local diagnostics = { dynamic_color("diagnostics"), cond = is_diagnostics_enabled } - local diag_status = { function() return "" end, cond = is_diagnostics_enabled } - local fileformat = { "fileformat", cond = window.is_medium } local filename = "fschauen.filename" - local filetype = { dynamic_color("filetype"), cond = window.is_medium } + 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 = orange }, padding = 0 } - local whitespace = { dynamic_color("fschauen.whitespace"), cond = window.is_wide } - local wrap = "fschauen.wrap" + local spell = indicator { + icon = icons.ui.SpellCheck, + cond = function() return vim.o.spell end, + } + local status = { + dynamic_color("fschauen.status"), + color = { fg = orange }, + padding = 0, + } + local whitespace = { + dynamic_color("fschauen.whitespace"), + cond = window.is_wide, + } + local wrap = indicator { + icon = icons.ui.TextWrap, + cond = function() return vim.o.wrap end, + } + + -- + -- Sections + -- local sections = { lualine_a = { mode }, diff --git a/config/nvim/lua/fschauen/util/autoformat.lua b/config/nvim/lua/fschauen/util/autoformat.lua index 94c502a..a2f27a7 100644 --- a/config/nvim/lua/fschauen/util/autoformat.lua +++ b/config/nvim/lua/fschauen/util/autoformat.lua @@ -4,18 +4,18 @@ M._augroup = nil ---Whether autoformatting is enabled. ---@return boolean -local is_enabled = function() return M._augroup ~= nil end +M.is_enabled = function() return M._augroup ~= nil end ---Disable autoformatting. M.disable = function() - if not is_enabled() then return end + if not M.is_enabled() then return end vim.api.nvim_del_augroup_by_id(M._augroup) M._augroup = nil end ---Enable autoformatting. M.enable = function() - if is_enabled() then return end + if M.is_enabled() then return end local ok, formatter = pcall(require, "formatter.format") if not ok then @@ -34,21 +34,11 @@ end ---Toggle autoformatting. M.toggle = function() - if is_enabled() then + if M.is_enabled() then M.disable() else M.enable() end end ----Create a lualine component that shows an icon when autoformatting is enabled. ----@return table component -M.lualine = function() - local icon = require("fschauen.util.icons").ui.Format - return { - function() return icon end, - cond = is_enabled, - } -end - return M diff --git a/config/nvim/lua/fschauen/util/icons.lua b/config/nvim/lua/fschauen/util/icons.lua index bf19aca..0737243 100644 --- a/config/nvim/lua/fschauen/util/icons.lua +++ b/config/nvim/lua/fschauen/util/icons.lua @@ -119,9 +119,11 @@ M.ui = { ReadOnly = "", -- "RO", Search = "", --  Sleep = "󰒲", + SpellCheck = "󰓆", Telescope = "", TestTube = "󰙨", -- 󰤑 Text = "󰬴", + TextWrap = "󰖶", Toggle = "󰨚", TrafficLight = "󱠪", Tree = "", diff --git a/config/nvim/lua/fschauen/util/lualine.lua b/config/nvim/lua/fschauen/util/lualine.lua index c232806..e07d6de 100644 --- a/config/nvim/lua/fschauen/util/lualine.lua +++ b/config/nvim/lua/fschauen/util/lualine.lua @@ -38,4 +38,37 @@ M.dynamic_color = function(base) return component end +---@class LualineIndicatorSpec +---@field icon string The icon to show when the indicator is active. +---@field cond function A function that returns true when the indicator should +--- be visible, false otherwise. + +---Make a lualine indicator that shows an icon when a condition is true. +---@param spec LualineIndicatorSpec +---@return table component +M.indicator = function(spec) + vim.validate { + spec = { spec, "table" }, + icon = { spec.icon, "string" }, + cond = { spec.cond, "function" }, + } + + local component = require("lualine.component"):extend() + + function component:init(options) + component.super.init( + self, + vim.tbl_deep_extend( + "keep", + options or {}, + { cond = spec.cond or function() return false end } + ) + ) + end + + function component:update_status(--[[is_focused]]_) return spec.icon or "?" end + + return component +end + return M diff --git a/config/nvim/lua/lualine/components/fschauen/spell.lua b/config/nvim/lua/lualine/components/fschauen/spell.lua deleted file mode 100644 index 0ccf69a..0000000 --- a/config/nvim/lua/lualine/components/fschauen/spell.lua +++ /dev/null @@ -1,14 +0,0 @@ -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/wrap.lua b/config/nvim/lua/lualine/components/fschauen/wrap.lua deleted file mode 100644 index 2fd2bc7..0000000 --- a/config/nvim/lua/lualine/components/fschauen/wrap.lua +++ /dev/null @@ -1,14 +0,0 @@ -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