Skip to content

Commit

Permalink
chore: minor rework
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed Apr 12, 2024
1 parent cd7205d commit f4ddb48
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 123 deletions.
22 changes: 20 additions & 2 deletions lua/fzf-lua-overlay/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
14 changes: 1 addition & 13 deletions lua/fzf-lua-overlay/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions lua/fzf-lua-overlay/overlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
19 changes: 19 additions & 0 deletions lua/fzf-lua-overlay/providers/find_notes.lua
Original file line number Diff line number Diff line change
@@ -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,
},
}
100 changes: 52 additions & 48 deletions lua/fzf-lua-overlay/providers/gitignore.lua
Original file line number Diff line number Diff line change
@@ -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
100 changes: 52 additions & 48 deletions lua/fzf-lua-overlay/providers/license.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f4ddb48

Please sign in to comment.