From e15acc34c1c3e7ec1e4d3d44a87b83cb65b3b615 Mon Sep 17 00:00:00 2001 From: Axel Dahlberg Date: Thu, 8 Apr 2021 17:41:13 +0200 Subject: [PATCH] preview mailboxes with telescope (#69) * Preview mailboxes with telescope * Output error message in preview * Throw error * Refactored himalaya#msg#list and handle errors in telescope preview * Changes based on feedback * Added argument should_throw to cli function --- vim/autoload/himalaya/mbox.lua | 26 ++++++++++++++++- vim/autoload/himalaya/mbox.vim | 2 +- vim/autoload/himalaya/msg.vim | 42 +++++++++++++++------------- vim/autoload/himalaya/shared/cli.vim | 6 ++-- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/vim/autoload/himalaya/mbox.lua b/vim/autoload/himalaya/mbox.lua index ab8e360..f32a56f 100644 --- a/vim/autoload/himalaya/mbox.lua +++ b/vim/autoload/himalaya/mbox.lua @@ -3,11 +3,34 @@ 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(mboxes), + 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() @@ -18,5 +41,6 @@ function mbox_picker(mboxes) return true end, + previewer = previewers.display_content.new({}) }:find() end diff --git a/vim/autoload/himalaya/mbox.vim b/vim/autoload/himalaya/mbox.vim index 8e02672..7809487 100644 --- a/vim/autoload/himalaya/mbox.vim +++ b/vim/autoload/himalaya/mbox.vim @@ -27,7 +27,7 @@ endfunction function! himalaya#mbox#input() try - let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes"), "v:val.name") + let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name") if &rtp =~ "telescope" execute printf("luafile %s/mbox.lua", s:dir) diff --git a/vim/autoload/himalaya/msg.vim b/vim/autoload/himalaya/msg.vim index b418eda..dd2688c 100644 --- a/vim/autoload/himalaya/msg.vim +++ b/vim/autoload/himalaya/msg.vim @@ -18,21 +18,25 @@ function! s:format_msg_for_list(msg) return msg endfunction +function! himalaya#msg#list_with(mbox, page, should_throw) + let msgs = s:cli("--mailbox %s list --page %d", [shellescape(a:mbox), a:page], printf("Fetching %s messages", a:mbox), a:should_throw) + let msgs = map(msgs, "s:format_msg_for_list(v:val)") + let buftype = stridx(bufname("%"), "Himalaya messages") == 0 ? "file" : "edit" + execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, a:mbox, a:page + 1) + setlocal modifiable + silent execute "%d" + call append(0, s:render("list", msgs)) + silent execute "$d" + setlocal filetype=himalaya-msg-list + let &modified = 0 + execute 0 +endfunction + function! himalaya#msg#list() try let mbox = himalaya#mbox#curr_mbox() let page = himalaya#mbox#curr_page() - let msgs = s:cli("--mailbox %s list --page %d", [shellescape(mbox), page], printf("Fetching %s messages", mbox)) - let msgs = map(msgs, "s:format_msg_for_list(v:val)") - let buftype = stridx(bufname("%"), "Himalaya messages") == 0 ? "file" : "edit" - execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, mbox, page + 1) - setlocal modifiable - silent execute "%d" - call append(0, s:render("list", msgs)) - silent execute "$d" - setlocal filetype=himalaya-msg-list - let &modified = 0 - execute 0 + call himalaya#msg#list_with(mbox, page, 0) catch if !empty(v:exception) redraw | call himalaya#shared#log#err(v:exception) @@ -44,7 +48,7 @@ function! himalaya#msg#read() try let s:msg_id = s:get_focused_msg_id() let mbox = himalaya#mbox#curr_mbox() - let msg = s:cli("--mailbox %s read %d", [shellescape(mbox), s:msg_id], printf("Fetching message %d", s:msg_id)) + let msg = s:cli("--mailbox %s read %d", [shellescape(mbox), s:msg_id], printf("Fetching message %d", s:msg_id), 0) let attachment = msg.hasAttachment ? " []" : "" execute printf("silent! edit Himalaya read message [%d]%s", s:msg_id, attachment) setlocal modifiable @@ -63,7 +67,7 @@ endfunction function! himalaya#msg#write() try - let msg = s:cli("template new", [], "Fetching new template") + let msg = s:cli("template new", [], "Fetching new template", 0) silent! edit Himalaya write call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) silent execute "$d" @@ -81,7 +85,7 @@ function! himalaya#msg#reply() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - let msg = s:cli("--mailbox %s template reply %d", [shellescape(mbox), msg_id], "Fetching reply template") + let msg = s:cli("--mailbox %s template reply %d", [shellescape(mbox), msg_id], "Fetching reply template", 0) execute printf("silent! edit Himalaya reply [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) silent execute "$d" @@ -99,7 +103,7 @@ function! himalaya#msg#reply_all() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - let msg = s:cli("--mailbox %s template reply %d --all", [shellescape(mbox), msg_id], "Fetching reply all template") + let msg = s:cli("--mailbox %s template reply %d --all", [shellescape(mbox), msg_id], "Fetching reply all template", 0) execute printf("silent! edit Himalaya reply all [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) silent execute "$d" @@ -117,7 +121,7 @@ function! himalaya#msg#forward() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - let msg = s:cli("--mailbox %s template forward %d", [shellescape(mbox), msg_id], "Fetching forward template") + let msg = s:cli("--mailbox %s template forward %d", [shellescape(mbox), msg_id], "Fetching forward template", 0) execute printf("silent! edit Himalaya forward [%d]", msg_id) call append(0, split(substitute(msg.template, "\r", "", "g"), "\n")) silent execute "$d" @@ -145,9 +149,9 @@ function! himalaya#msg#draft_handle() redraw | echo if choice == "s" - return s:cli("send -- %s", [shellescape(s:draft)], "Sending message") + return s:cli("send -- %s", [shellescape(s:draft)], "Sending message", 0) elseif choice == "d" - return s:cli("--mailbox Drafts save -- %s", [shellescape(s:draft)], "Saving draft") + return s:cli("--mailbox Drafts save -- %s", [shellescape(s:draft)], "Saving draft", 0) elseif choice == "q" return elseif choice == "c" @@ -165,7 +169,7 @@ function! himalaya#msg#attachments() try let mbox = himalaya#mbox#curr_mbox() let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id - let msg = s:cli("--mailbox %s attachments %d", [shellescape(mbox), msg_id], "Downloading attachments") + let msg = s:cli("--mailbox %s attachments %d", [shellescape(mbox), msg_id], "Downloading attachments", 0) catch if !empty(v:exception) redraw | call himalaya#shared#log#err(v:exception) diff --git a/vim/autoload/himalaya/shared/cli.vim b/vim/autoload/himalaya/shared/cli.vim index bdd2f40..c3b04a5 100644 --- a/vim/autoload/himalaya/shared/cli.vim +++ b/vim/autoload/himalaya/shared/cli.vim @@ -1,4 +1,4 @@ -function! himalaya#shared#cli#call(cmd, args, log) +function! himalaya#shared#cli#call(cmd, args, log, should_throw) call himalaya#shared#log#info(printf("%s…", a:log)) let cmd = call("printf", ["himalaya --output json " . a:cmd] + a:args) let res = system(cmd) @@ -15,7 +15,9 @@ function! himalaya#shared#cli#call(cmd, args, log) for line in split(res, "\n") call himalaya#shared#log#err(line) endfor - throw "" + if a:should_throw + throw res + endif endtry endif endfunction