From 5df382765b2bbeb5f3168b3597f33c419f31a2a7 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 13 Nov 2024 17:20:50 +0100 Subject: [PATCH] refactor(lualine): status component --- lua/lazyvim/plugins/extras/ai/copilot.lua | 39 +++++-------------- lua/lazyvim/util/lualine.lua | 46 +++++++++++++---------- 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/lua/lazyvim/plugins/extras/ai/copilot.lua b/lua/lazyvim/plugins/extras/ai/copilot.lua index 69429e33..30d05ca1 100644 --- a/lua/lazyvim/plugins/extras/ai/copilot.lua +++ b/lua/lazyvim/plugins/extras/ai/copilot.lua @@ -44,36 +44,17 @@ return { optional = true, event = "VeryLazy", opts = function(_, opts) - local colors = { - [""] = LazyVim.ui.fg("Special"), - ["Normal"] = LazyVim.ui.fg("Special"), - ["Warning"] = LazyVim.ui.fg("DiagnosticError"), - ["InProgress"] = LazyVim.ui.fg("DiagnosticWarn"), - } - table.insert(opts.sections.lualine_x, 2, { - function() - local icon = LazyVim.config.icons.kinds.Copilot - local status = require("copilot.api").status.data - return icon .. (status.message or "") - end, - cond = function() - if not package.loaded["copilot"] then - return + table.insert( + opts.sections.lualine_x, + 2, + LazyVim.lualine.status(LazyVim.config.icons.kinds.Copilot, function() + local clients = package.loaded["copilot"] and LazyVim.lsp.get_clients({ name = "copilot", bufnr = 0 }) or {} + if #clients > 0 then + local status = require("copilot.api").status.data.status + return (status == "InProgress" and "pending") or (status == "Warning" and "error") or "ok" end - local ok, clients = pcall(LazyVim.lsp.get_clients, { name = "copilot", bufnr = 0 }) - if not ok then - return false - end - return ok and #clients > 0 - end, - color = function() - if not package.loaded["copilot"] then - return - end - local status = require("copilot.api").status.data - return colors[status.status] or colors[""] - end, - }) + end) + ) end, }, diff --git a/lua/lazyvim/util/lualine.lua b/lua/lazyvim/util/lualine.lua index 08308c56..c759e6dc 100644 --- a/lua/lazyvim/util/lualine.lua +++ b/lua/lazyvim/util/lualine.lua @@ -1,9 +1,33 @@ ---@class lazyvim.util.lualine local M = {} +---@param icon string +---@param status fun(): nil|"ok"|"error"|"pending" +function M.status(icon, status) + local colors = { + ok = LazyVim.ui.fg("Special"), + error = LazyVim.ui.fg("DiagnosticError"), + pending = LazyVim.ui.fg("DiagnosticWarn"), + } + return { + function() + return icon + end, + cond = function() + return status() ~= nil + end, + color = function() + return colors[status()] or colors.ok + end, + } +end + +---@param name string +---@param icon? string function M.cmp_source(name, icon) + icon = icon or LazyVim.config.icons.kinds[name:sub(1, 1):upper() .. name:sub(2)] local started = false - local function status() + return M.status(icon, function() if not package.loaded["cmp"] then return end @@ -20,25 +44,7 @@ function M.cmp_source(name, icon) return "ok" end end - end - - local colors = { - ok = LazyVim.ui.fg("Special"), - error = LazyVim.ui.fg("DiagnosticError"), - pending = LazyVim.ui.fg("DiagnosticWarn"), - } - - return { - function() - return icon or LazyVim.config.icons.kinds[name:sub(1, 1):upper() .. name:sub(2)] - end, - cond = function() - return status() ~= nil - end, - color = function() - return colors[status()] or colors.ok - end, - } + end) end ---@param component any