Skip to content

Commit

Permalink
feat(core.ui.calendar): implement more of the barebones UI
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed May 14, 2023
1 parent 7dbbe9d commit 364f44a
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions lua/neorg/modules/core/ui/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ module.public = {

-- Next, we need two namespaces: one for rendering decorational
-- extmarks, and one for logical operations.
-- TODO: Break out the logical namespace into a separate `do` block?
local decorational_namespace, logical_namespace = vim.api.nvim_create_namespace("neorg/calendar/decorational"), vim.api.nvim_create_namespace("neorg/calendar/logical")
local decorational_namespace = vim.api.nvim_create_namespace("neorg/calendar/decorational")

vim.api.nvim_buf_clear_namespace(buffer, decorational_namespace, 0, -1)
vim.api.nvim_buf_clear_namespace(buffer, logical_namespace, 0, -1)

--> Decorational section
-- CALENDAR text:
Expand All @@ -56,6 +54,55 @@ module.public = {
virt_text_pos = "overlay"
})
end

local logical_namespace = vim.api.nvim_create_namespace("neorg/calendar/logical")

vim.api.nvim_buf_clear_namespace(buffer, logical_namespace, 0, -1)

local year, month, day = os.date("%Y-%m-%d"):match("(%d+)%-(%d+)%-(%d+)")

-- Display the current year
local year_extmark = vim.api.nvim_buf_set_extmark(buffer, logical_namespace, 2, half_width - math.floor(string.len("< " .. tostring(year) .. " >") / 2), {
virt_text = { { "< ", "Whitespace" }, { tostring(year), "TSNumber" }, { " >", "Whitespace" } },
virt_text_pos = "overlay",
})

--> Month rendering routine
-- We render the first month at the very center of the screen. Each month takes up a static 26 characters.

-- Render the top text of the month (June, August etc.)
-- The top text displays the abbreviated month
-- TODO: Extract this logic out into a function because different views
-- will supply different abbreviations to render.
local abbreviated_month_name = os.date("%b", os.time({
year = year,
month = month,
day = day,
}))

vim.api.nvim_buf_set_extmark(buffer, logical_namespace, 4, half_width - math.floor(string.len(abbreviated_month_name) / 2), {
virt_text = { { abbreviated_month_name, "TSUnderline" } },
virt_text_pos = "overlay",
})

-- Render the days of the week
-- To effectively do this, we grab all the weekdays from a constant time.
-- This makes the weekdays retrieved locale dependent (which is what we want).
local weekdays = {}

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

vim.api.nvim_buf_set_extmark(buffer, logical_namespace, 5, half_width - math.floor(#weekdays / 2) + 1, {
virt_text = weekdays,
virt_text_pos = "overlay",
})
end,

select_date = function(options)
Expand Down

0 comments on commit 364f44a

Please sign in to comment.