Automatically generate vimwiki reviews index

pull/1/head
Ensar Sarajčić 2021-03-06 22:23:05 +01:00
parent 3ae57193de
commit efccf421c8
3 changed files with 177 additions and 19 deletions

View File

@ -36,9 +36,5 @@ vim.api.nvim_set_keymap('n', '<Leader>c', ':ccl <bar> lcl<CR>', {noremap = true}
-- Allow recursive searches
vim.cmd('set path+=**')
-- Save whenever switching windows or leaving vim. This is useful when running
-- the tests inside vim without having to save all files first.
vim.cmd('autocmd FocusLost,WinLeave * :silent! wa')
-- automatically rebalance windows on vim resize
vim.cmd('autocmd VimResized * :wincmd =')

View File

@ -53,29 +53,29 @@ local M = {}
-- Prints current branches PR url (not saved to :messages)
-- Makes it easy to use terminal for opening url on click
M.print_pr_url = function(...)
function M.print_pr_url(...)
vim.cmd('echo "' .. get_pr_url(...) .. '"')
end
-- Copies current branches PR url to system clipboard
M.copy_pr_url = function(...)
function M.copy_pr_url(...)
vim.cmd('let @+ = "' .. get_pr_url(...) .. '"')
end
-- Opens current banches PR url in default browser
-- Utilizes netrw browse, meaning it should behave same as netrw
M.open_new_pr = function(...)
function M.open_new_pr(...)
vim.fn['netrw#BrowseX'](get_pr_url(...), 0)
end
-- Creates new branch and checks out to it
-- Similar to `gcb` in fish config
M.create_branch = function(branch)
function M.create_branch(branch)
vim.cmd('Git checkout -b ' .. branch)
end
-- Switches to branch
M.checkout_branch = function(branch)
function M.checkout_branch(branch)
vim.cmd('Git checkout ' .. branch)
end

View File

@ -2,6 +2,9 @@
-- - Vimwiki reviews extension library -
-------------------------------------------------------------------------------
local Path = require('plenary.path')
local scandir = require('plenary.scandir')
-- Gets path to reviews dir of provided vimwiki (by index)
local function get_reviews_dir(...)
local vimwiki = {}
@ -34,6 +37,7 @@ local function read_review_template_into_buffer(vimwiki_reviews_path, review_typ
if (vim.fn.filereadable(vim.fn.glob(template_path)))
then
vim.cmd('read ' .. template_path)
vim.cmd('0d')
else
if (review_type == 'week')
then
@ -48,25 +52,36 @@ local function read_review_template_into_buffer(vimwiki_reviews_path, review_typ
end
end
local function file_exists_or_open(file_name)
local exists = Path:new(Path:new(file_name):expand()):exists()
local open = vim.fn.bufwinnr(file_name) > 0
return exists or open
end
local function get_filename_from_path(path)
local _, filename, _ = string.match(path, "(.-)([^\\/]-%.?([^%.\\/]*))$")
return filename
end
-------------------------------------------------------------------------------
-- - Public API -
-------------------------------------------------------------------------------
local M = {}
-- Edits weekly review template
M.open_review_weekly_template = function(...)
function M.open_review_weekly_template(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'week'))
end
-- Edits monthly review template
M.open_review_monthly_template = function(...)
function M.open_review_monthly_template(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'month'))
end
-- Edits yearly review template
M.open_review_yearly_template = function(...)
function M.open_review_yearly_template(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. get_review_template_path(reviews_dir, 'year'))
end
@ -74,12 +89,12 @@ end
-- Open current week weekly review file
-- Created buffer is dated to Sunday of current week
-- Opens current week because Sunday is good time to do this review
M.vimwiki_weekly_review = function(...)
function M.vimwiki_weekly_review(...)
local reviews_dir = get_reviews_dir(...)
local days_to_sunday = 7 - tonumber(os.date('%u'))
local week_date = os.date('%Y-%m-%d', os.time() + days_to_sunday * 24 * 60 * 60)
local file_name = reviews_dir .. week_date .. '-week.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
local exists = file_exists_or_open(file_name)
vim.cmd('edit ' .. file_name)
if (exists == false)
then
@ -93,12 +108,12 @@ end
-- Created buffer is dated to previous month
-- Previous month is calculated in an erroneous way
-- 28 days are subtracted from current time to get previous month
M.vimwiki_monthly_review = function(...)
function M.vimwiki_monthly_review(...)
local reviews_dir = get_reviews_dir(...)
local month_time = os.time() - 28 * 24 * 60 * 60
local month_date = os.date('%Y-%m', month_time)
local file_name = reviews_dir .. month_date .. '-month.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
local exists = file_exists_or_open(file_name)
vim.cmd('edit ' .. file_name)
if (exists == false)
then
@ -109,11 +124,11 @@ end
-- Open past year yearly review file
-- Created buffer is dated to previous year
M.vimwiki_yearly_review = function(...)
function M.vimwiki_yearly_review(...)
local reviews_dir = get_reviews_dir(...)
local year_date = (tonumber(os.date('%Y')) - 1)
local file_name = reviews_dir .. year_date .. '-year.md'
local exists = vim.fn.filereadable(vim.fn.glob(file_name)) == 1
local exists = file_exists_or_open(file_name)
vim.cmd('edit ' .. file_name)
if (exists == false)
then
@ -122,10 +137,157 @@ M.vimwiki_yearly_review = function(...)
end
end
-- Generates review index table
-- Takes arguments similar to all vimwiki calls
-- (no args, or 1 arg representing vimwiki index)
function M.get_review_index(...)
local reviews_dir = get_reviews_dir(...)
local reviews_path = Path:new(reviews_dir):expand()
local entries = scandir.scan_dir(
reviews_path,
{
hidden = false,
add_dirs = false,
respect_gitignore = true,
depth = 1
})
local index = {}
-- TODO better default values handling
for _,entry in pairs(entries) do
local filename = get_filename_from_path(entry)
if (string.match(filename, '^%d*-year.md$'))
then
local year = string.match(filename, '(%d*)-year')
if (index[year] == nil)
then
index[year] = {
months = {}
}
end
index[year].year = filename
elseif (string.match(filename, '^%d*-%d*-month.md$'))
then
local year = string.match(filename, '(%d*)-%d*-month')
local month = string.match(filename, '(%d*)-month')
if (index[year] == nil)
then
index[year] = {
months = {}
}
end
if (index[year].months[month] == nil)
then
index[year].months[month] = {
weeks = {}
}
end
index[year].months[month].month = filename
elseif (string.match(filename, '^%d*-%d*-%d*-week.md$'))
then
local year = string.match(filename, '(%d*)-%d*-%d*-week')
local month = string.match(filename, '(%d*)-%d*-week')
local week = string.match(filename, '(%d*)-week')
if (index[year] == nil)
then
index[year] = {
months = {}
}
end
if (index[year].months[month] == nil)
then
index[year].months[month] = {
weeks = {}
}
end
index[year].months[month].weeks[week] = filename
end
end
return index
end
-- Open reviews index file
M.vimwiki_review_index = function(...)
function M.vimwiki_review_index(...)
local reviews_dir = get_reviews_dir(...)
vim.cmd('edit ' .. reviews_dir .. 'reviews.md')
local index = M.get_review_index(...)
local lines = {
'# Reviews',
'',
}
local month_names = {
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
}
-- Sort index by year
local years = {}
for k in pairs(index) do table.insert(years, k) end
table.sort(years, function(a, b) return a > b end)
local tablelength = function(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- Add years
for _, year in pairs(years) do
table.insert(lines, '## ' .. year)
table.insert(lines, '')
if (index[year].year)
then
table.insert(lines, '- [Yearly review](' .. index[year].year .. ')')
table.insert(lines, '')
end
-- Sort months
local months = {}
for k in pairs(index[year].months) do table.insert(months, k) end
table.sort(months, function(a, b) return a > b end)
-- Add months
for _, month in pairs(months) do
table.insert(lines, '### ' .. month_names[tonumber(month)])
table.insert(lines, '')
if (index[year].months[month].month)
then
table.insert(lines, '- [Monthly review](' .. index[year].months[month].month .. ')')
end
-- Sort weeks
local weeks = index[year].months[month].weeks
table.sort(weeks, function(a, b) return a > b end)
-- Add weeks
local count = tablelength(weeks)
for _, week in pairs(weeks) do
table.insert(lines, '- [Week #' .. count .. ' Review](' .. week .. ')')
count = count - 1
end
table.insert(lines, '')
end
end
local buf = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {}) -- Clear out
vim.api.nvim_buf_set_lines(buf, 0, 0, false, lines) -- Put new contents
end
return M