49 lines
1.4 KiB
Lua
49 lines
1.4 KiB
Lua
-- Shared helper functions for use inside snippet function_node()/dynamic_node()
|
|
|
|
local M = {}
|
|
|
|
function M.today()
|
|
return os.date("%Y-%m-%d")
|
|
end
|
|
|
|
function M.timestamp()
|
|
return os.date("| %Y-%m-%d %H:%M | ")
|
|
end
|
|
|
|
function M.clipboard_or(default)
|
|
return function()
|
|
local clip = vim.fn.getreg "+"
|
|
return clip ~= "" and clip or default
|
|
end
|
|
end
|
|
|
|
-- Space padding to center `text` between fixed-width markers (e.g. "###")
|
|
-- so the whole line (markers + padding + text) is exactly `width` chars.
|
|
local function side_pad(text, width, marker_len)
|
|
local interior = width - marker_len * 2
|
|
local pad = math.max(interior - #text, 0)
|
|
local left = math.floor(pad / 2)
|
|
local right = pad - left
|
|
return left, right
|
|
end
|
|
|
|
-- Use as f(util.pad_left(100, 3), { N }) where N is the node index holding
|
|
-- the text to center, placed right after the opening marker.
|
|
function M.pad_left(width, marker_len)
|
|
width, marker_len = width or 100, marker_len or 3
|
|
return function(args)
|
|
local left = side_pad(args[1][1] or "", width, marker_len)
|
|
return string.rep(" ", left)
|
|
end
|
|
end
|
|
|
|
-- Companion to pad_left(), placed right before the closing marker.
|
|
function M.pad_right(width, marker_len)
|
|
width, marker_len = width or 100, marker_len or 3
|
|
return function(args)
|
|
local _, right = side_pad(args[1][1] or "", width, marker_len)
|
|
return string.rep(" ", right)
|
|
end
|
|
end
|
|
|
|
return M
|