add flag to enable telescope preview and to choose picker (#97)

* Flags to enable telescope preview and to choose picker

* 4 spaces -> 2 space lua

* changes to align with repo code-style
This commit is contained in:
Axel Dahlberg 2021-04-13 17:10:42 +02:00 committed by GitHub
parent 647c0c25ce
commit 8a617c314e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 61 deletions

View file

@ -1,46 +0,0 @@
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local finders = require('telescope.finders')
local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters')
local previewers = require('telescope.previewers')
function mbox_picker(mboxes)
pickers.new {
results_title = 'Mailboxes',
finder = finders.new_table {
results = mboxes,
entry_maker = function(entry)
return {
value = entry,
display = entry,
ordinal = entry,
preview_command = function(entry, bufnr)
vim.api.nvim_buf_call(bufnr, function()
local page = 0 -- page 0 for preview
local success, output = pcall(vim.fn['himalaya#msg#list_with'], entry.value, page, true)
if not (success) then
vim.cmd('redraw')
vim.bo.modifiable = true
local errors = vim.fn.split(output, '\n')
errors[1] = "Errors: "..errors[1]
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, errors)
end
end)
end
}
end,
},
sorter = sorters.fuzzy_with_index_bias(),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.fn['himalaya#mbox#post_input'](selection.display)
end)
return true
end,
previewer = previewers.display_content.new({})
}:find()
end

View file

@ -25,27 +25,44 @@ function! himalaya#mbox#curr_mbox()
return s:curr_mbox return s:curr_mbox
endfunction endfunction
function! s:telescope_picker(mboxes)
call luaeval('require("himalaya.mbox").mbox_picker')(a:mboxes)
endfunction
function! s:fzf_picker(mboxes)
call fzf#run({
\"source": a:mboxes,
\"sink": function("himalaya#mbox#post_input"),
\"down": "25%",
\})
endfunction
function! s:native_picker(mboxes)
let choice = map(copy(a:mboxes), "printf('%s (%d)', v:val, v:key)")
let choice = input(join(choice, ", ") . ": ")
redraw | echo
call himalaya#mbox#post_input(a:mboxes[choice])
endfunction
let s:pickers = {"telescope": function("s:telescope_picker"), "fzf": function("s:fzf_picker"), "native": function("s:native_picker")}
function! himalaya#mbox#input() function! himalaya#mbox#input()
try try
let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name") let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name")
if &rtp =~ "telescope" if exists("g:himalaya_mailbox_picker") " Get user choice for picker, otherwise check runtimepath
execute printf("luafile %s/mbox.lua", s:dir) let mbox_picker = g:himalaya_mailbox_picker
call luaeval(printf("mbox_picker({%s})", join(map(mboxes, "string(v:val)"), ", ")))
elseif &rtp =~ "fzf"
call fzf#run({
\"source": mboxes,
\"sink": function("himalaya#mbox#post_input"),
\"down": "25%",
\})
else else
let choice = map(copy(mboxes), "printf('%s (%d)', v:val, v:key)") if &rtp =~ "telescope"
let choice = input(join(choice, ", ") . ": ") let mbox_picker = "telescope"
redraw | echo elseif &rtp =~ "fzf"
call himalaya#mbox#post_input(mboxes[choice]) let mbox_picker = "fzf"
else
let mbox_picker = "native"
endif
endif endif
call s:pickers[mbox_picker](mboxes)
catch catch
if !empty(v:exception) if !empty(v:exception)
redraw | call himalaya#shared#log#err(v:exception) redraw | call himalaya#shared#log#err(v:exception)

57
vim/lua/himalaya/mbox.lua Normal file
View file

@ -0,0 +1,57 @@
local M = {}
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local finders = require('telescope.finders')
local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters')
local previewers = require('telescope.previewers')
local function preview_command(entry, bufnr)
vim.api.nvim_buf_call(bufnr, function()
local page = 0 -- page 0 for preview
local success, output = pcall(vim.fn['himalaya#msg#list_with'], entry.value, page, true)
if not (success) then
vim.cmd('redraw')
vim.bo.modifiable = true
local errors = vim.fn.split(output, '\n')
errors[1] = "Errors: "..errors[1]
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, errors)
end
end)
end
local function entry_maker(entry)
return {
value = entry,
display = entry,
ordinal = entry,
preview_command = preview_command,
}
end
M.mbox_picker = function(mboxes)
local finder_opts = {results = mboxes}
local previewer = nil
if vim.g.himalaya_telescope_preview_enabled then
finder_opts.entry_maker = entry_maker
previewer = previewers.display_content.new({})
end
pickers.new {
results_title = 'Mailboxes',
finder = finders.new_table(finder_opts),
sorter = sorters.fuzzy_with_index_bias(),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
vim.fn['himalaya#mbox#post_input'](selection.display)
end)
return true
end,
previewer = previewer,
}:find()
end
return M