Skip to content

Commit

Permalink
feat: add todos in notes query
Browse files Browse the repository at this point in the history
nobody use whitespace in filenames
  • Loading branch information
phanen committed Apr 29, 2024
1 parent e5ca29a commit 777c4fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
25 changes: 24 additions & 1 deletion lua/fzf-lua-overlay/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ M.create_notes = function(_, opts)
if not query or query == '' then query = os.date('%m-%d') .. '.md' end
-- no ext -> append `.md`
local path = vim.fn.expand(('%s/%s'):format(opts.cwd, query))
if #(vim.split(query, '.', { plain = true })) == 1 then path = path .. '.md' end
local sec = vim.split(query, ' ', { trimempty = true })
local sec_nr = #sec

if sec_nr == 0 then return end
if sec_nr > 1 then return require('fzf-lua-overlay.actions').add_todos(query) end

-- non-suffix are md by default
if #(vim.split(sec[1], '.', { plain = true })) == 1 then path = path .. '.md' end

if not vim.uv.fs_stat(path) then
local u = require('fzf-lua-overlay.util')
local ok = u.write_file(path, nil, 'w')
Expand Down Expand Up @@ -62,4 +70,19 @@ M.rename_files = function(selected, opts)
vim.notify(('%s has been renamed to %s'):format(oldpath, newpath), vim.log.levels.INFO)
end

M.add_todos = function(query)
local line = query or require('fzf-lua').get_last_query()
local tag, content = unpack(vim.split(line, ': '))
if not tag or not content then
return vim.notify('format should be [tag: content]', vim.log.levels.WARN)
end
local u = require('fzf-lua-overlay.util')

local cfg = require('fzf-lua-overlay.config').opts
local filename = vim.fs.normalize(vim.fs.joinpath(cfg.todos_dir, tag)) .. '.md'
content = ('* %s\n'):format(content)
local ok = u.write_file(filename, content, 'a')
if not ok then return vim.notify('fail to write to storage', vim.log.levels.WARN) end
end

return M
17 changes: 1 addition & 16 deletions lua/fzf-lua-overlay/providers/todos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,7 @@ M.opts = {
winopts = { preview = { hidden = 'nohidden' } },
actions = {
['ctrl-g'] = function(...) end,
['ctrl-o'] = function()
-- open editor for writing
end,
['ctrl-n'] = function()
-- TODO: query `nvim: ` -> preview nvim entries? fzf match rules?
local line = require('fzf-lua').get_last_query()
local tag, content = unpack(vim.split(line, ': '))
if not tag or not content then
return vim.notify('format should be [tag: content]', vim.log.levels.WARN)
end
local u = require('fzf-lua-overlay.util')
local filename = vim.fs.normalize(vim.fs.joinpath(cfg.todos_dir, tag)) .. '.md'
content = ('* %s\n'):format(content)
local ok = u.write_file(filename, content, 'a')
if not ok then return vim.notify('fail to write to storage', vim.log.levels.WARN) end
end,
['ctrl-n'] = function() require('fzf-lua-overlay.actions').add_todos() end,
['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').delete_files(...) end,
},
fzf_opts = { ['--history'] = notes_history },
Expand Down

0 comments on commit 777c4fd

Please sign in to comment.