Skip to content

Commit

Permalink
refactor: extract truncate logic
Browse files Browse the repository at this point in the history
Co-authored-by: Micah Halter <[email protected]>
  • Loading branch information
azdanov and mehalter authored Oct 17, 2024
1 parent 092b571 commit 9463ab8
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/content/docs/recipes/cmp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,20 @@ return {
"hrsh7th/nvim-cmp",
opts = function(_, opts)
local astrocore, astroui = require "astrocore", require "astroui"
local function truncate(str, len)
if not str then return end
local truncated = vim.fn.strcharpart(str, 0, len)
return truncated == str and str or truncated .. astroui.get_icon "Ellipsis"
end

if not opts.formatting then opts.formatting = {} end
opts.formatting.format = astrocore.patch_func(opts.formatting.format, function(format, ...)
-- get item from original formatting function
local vim_item = format(...)

-- truncate label to maximum of 25% of the window
local label = vim_item.abbr
local truncated_label = vim.fn.strcharpart(label, 0, math.floor(0.25 * vim.o.columns))
if truncated_label ~= label then vim_item.abbr = truncated_label .. astroui.get_icon "Ellipsis" end

-- truncate menu to maximum of 25% of the window
if vim_item.menu then
local menu = vim_item.menu
local truncated_menu = vim.fn.strcharpart(menu, 0, math.floor(0.25 * vim.o.columns))
if truncated_menu ~= menu then vim_item.menu = truncated_menu .. astroui.get_icon "Ellipsis" end
end
-- truncate text fields to maximum of 25% of the window
vim_item.abbr = truncate(vim_item.abbr, math.floor(0.25 * vim.o.columns))
vim_item.menu = truncate(vim_item.menu, math.floor(0.25 * vim.o.columns))

return vim_item
end)
Expand Down

0 comments on commit 9463ab8

Please sign in to comment.