Skip to content
This repository has been archived by the owner on Feb 20, 2025. It is now read-only.

Commit

Permalink
perf(zettelkasten): check title width
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdjeg committed Dec 22, 2024
1 parent afe49af commit 113110b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions bundle/vim-zettelkasten/lua/zettelkasten/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
--=============================================================================

local M = {}

local function str2chars(str)
local t = {}
for _, k in ipairs(vim.fn.split(str, '\\zs')) do
table.insert(t, k)
end
return t
end

local s_formatters = {
['%r'] = function(line)
return #line.references
Expand All @@ -18,10 +27,19 @@ local s_formatters = {
return vim.fn.fnamemodify(line.file_name, ':t')
end,
['%h'] = function(line)
if vim.fn.strdisplaywidth(line.title) <= 30 then
if vim.fn.strdisplaywidth(line.title) < 30 then
return line.title .. string.rep(' ', 30 - vim.fn.strdisplaywidth(line.title))
else
return string.sub(line.title, 1, 27) .. '...'
local t = ''
for _, char in ipairs(str2chars(line.title)) do
if vim.fn.strdisplaywidth(t) + vim.fn.strdisplaywidth(char) <= 27 then
t = t .. char
else
break
end
end
t = t .. '...'
return t .. string.rep(' ', 30 - vim.fn.strdisplaywidth(t))
end
end,
['%d'] = function(line)
Expand Down

0 comments on commit 113110b

Please sign in to comment.