diff --git a/lua/fzf-lua-overlay/actions.lua b/lua/fzf-lua-overlay/actions.lua index 9345225..e6402f0 100644 --- a/lua/fzf-lua-overlay/actions.lua +++ b/lua/fzf-lua-overlay/actions.lua @@ -31,8 +31,10 @@ end M.delete_files = function(selected, opts) -- TODO: multi? - local cwd = opts.cwd or vim.fn.getcwd() - local path = vim.fn.expand(('%s/%s'):format(cwd, selected[1])) + -- local cwd = opts.cwd or vim.fn.getcwd() + -- local path = vim.fn.expand(('%s/%s'):format(cwd, selected[1])) + local file = require('fzf-lua').path.entry_to_file(selected[1], opts) + local path = file.path local _fn, _opts = opts.__call_fn, opts.__call_opts require('fzf-lua').fzf_exec({ 'YES', 'NO' }, { prompt = ('Delete %s'):format(path), @@ -48,4 +50,20 @@ M.delete_files = function(selected, opts) }) end +-- used by fzf's builtin file pickers +M.rename_files = function(selected, opts) + local file = require('fzf-lua').path.entry_to_file(selected[1], opts) + local oldpath = file.path + local oldname = vim.fs.basename(oldpath) + local newname = vim.fn.input('New name: ', oldname) + newname = vim.trim(newname) + if newname == '' or newname == oldname then + return + end + local cwd = opts.cwd or vim.fn.getcwd() + local newpath = ('%s/%s'):format(cwd, newname) + vim.uv.fs_rename(oldpath, newpath) + vim.notify(('%s has been renamed to %s'):format(oldpath, newpath), vim.log.levels.INFO) +end + return M diff --git a/lua/fzf-lua-overlay/config.lua b/lua/fzf-lua-overlay/config.lua index 9702192..bf29177 100644 --- a/lua/fzf-lua-overlay/config.lua +++ b/lua/fzf-lua-overlay/config.lua @@ -3,25 +3,13 @@ local M = {} local default_opts = { dot_dir = '~', notes_dir = '~/notes', - cache_dir = vim.fs.joinpath(vim.fn.stdpath 'cache', 'fzf-lua-overlay'), - -- stylua: ignore - notes_actions = { - ['ctrl-g'] = function(...) require('fzf-lua-overlay.actions').toggle_daily(...) end, - ['ctrl-n'] = function(...) require('fzf-lua-overlay.actions').create_notes(...) end, - ['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').delete_files(...) end, - }, + cache_dir = vim.fs.joinpath(vim.g.cache_dir or vim.fn.stdpath 'cache', 'fzf-lua-overlay'), } M.opts = default_opts M.setup = function(opts) M.opts = vim.tbl_deep_extend('force', default_opts, opts or {}) - - local cache_dir = M.opts.cache_dir - M.opts.notes_history = vim.fs.joinpath(cache_dir, 'notes_history') - M.opts.gitignore = vim.fs.joinpath(cache_dir, 'gitignore.json') - M.opts.license = vim.fs.joinpath(cache_dir, 'license.json') - if not vim.uv.fs_stat(M.opts.cache_dir) then vim.fn.mkdir(M.opts.cache_dir) end diff --git a/lua/fzf-lua-overlay/overlay.lua b/lua/fzf-lua-overlay/overlay.lua index 2b54c40..96975f9 100644 --- a/lua/fzf-lua-overlay/overlay.lua +++ b/lua/fzf-lua-overlay/overlay.lua @@ -12,18 +12,6 @@ local overlay = setmetatable({ grep_dots = { name = 'live_grep_native', opts = { cwd = cfg.dot_dir } }, grep_notes = { name = 'live_grep_native', opts = { cwd = cfg.notes_dir } }, todo_comment = { name = 'grep', opts = { search = 'TODO|HACK|PERF|NOTE|FIX', no_esc = true } }, - find_notes = { - name = 'files', - opts = { - cwd = cfg.notes_dir, - actions = cfg.notes_actions, - fzf_opts = { - ['--history'] = cfg.notes_history, - }, - file_icons = false, - git_icons = false, - }, - }, }, { __index = function(t, k) local ok, ret = pcall(require, ('fzf-lua-overlay.providers.%s'):format(k)) diff --git a/lua/fzf-lua-overlay/providers/find_notes.lua b/lua/fzf-lua-overlay/providers/find_notes.lua new file mode 100644 index 0000000..f667c81 --- /dev/null +++ b/lua/fzf-lua-overlay/providers/find_notes.lua @@ -0,0 +1,19 @@ +local cfg = require('fzf-lua-overlay.config').opts + +local notes_history = vim.fs.joinpath(cfg.cache_dir, 'notes_history') + +return { + name = 'files', + opts = { + cwd = cfg.notes_dir, + -- stylua: ignore + actions = { + ['ctrl-g'] = function(...) require('fzf-lua-overlay.actions').toggle_daily(...) end, + ['ctrl-n'] = function(...) require('fzf-lua-overlay.actions').create_notes(...) end, + ['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').delete_files(...) end, + }, + fzf_opts = { ['--history'] = notes_history }, + file_icons = false, + git_icons = false, + }, +} diff --git a/lua/fzf-lua-overlay/providers/gitignore.lua b/lua/fzf-lua-overlay/providers/gitignore.lua index 48a94e2..035c79f 100644 --- a/lua/fzf-lua-overlay/providers/gitignore.lua +++ b/lua/fzf-lua-overlay/providers/gitignore.lua @@ -1,55 +1,59 @@ +local M = {} + local url = 'https://api.github.com/gitignore/templates' -return { - name = 'fzf_exec', - opts = { - prompt = 'gitignore> ', - actions = { - ['default'] = function(selected) - local util = require('fzf-lua-overlay.util') - local gitroot = util.find_gitroot() - if not gitroot then - vim.notify('not in a git repository') - end - local path = vim.fs.joinpath(gitroot, '.gitignore') - vim.print(path) - if vim.uv.fs_stat(path) then - local confirm = vim.fn.confirm('Override?', '&Yes\n&No') - if confirm ~= 1 then - return - end +local cache_dir = require('fzf-lua-overlay.config').opts.cache_dir +local cache_path = vim.fs.joinpath(cache_dir, 'gitignore.json') + +M.name = 'fzf_exec' + +M.opts = { + prompt = 'gitignore> ', + actions = { + ['default'] = function(selected) + local util = require('fzf-lua-overlay.util') + local gitroot = util.find_gitroot() + if not gitroot then + vim.notify('not in a git repository') + end + local path = vim.fs.joinpath(gitroot, '.gitignore') + vim.print(path) + if vim.uv.fs_stat(path) then + local confirm = vim.fn.confirm('Override?', '&Yes\n&No') + if confirm ~= 1 then + return end - local template_url = ('%s/%s'):format(url, selected[1]) - local content = vim.fn.system { 'curl', '-s', template_url } - content = vim.json.decode(content).source - util.write_file(path, content) - vim.cmd.e(path) - end, - }, + end + local template_url = ('%s/%s'):format(url, selected[1]) + local content = vim.fn.system({ 'curl', '-s', template_url }) + content = vim.json.decode(content).source + util.write_file(path, content) + vim.cmd.e(path) + end, }, - fzf_exec_arg = function(fzf_cb) - local util = require('fzf-lua-overlay.util') - local cfg = require('fzf-lua-overlay.config').opts +} + +M.fzf_exec_arg = function(fzf_cb) + local util = require('fzf-lua-overlay.util') - local path = cfg.gitignore + local json + if not vim.uv.fs_stat(cache_path) then + local json_str = vim.fn.system({ 'curl', '-s', url }) + util.write_file(cache_path, json_str) + json = vim.json.decode(json_str) + end + json = json or util.read_json(cache_path) - local json - if not vim.uv.fs_stat(path) then - local json_str = vim.fn.system { 'curl', '-s', url } - util.write_file(path, json_str) - json = vim.json.decode(json_str) + coroutine.wrap(function() + local co = coroutine.running() + for _, item in ipairs(json) do + fzf_cb(item, function() + coroutine.resume(co) + end) + coroutine.yield() end - json = json or util.read_json(path) - - coroutine.wrap(function() - local co = coroutine.running() - for _, item in ipairs(json) do - fzf_cb(item, function() - coroutine.resume(co) - end) - coroutine.yield() - end - fzf_cb() - end)() - end, -} + fzf_cb() + end)() +end + +return M diff --git a/lua/fzf-lua-overlay/providers/license.lua b/lua/fzf-lua-overlay/providers/license.lua index faacdf0..e255833 100644 --- a/lua/fzf-lua-overlay/providers/license.lua +++ b/lua/fzf-lua-overlay/providers/license.lua @@ -1,55 +1,59 @@ local url = 'https://api.github.com/licenses' -return { - name = 'fzf_exec', - opts = { - prompt = 'license> ', - actions = { - ['default'] = function(selected) - local util = require('fzf-lua-overlay.util') - local gitroot = util.find_gitroot() - if not gitroot then - vim.notify('not in a git repository') - end - local path = vim.fs.joinpath(gitroot, 'LICENSE') - vim.print(path) - if vim.uv.fs_stat(path) then - local confirm = vim.fn.confirm('Override?', '&Yes\n&No') - if confirm ~= 1 then - return - end +local cache_dir = require('fzf-lua-overlay.config').opts.cache_dir +local cache_path = vim.fs.joinpath(cache_dir, 'license.json') + +local M = {} + +M.name = 'fzf_exec' +M.prompt = 'license> ' + +M.opts = { + actions = { + ['default'] = function(selected) + local util = require('fzf-lua-overlay.util') + local gitroot = util.find_gitroot() + if not gitroot then + vim.notify('not in a git repository') + end + local path = vim.fs.joinpath(gitroot, 'LICENSE') + vim.print(path) + if vim.uv.fs_stat(path) then + local confirm = vim.fn.confirm('Override?', '&Yes\n&No') + if confirm ~= 1 then + return end - local template_url = ('%s/%s'):format(url, selected[1]) - local content = vim.fn.system { 'curl', '-s', template_url } - content = vim.json.decode(content).body - util.write_file(path, content) - vim.cmd.e(path) - end, - }, + end + local template_url = ('%s/%s'):format(url, selected[1]) + local content = vim.fn.system { 'curl', '-s', template_url } + content = vim.json.decode(content).body + util.write_file(path, content) + vim.cmd.e(path) + end, }, - fzf_exec_arg = function(fzf_cb) - local util = require('fzf-lua-overlay.util') - local cfg = require('fzf-lua-overlay.config').opts +} + +M.fzf_exec_arg = function(fzf_cb) + local util = require('fzf-lua-overlay.util') - local path = cfg.license + local json + if not vim.uv.fs_stat(cache_path) then + local json_str = vim.fn.system { 'curl', '-s', url } + util.write_file(cache_path, json_str) + json = vim.json.decode(json_str) + end + json = json or util.read_json(cache_path) - local json - if not vim.uv.fs_stat(path) then - local json_str = vim.fn.system { 'curl', '-s', url } - util.write_file(path, json_str) - json = vim.json.decode(json_str) + coroutine.wrap(function() + local co = coroutine.running() + for _, item in ipairs(json) do + fzf_cb(item.key, function() + coroutine.resume(co) + end) + coroutine.yield() end - json = json or util.read_json(path) - - coroutine.wrap(function() - local co = coroutine.running() - for _, item in ipairs(json) do - fzf_cb(item.key, function() - coroutine.resume(co) - end) - coroutine.yield() - end - fzf_cb() - end)() - end, -} + fzf_cb() + end)() +end + +return M