♻️ refactor(lualine): refactor lualine configuration

Initiate lualine configuration with lualine plugin
This commit is contained in:
2024-08-03 22:43:43 -04:00
parent 61bd1b6863
commit b36d8622f7
2 changed files with 214 additions and 190 deletions
-190
View File
@@ -1,190 +0,0 @@
-- -----------------------------------------------------------------------------
-- -------------------------------- LUA-LINE -----------------------------------
-- -----------------------------------------------------------------------------
--- @param trunc_width number trunctates component when screen width is less then trunc_width
--- @param trunc_len number truncates component to trunc_len number of chars
--- @param hide_width number hides component when window width is smaller then hide_width
--- @param no_ellipsis boolean whether to disable adding '...' at end after truncation
--- return function that can format the component accordingly
local function trunc(trunc_width, trunc_len, hide_width, no_ellipsis)
return function(str)
local win_width = vim.fn.winwidth(0)
if hide_width and win_width < hide_width then
return ""
elseif
trunc_width
and trunc_len
and win_width < trunc_width
and #str > trunc_len
then
return str:sub(1, trunc_len) .. (no_ellipsis and "" or "...")
end
return str
end
end
--- @param trunc_width number trunctates component when screen width is less then trunc_width
--- @param trunc_len number truncates component to trunc_len number of chars
--- @param hide_width number hides component when window width is smaller then hide_width
--- @param no_ellipsis boolean whether to disable adding '...' at end after truncation
--- return function that can format the component accordingly
local function trunc_front(trunc_width, trunc_len, hide_width, no_ellipsis)
return function(str)
local win_width = vim.fn.winwidth(0)
if hide_width and win_width < hide_width then
return ""
elseif
trunc_width
and trunc_len
and win_width < trunc_width
and #str > trunc_len
then
local truncated_str = str:sub(-trunc_len)
return (no_ellipsis and "" or "...") .. truncated_str
end
return str
end
end
-- Function to convert RGB to hexadecimal
local function rgb_to_hex(rgb)
return string.format("#%02x%02x%02x", rgb[1], rgb[2], rgb[3])
end
local function get_fg_color(hlgroup)
local hl = vim.api.nvim_get_hl(0, { name = hlgroup, link = false })
local fg = hl.fg
if fg then
return rgb_to_hex({
bit.rshift(bit.band(fg, 0xFF0000), 16),
bit.rshift(bit.band(fg, 0x00FF00), 8),
bit.band(fg, 0x0000FF),
})
end
return nil
end
-- Ensure the function is called only once
local lualine_updated = false
-- Function to add custom sections to lualine
local function add_custom_sections_to_lualine()
if lualine_updated then
return
end
lualine_updated = true
-- Require the dependencies
local wakatime_cache = require("config.wakatime_cache")
local music_current = require("config.music_stats")
local git_blame = require("gitblame")
-- Get the current lualine configuration
local config = require("lualine").get_config()
config.options.section_separators = { left = "", right = "" }
config.options.component_separators = { left = "", right = "" }
config.options.globalstatus = true
-- Replace the lualine_a section
config.sections.lualine_a = {
{ "mode", fmt = trunc(80, 4, 0, true) },
}
-- Replace the lualine_b section
config.sections.lualine_b = {
{ "branch", fmt = trunc(80, 4, 0, true) },
}
-- Add Git-blame to lualine_c section
table.insert(config.sections.lualine_c, {
git_blame.get_current_blame_text,
fmt = trunc_front(160, 40, 120, true),
cond = git_blame.is_blame_text_available,
color = { fg = get_fg_color("GitSignsCurrentLineBlame") },
})
-- Add the WakaTime data to the lualine_x section
table.insert(config.sections.lualine_x, {
function()
return (wakatime_cache.get_today() or "Loading...")
end,
icon = wakatime_cache.get_icon(),
fmt = trunc(120, 20, 120, true),
color = function()
if wakatime_cache.get_total_minutes() >= 60 then
return { fg = get_fg_color("diffAdded") }
elseif wakatime_cache.get_total_minutes() >= 30 then
return { fg = get_fg_color("diffOldFile") }
else
return { fg = get_fg_color("diffRemoved") }
end
end,
on_click = function(button)
if button == "left" then
vim.cmd(":WakaTimeToday")
end
end,
})
-- Add the Music Current data to the lualine_y section
table.insert(config.sections.lualine_y, {
function()
return music_current.get_current() -- Always display the current state
end,
icon = music_current.get_icon(),
fmt = trunc(120, 20, 80, true),
on_click = function(button)
if button == "left" then
vim.cmd(":MusicPause")
elseif button == "right" then
vim.cmd(":MusicNext")
end
end,
})
-- Add word count to the lualine_y section
table.insert(config.sections.lualine_y, 1, {
function()
return vim.fn.wordcount().words
end,
icon = "",
fmt = trunc(140, 20, 140, true),
})
-- Add selection_count to the lualine_y section
table.insert(config.sections.lualine_y, 2, {
"selection_count",
fmt = trunc(120, 20, 120, true),
})
-- Add filesize to the lualine_y section
table.insert(config.sections.lualine_y, {
"filesize",
fmt = trunc(120, 20, 120, true),
})
-- Add search count to the lualine_z section
table.insert(config.sections.lualine_z, 1, {
"searchcount",
fmt = trunc(80, 20, 80, true),
color = "CurSearch",
separator = { left = "", right = "" },
icon = "󰍉 ",
})
-- Apply the new configuration
require("lualine").setup(config)
end
-- Use an autocommand to ensure it runs after lualine is set up
vim.api.nvim_create_autocmd("User", {
pattern = "LualineSetup",
callback = add_custom_sections_to_lualine,
})
-- Or use defer_fn if you're unsure about the exact timing
vim.defer_fn(add_custom_sections_to_lualine, 100) -- Delay in milliseconds
+214
View File
@@ -0,0 +1,214 @@
-- -----------------------------------------------------------------------------
-- -------------------------------- LUA-LINE -----------------------------------
-- -----------------------------------------------------------------------------
-- Function to convert RGB to hexadecimal
local function rgb_to_hex(rgb)
return string.format("#%02x%02x%02x", rgb[1], rgb[2], rgb[3])
end
local function get_fg_color(hlgroup)
local hl = vim.api.nvim_get_hl(0, { name = hlgroup, link = false })
local fg = hl.fg
if fg then
return rgb_to_hex({
bit.rshift(bit.band(fg, 0xFF0000), 16),
bit.rshift(bit.band(fg, 0x00FF00), 8),
bit.band(fg, 0x0000FF),
})
end
return nil
end
return {
"nvim-lualine/lualine.nvim",
event = "VeryLazy",
init = function()
vim.g.lualine_laststatus = vim.o.laststatus
if vim.fn.argc(-1) > 0 then
-- set an empty statusline till lualine loads
vim.o.statusline = " "
else
-- hide the statusline on the starter page
vim.o.laststatus = 0
end
end,
opts = function()
local icons = LazyVim.config.icons
-- Require the dependencies
local wakatime_stats = require("config.wakatime_stats")
local music_stats = require("config.music_stats")
vim.o.laststatus = vim.g.lualine_laststatus
local opts = {
options = {
theme = "auto",
globalstatus = vim.o.laststatus == 3,
disabled_filetypes = {
statusline = { "dashboard", "alpha", "ministarter" },
},
section_separators = { left = "", right = "" },
component_separators = { left = "", right = "" },
},
sections = {
lualine_a = { "mode" },
lualine_b = { "branch" },
lualine_c = {
LazyVim.lualine.root_dir(),
{
"diagnostics",
symbols = {
error = icons.diagnostics.Error,
warn = icons.diagnostics.Warn,
info = icons.diagnostics.Info,
hint = icons.diagnostics.Hint,
},
},
{
"filetype",
icon_only = true,
separator = "",
padding = { left = 1, right = 0 },
},
{ LazyVim.lualine.pretty_path() },
},
lualine_x = {
-- stylua: ignore
{
---@diagnostic disable-next-line: undefined-field
function() return require("noice").api.status.command.get() end,
---@diagnostic disable-next-line: undefined-field
cond = function() return package.loaded["noice"] and require("noice").api.status.command.has() end,
color = function() return LazyVim.ui.fg("Statement") end,
},
-- stylua: ignore
{
---@diagnostic disable-next-line: undefined-field
function() return require("noice").api.status.mode.get() end,
---@diagnostic disable-next-line: undefined-field
cond = function() return package.loaded["noice"] and require("noice").api.status.mode.has() end,
color = function() return LazyVim.ui.fg("Constant") end,
},
-- stylua: ignore
{
function() return "" .. require("dap").status() end,
cond = function() return package.loaded["dap"] and require("dap").status() ~= "" end,
color = function() return LazyVim.ui.fg("Debug") end,
},
-- stylua: ignore
{
require("lazy.status").updates,
cond = require("lazy.status").has_updates,
color = function() return LazyVim.ui.fg("Special") end,
},
{
"diff",
symbols = {
added = icons.git.added,
modified = icons.git.modified,
removed = icons.git.removed,
},
source = function()
local gitsigns = vim.b.gitsigns_status_dict
if gitsigns then
return {
added = gitsigns.added,
modified = gitsigns.changed,
removed = gitsigns.removed,
}
end
end,
},
{
function()
return wakatime_stats.get_icon_with_text() -- Display the icon and WakaTime data together
end,
color = function()
if wakatime_stats.get_total_minutes() >= 60 then
return { fg = get_fg_color("diffAdded") }
elseif wakatime_stats.get_total_minutes() >= 30 then
return { fg = get_fg_color("diffOldFile") }
else
return { fg = get_fg_color("diffRemoved") }
end
end,
on_click = function(button)
if button == "left" then
vim.cmd(":WakaTimeToday")
end
end,
},
},
lualine_y = {
{
function()
return music_stats.get_icon_with_text() -- Display the icon and current state together
end,
},
{
function()
return vim.fn.wordcount().words
end,
icon = "",
},
{
"progress",
separator = " ",
padding = { left = 1, right = 0 },
},
{
"location",
padding = { left = 0, right = 1 },
},
{
"selection_count",
},
{
"filesize",
},
},
lualine_z = {
{
"searchcount",
color = "CurSearch",
separator = { left = "", right = "" },
icon = "󰍉 ",
},
{
function()
return "" .. os.date("%R")
end,
},
},
},
extensions = { "neo-tree", "lazy" },
}
-- do not add trouble symbols if aerial is enabled
-- And allow it to be overriden for some buffer types (see autocmds)
if vim.g.trouble_lualine and LazyVim.has("trouble.nvim") then
local trouble = require("trouble")
local symbols = trouble.statusline({
mode = "symbols",
groups = {},
title = false,
filter = { range = true },
format = "{kind_icon}{symbol.name:Normal}",
hl_group = "lualine_c_normal",
})
table.insert(opts.sections.lualine_c, {
symbols and symbols.get,
cond = function()
return vim.b.trouble_lualine ~= false and symbols.has()
end,
})
end
return opts
end,
}