Skip to content

Commit

Permalink
fix(calendar): display weekdays based on nvim_strwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 authored and vhyrro committed Oct 29, 2023
1 parent 5980a03 commit 5eadb3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
17 changes: 17 additions & 0 deletions lua/neorg/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,21 @@ function utils.wrap_dotrepeat(event_handler)
end
end

--- Truncate input str to fit inside the col_limit when displayed. Takes non-ascii chars into account.
---@param str string
---@param col_limit integer #str will be cut so that when displayed, the display length does not exceed limit
---@return string #substring of input str
function utils.truncate_by_cell(str, col_limit)
if str and str:len() == vim.api.nvim_strwidth(str) then
return vim.fn.strcharpart(str, 0, col_limit)
end
local short = vim.fn.strcharpart(str, 0, col_limit)
if vim.api.nvim_strwidth(short) > col_limit then
while vim.api.nvim_strwidth(short) > col_limit do
short = vim.fn.strcharpart(short, 0, vim.fn.strchars(short) - 1)
end
end
return short
end

return utils
39 changes: 15 additions & 24 deletions lua/neorg/modules/core/ui/calendar/views/monthly.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local neorg = require("neorg.core")
local lib, log, modules = neorg.lib, neorg.log, neorg.modules
local lib, log, modules, utils = neorg.lib, neorg.log, neorg.modules, neorg.utils

local module = modules.create("core.ui.calendar.views.monthly")

Expand Down Expand Up @@ -111,9 +111,9 @@ module.private = {
day = date.day,
})
)

-- NOTE(vhyrro): This is just here to make the language server's type annotator happy.
assert(type(month_name) == "string")
---@cast month_name string
-- local month_length = vim.api.nvim_strwidth(month_name)
local month_length = string.len(month_name)

local weekday_banner_id = vim.api.nvim_buf_get_extmark_by_id(
ui_info.buffer,
Expand All @@ -129,8 +129,8 @@ module.private = {
4,
weekday_banner_id[2]
+ math.ceil((weekday_banner_id[3].end_col - weekday_banner_id[2]) / 2)
- math.floor(month_name:len() / 2),
month_name:len(),
- math.floor(month_length / 2),
month_length,
{ { month_name, "@text.underline" } },
nil,
{
Expand All @@ -148,26 +148,17 @@ module.private = {
-- This makes the weekdays retrieved locale dependent (which is what we want).
local weekdays = {}
local weekdays_string_length = 0

for i = 1, 7 do
table.insert(weekdays, {
os.date(
"%a",
os.time({
year = 2000,
month = 5,
day = i,
})
):sub(1, 2),
"@text.title",
})

if i ~= 7 then
table.insert(weekdays, { " " })
end

weekdays_string_length = weekdays_string_length + (i ~= 7 and 4 or 2)
local weekday = os.date("%a", os.time({ year = 2000, month = 5, day = i }))
---@cast weekday string
local truncated = utils.truncate_by_cell(weekday, 2)
local truncated_length = vim.api.nvim_strwidth(truncated)
weekdays[#weekdays + 1] = { truncated, "@text.title" }
weekdays[#weekdays + 1] = { (" "):rep(4 - truncated_length) }
weekdays_string_length = truncated_length -- remember last day's length
end
weekdays[#weekdays] = nil -- delete last padding
weekdays_string_length = weekdays_string_length + 4 * 6

-- This serves as the index of this week banner extmark inside the extmark table
local absolute_offset = offset + (offset < 0 and (-offset * 100) or 0)
Expand Down

0 comments on commit 5eadb3c

Please sign in to comment.