✨ feat(lualine): implement custom lualine sections
Implements lualine sections for WakaTime, wordcount, searchcount, music metadata, git-blame and more! Also implements automatic truncating throughout so that sections will truncate or hide depending on the available space.
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- -------------------------------- 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
|
||||
|
||||
-- Ensure the function is called only once
|
||||
local lualine_updated = false
|
||||
|
||||
-- Function to add WakaTime and Music Current data 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_current")
|
||||
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,
|
||||
})
|
||||
|
||||
-- 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),
|
||||
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_y section
|
||||
table.insert(config.sections.lualine_y, 1, {
|
||||
"searchcount",
|
||||
fmt = trunc(80, 20, 80, true),
|
||||
})
|
||||
|
||||
-- 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
|
||||
@@ -0,0 +1,81 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- ------------------------------ MUSIC-CURREN ---------------------------------
|
||||
-- -----------------------------------------------------------------------------T
|
||||
|
||||
local M = {}
|
||||
local cache = {
|
||||
text = "", -- Default to empty string to handle initial state
|
||||
timestamp = 0,
|
||||
}
|
||||
local cache_duration = 1 -- Cache duration in seconds
|
||||
|
||||
-- List of players to ignore
|
||||
local ignored_players = {
|
||||
"firefox",
|
||||
"kdeconnect",
|
||||
"plasma-browser-integration",
|
||||
}
|
||||
|
||||
local function get_ignored_players_string()
|
||||
return table.concat(ignored_players, ",")
|
||||
end
|
||||
|
||||
local function get_music_current()
|
||||
local ignored_players_str = get_ignored_players_string()
|
||||
local cmd = string.format(
|
||||
"playerctl --ignore-player %s metadata --format '{{ artist }} - {{ title }}' 2>/dev/null",
|
||||
ignored_players_str
|
||||
)
|
||||
|
||||
local result = vim.call("system", cmd)
|
||||
|
||||
if not result then
|
||||
return "" -- Return empty string for no music
|
||||
end
|
||||
|
||||
-- Check for "No players found" message
|
||||
if result:match("No players found") then
|
||||
return "" -- Return empty string for no music
|
||||
end
|
||||
|
||||
local artist_title = result:match("^(.-)%s*$") -- Trim any whitespace
|
||||
return artist_title or "" -- Return empty string if no artist/title
|
||||
end
|
||||
|
||||
local function get_music_icon()
|
||||
local ignored_players_str = get_ignored_players_string()
|
||||
local cmd = string.format(
|
||||
"playerctl --ignore-player %s status 2>/dev/null",
|
||||
ignored_players_str
|
||||
)
|
||||
|
||||
local result = vim.call("system", cmd)
|
||||
|
||||
if not result then
|
||||
return " " -- Default icon for music stopped
|
||||
end
|
||||
|
||||
-- Determine the icon based on the player status
|
||||
if result:match("Playing") then
|
||||
return " " -- Icon for music playing
|
||||
elseif result:match("Paused") then
|
||||
return " " -- Icon for music paused
|
||||
else
|
||||
return " " -- Default icon for music stopped
|
||||
end
|
||||
end
|
||||
|
||||
function M.get_current()
|
||||
local current_time = os.time()
|
||||
if current_time - cache.timestamp > cache_duration then
|
||||
cache.text = get_music_current()
|
||||
cache.timestamp = current_time
|
||||
end
|
||||
return cache.text
|
||||
end
|
||||
|
||||
function M.get_icon()
|
||||
return get_music_icon()
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,7 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- ---------------------------------- POST -------------------------------------
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Post-plugin configuration functions
|
||||
|
||||
-- Add custom data to lualine
|
||||
require("config.lua_line")
|
||||
@@ -0,0 +1,47 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- ----------------------------- WAKATIME-CACHE --------------------------------
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
local M = {}
|
||||
local cache = {
|
||||
value = nil,
|
||||
timestamp = 0,
|
||||
}
|
||||
local cache_duration = 60 -- Cache duration in seconds
|
||||
|
||||
local function get_wakatime_today()
|
||||
local result = vim.call("system", "~/.wakatime/wakatime-cli --today 2>&1")
|
||||
if not result or result == "" then
|
||||
return "Error: Unable to read wakatime-cli output"
|
||||
end
|
||||
|
||||
-- Handle common error messages
|
||||
if
|
||||
result:match("command not found")
|
||||
or result:match("No such file or directory")
|
||||
then
|
||||
return "Error: wakatime-cli not found"
|
||||
end
|
||||
|
||||
return result:match("^%s*(.-)%s*$") -- Trim any whitespace
|
||||
end
|
||||
|
||||
function M.get_today()
|
||||
local current_time = os.time()
|
||||
if current_time - cache.timestamp > cache_duration then
|
||||
cache.value = get_wakatime_today()
|
||||
cache.timestamp = current_time
|
||||
end
|
||||
return cache.value
|
||||
end
|
||||
|
||||
function M.get_icon()
|
||||
local text = M.get_today()
|
||||
if text and text:match("Error:") then
|
||||
return " " -- Icon for error or issue
|
||||
else
|
||||
return " " -- Default icon
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user