Skip to content

Commit

Permalink
refactor: share previewers
Browse files Browse the repository at this point in the history
also cleanup fzf previewer not fancy which we never use
phanen committed Oct 11, 2024
1 parent 9bf0989 commit 632e91b
Showing 6 changed files with 56 additions and 111 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -113,3 +113,4 @@ vim.api.nvim_create_autocmd("BufDelete", {
## todo
* [x] integration with dirstack.nvim (https://github.com/phanen/dirstack.nvim/commit/f5efd5e8c7768c22d2d52f6d1ae827a54ccaf416)
* [x] inject new pickers into fzflua builtin
* [ ] generic gh api can also be used in lazy previewer (fair enough... btw we finally need more structured-async)
101 changes: 42 additions & 59 deletions lua/flo/previewers/lazy.lua → lua/flo/previewers.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
local builtin_previewer = require('fzf-lua.previewer.builtin')
local fzf_previewer = require('fzf-lua.previewer.fzf')
local M = {}
local previewer = require('fzf-lua.previewer.builtin')
local floutil = require('flo.util')

local preview_with = function(_self, content)
local tmpbuf = _self:get_tmp_buffer()
vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, content)
if _self.filetype then vim.bo[tmpbuf].filetype = _self.filetype end
_self:set_preview_buf(tmpbuf)
_self.win:update_scrollbar()
end

local github_raw_url = function(url, filepath)
return url:gsub('github.com', 'raw.githubusercontent.com'):gsub('%.git$', '')
.. '/master/'
@@ -47,67 +55,21 @@ local parse_plugin_type = function(_, plugin)
return p_type.INS_NO_MD
end

local lazy_fzf = fzf_previewer.cmd_async:extend()

function lazy_fzf:new(o, op, fzf_win)
lazy_fzf.super.new(self, o, op, fzf_win)
self.bat_cmd = 'bat --color=always --style=numbers,changes'
self.ls_cmd = 'eza --color=always --tree --level=3 --icons=always {}'
return self
end
M.lazy = previewer.base:extend()

function lazy_fzf:cmdline(_)
return require('fzf-lua').shell.raw_preview_action_cmd(function(items, _)
local plugin = parse_entry(self, items[1])
local t, data = parse_plugin_type(self, plugin)

local handlers = {
-- TODO: parse local dir (absolute, or relative to vim.fn.stdpath('config'))
[p_type.LOCAL] = 'echo Local module!',

-- https://raw.githubusercontent.com/author/repo/master/README.md
-- main? master
-- FIXME: 1. if subprocess false, still return 0; 2. 404 is even not a false (drop output if 404?)
-- anyway, we just always run both commands here
[p_type.UNINS_GH] = function()
return ('echo "> Not Installed (fetch from github)!\n" && { curl -sL %s | %s --language md; }; { curl -sL %s | %s --language md; }'):format(
github_raw_url(plugin.url, 'README.md'),
self.bat_cmd,
github_raw_url(plugin.url, 'readme.md'),
self.bat_cmd
)
end,

[p_type.UNINS_NO_GH] = 'echo "> Not Installed (not github)"!',

-- TODO: buffer_or_file/quickfix
[p_type.INS_MD] = ('%s %s'):format(self.bat_cmd, data),

[p_type.INS_NO_MD] = self.ls_cmd:format(plugin.dir),
}
local cmdline = handlers[t]
if type(cmdline) == 'function' then cmdline = cmdline() end
if cmdline then return cmdline end
return 'echo Unknown plugin type!'
end, '{}', self.opts.debug)
end

local lazy_builtin = builtin_previewer.base:extend()
-- local lazy_builtin = builtin_previewer.buffer_or_file:extend()

function lazy_builtin:new(o, opts, fzf_win)
lazy_builtin.super.new(self, o, opts, fzf_win)
function M.lazy:new(o, opts, fzf_win)
M.lazy.super.new(self, o, opts, fzf_win)
-- self.filetype = 'man'
self.cmd = o.cmd or 'man -c %s | col -bx'
self.cmd = type(self.cmd) == 'function' and self.cmd() or self.cmd

-- lazy_builtin.super.new(self, o, op, fzf_win)
self.ls_cmd = 'ls -lh'
-- FIXME: why this is needed (why fzf previewer don't needed this...)
return setmetatable(self, self)
return self
end

function lazy_builtin:populate_preview_buf(entry_str)
function M.lazy:populate_preview_buf(entry_str)
if entry_str == '' then
self:clear_preview_buf(true)
return
@@ -157,13 +119,34 @@ function lazy_builtin:populate_preview_buf(entry_str)
---@diagnostic disable-next-line: param-type-mismatch
vim.schedule_wrap(function(obj)
local content = vim.split(obj.stdout, '\n')
floutil.preview_with(self, content)
preview_with(self, content)
end)
)
end

return {
fzf = lazy_fzf,
builtin = lazy_builtin,
-- builtin = lazy_fzf,
}
M.gitignore = previewer.buffer_or_file:extend()

function M.gitignore:new(o, opts, fzf_win)
M.gitignore.super.new(self, o, opts, fzf_win)
self.api_root = opts.api_root
self.filetype = opts.filetype
self.json_key = opts.json_key
return self
end

function M.gitignore:populate_preview_buf(entry_str)
if entry_str == '' then
self:clear_preview_buf(true)
return
end
floutil.gh_cache(
self.api_root .. '/' .. entry_str,
vim.schedule_wrap(function(_, json)
local content = assert(json[self.json_key])
content = vim.split(content, '\n')
preview_with(self, content)
end)
)
end

return M
36 changes: 6 additions & 30 deletions lua/flo/providers/gitignore.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
local floutil = require('flo.util')
local builtin_previewer = require('fzf-lua.previewer.builtin')

---@type FzfLuaOverlaySpec
local M = {}

local api_root = 'gitignore/templates'
local previewer = builtin_previewer.buffer_or_file:extend()

function previewer:new(o, opts, fzf_win)
previewer.super.new(self, o, opts, fzf_win)
self.api_root = api_root
self.filetype = 'gitignore'
self.json_key = 'source'
return self
end

function previewer:populate_preview_buf(entry_str)
if entry_str == '' then
self:clear_preview_buf(true)
return
end
floutil.gh_cache(
self.api_root .. '/' .. entry_str,
vim.schedule_wrap(function(_, json)
local content = assert(json[self.json_key])
content = vim.split(content, '\n')
floutil.preview_with(self, content)
end)
)
end

M.opts = {
previewer = { _ctor = function() return previewer end },
previewer = { _ctor = function() return require('flo.previewers').gitignore end },
api_root = 'gitignore/templates',
json_key = 'source',
filetype = 'gitignore',
actions = {
['default'] = function(selected)
local root = floutil.gitroot()
@@ -42,7 +18,7 @@ M.opts = {
if confirm ~= 1 then return end
end
local filetype = assert(selected[1])
floutil.gh_cache(api_root .. '/' .. filetype, function(_, json)
floutil.gh_cache(M.opts.api_root .. '/' .. filetype, function(_, json)
local content = assert(json.source)
floutil.write_file(path, content)
vim.cmd.edit(path)
@@ -53,7 +29,7 @@ M.opts = {

M.fn = function(opts)
local contents = function(fzf_cb)
floutil.gh_cache(api_root, function(_, json)
floutil.gh_cache(M.opts.api_root, function(_, json)
coroutine.wrap(function()
local co = coroutine.running()
vim.iter(json):each(function(item)
2 changes: 1 addition & 1 deletion lua/flo/providers/lazy.lua
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ local p_do = function(cb, limit)
end

M.opts = {
previewer = require('flo.previewers.lazy').builtin,
previewer = { _ctor = function() return require('flo.previewers').lazy end },
actions = {
['default'] = p_do(function(p)
local dir = p.dir
19 changes: 6 additions & 13 deletions lua/flo/providers/license.lua
Original file line number Diff line number Diff line change
@@ -4,18 +4,11 @@ local _, fn, uv = vim.api, vim.fn, vim.uv
---@type FzfLuaOverlaySpec
local M = {}

local api_root = 'licenses'
local previewer = require('flo.providers.gitignore').opts.previewer._ctor():extend()
function previewer:new(o, opts, fzf_win)
previewer.super:new(o, opts, fzf_win)
self.api_root = api_root
self.filetype = 'text'
self.json_key = 'body'
return self -- use setmetatable(self, self) can avoid ctor
end

M.opts = {
previewer = { _ctor = function() return previewer end },
previewer = { _ctor = function() return require('flo.previewers').gitignore:extend() end },
api_root = 'licenses',
json_key = 'body',
filetype = 'text',
actions = {
['default'] = function(selected)
local root = floutil.gitroot()
@@ -30,7 +23,7 @@ M.opts = {

if path and fn.confirm('Override?', '&Yes\n&No') ~= 1 then return end
local license = assert(selected[1])
floutil.gh_cache('licenses/' .. license, function(_, json)
floutil.gh_cache(M.opts.api_root .. 'licenses/' .. license, function(_, json)
local content = assert(json.body)
floutil.write_file(path, content)
vim.cmd.edit(path)
@@ -41,7 +34,7 @@ M.opts = {

M.fn = function(opts)
local contents = function(fzf_cb)
floutil.gh_cache('licenses', function(_, json)
floutil.gh_cache(M.opts.api_root, function(_, json)
coroutine.wrap(function()
local co = coroutine.running()
vim.iter(json):each(function(item)
8 changes: 0 additions & 8 deletions lua/flo/util.lua
Original file line number Diff line number Diff line change
@@ -167,14 +167,6 @@ M.snake_to_camel = function(name)
return table.concat(parts, '')
end

M.preview_with = function(_self, content)
local tmpbuf = _self:get_tmp_buffer()
vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, content)
if _self.filetype then vim.bo[tmpbuf].filetype = _self.filetype end
_self:set_preview_buf(tmpbuf)
_self.win:update_scrollbar()
end

M.ls = function(path, _fn)
for name, type in fs.dir(path) do
local fname = fs.joinpath(path, name)

0 comments on commit 632e91b

Please sign in to comment.