mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-20 07:01:12 +00:00
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
This commit is contained in:
parent
402f9ef4aa
commit
e15acc34c1
|
@ -3,11 +3,34 @@ local action_state = require('telescope.actions.state')
|
||||||
local finders = require('telescope.finders')
|
local finders = require('telescope.finders')
|
||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
local sorters = require('telescope.sorters')
|
local sorters = require('telescope.sorters')
|
||||||
|
local previewers = require('telescope.previewers')
|
||||||
|
|
||||||
function mbox_picker(mboxes)
|
function mbox_picker(mboxes)
|
||||||
pickers.new {
|
pickers.new {
|
||||||
results_title = 'Mailboxes',
|
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(),
|
sorter = sorters.fuzzy_with_index_bias(),
|
||||||
attach_mappings = function(prompt_bufnr)
|
attach_mappings = function(prompt_bufnr)
|
||||||
actions.select_default:replace(function()
|
actions.select_default:replace(function()
|
||||||
|
@ -18,5 +41,6 @@ function mbox_picker(mboxes)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
previewer = previewers.display_content.new({})
|
||||||
}:find()
|
}:find()
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,7 +27,7 @@ endfunction
|
||||||
|
|
||||||
function! himalaya#mbox#input()
|
function! himalaya#mbox#input()
|
||||||
try
|
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"
|
if &rtp =~ "telescope"
|
||||||
execute printf("luafile %s/mbox.lua", s:dir)
|
execute printf("luafile %s/mbox.lua", s:dir)
|
||||||
|
|
|
@ -18,14 +18,11 @@ function! s:format_msg_for_list(msg)
|
||||||
return msg
|
return msg
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! himalaya#msg#list()
|
function! himalaya#msg#list_with(mbox, page, should_throw)
|
||||||
try
|
let msgs = s:cli("--mailbox %s list --page %d", [shellescape(a:mbox), a:page], printf("Fetching %s messages", a:mbox), a:should_throw)
|
||||||
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 msgs = map(msgs, "s:format_msg_for_list(v:val)")
|
||||||
let buftype = stridx(bufname("%"), "Himalaya messages") == 0 ? "file" : "edit"
|
let buftype = stridx(bufname("%"), "Himalaya messages") == 0 ? "file" : "edit"
|
||||||
execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, mbox, page + 1)
|
execute printf("silent! %s Himalaya messages [%s] [page %d]", buftype, a:mbox, a:page + 1)
|
||||||
setlocal modifiable
|
setlocal modifiable
|
||||||
silent execute "%d"
|
silent execute "%d"
|
||||||
call append(0, s:render("list", msgs))
|
call append(0, s:render("list", msgs))
|
||||||
|
@ -33,6 +30,13 @@ function! himalaya#msg#list()
|
||||||
setlocal filetype=himalaya-msg-list
|
setlocal filetype=himalaya-msg-list
|
||||||
let &modified = 0
|
let &modified = 0
|
||||||
execute 0
|
execute 0
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! himalaya#msg#list()
|
||||||
|
try
|
||||||
|
let mbox = himalaya#mbox#curr_mbox()
|
||||||
|
let page = himalaya#mbox#curr_page()
|
||||||
|
call himalaya#msg#list_with(mbox, page, 0)
|
||||||
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)
|
||||||
|
@ -44,7 +48,7 @@ function! himalaya#msg#read()
|
||||||
try
|
try
|
||||||
let s:msg_id = s:get_focused_msg_id()
|
let s:msg_id = s:get_focused_msg_id()
|
||||||
let mbox = himalaya#mbox#curr_mbox()
|
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 ? " []" : ""
|
let attachment = msg.hasAttachment ? " []" : ""
|
||||||
execute printf("silent! edit Himalaya read message [%d]%s", s:msg_id, attachment)
|
execute printf("silent! edit Himalaya read message [%d]%s", s:msg_id, attachment)
|
||||||
setlocal modifiable
|
setlocal modifiable
|
||||||
|
@ -63,7 +67,7 @@ endfunction
|
||||||
|
|
||||||
function! himalaya#msg#write()
|
function! himalaya#msg#write()
|
||||||
try
|
try
|
||||||
let msg = s:cli("template new", [], "Fetching new template")
|
let msg = s:cli("template new", [], "Fetching new template", 0)
|
||||||
silent! edit Himalaya write
|
silent! edit Himalaya write
|
||||||
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
||||||
silent execute "$d"
|
silent execute "$d"
|
||||||
|
@ -81,7 +85,7 @@ function! himalaya#msg#reply()
|
||||||
try
|
try
|
||||||
let mbox = himalaya#mbox#curr_mbox()
|
let mbox = himalaya#mbox#curr_mbox()
|
||||||
let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id
|
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)
|
execute printf("silent! edit Himalaya reply [%d]", msg_id)
|
||||||
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
||||||
silent execute "$d"
|
silent execute "$d"
|
||||||
|
@ -99,7 +103,7 @@ function! himalaya#msg#reply_all()
|
||||||
try
|
try
|
||||||
let mbox = himalaya#mbox#curr_mbox()
|
let mbox = himalaya#mbox#curr_mbox()
|
||||||
let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id
|
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)
|
execute printf("silent! edit Himalaya reply all [%d]", msg_id)
|
||||||
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
||||||
silent execute "$d"
|
silent execute "$d"
|
||||||
|
@ -117,7 +121,7 @@ function! himalaya#msg#forward()
|
||||||
try
|
try
|
||||||
let mbox = himalaya#mbox#curr_mbox()
|
let mbox = himalaya#mbox#curr_mbox()
|
||||||
let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id
|
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)
|
execute printf("silent! edit Himalaya forward [%d]", msg_id)
|
||||||
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
call append(0, split(substitute(msg.template, "\r", "", "g"), "\n"))
|
||||||
silent execute "$d"
|
silent execute "$d"
|
||||||
|
@ -145,9 +149,9 @@ function! himalaya#msg#draft_handle()
|
||||||
redraw | echo
|
redraw | echo
|
||||||
|
|
||||||
if choice == "s"
|
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"
|
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"
|
elseif choice == "q"
|
||||||
return
|
return
|
||||||
elseif choice == "c"
|
elseif choice == "c"
|
||||||
|
@ -165,7 +169,7 @@ function! himalaya#msg#attachments()
|
||||||
try
|
try
|
||||||
let mbox = himalaya#mbox#curr_mbox()
|
let mbox = himalaya#mbox#curr_mbox()
|
||||||
let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id
|
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
|
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)
|
||||||
|
|
|
@ -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))
|
call himalaya#shared#log#info(printf("%s…", a:log))
|
||||||
let cmd = call("printf", ["himalaya --output json " . a:cmd] + a:args)
|
let cmd = call("printf", ["himalaya --output json " . a:cmd] + a:args)
|
||||||
let res = system(cmd)
|
let res = system(cmd)
|
||||||
|
@ -15,7 +15,9 @@ function! himalaya#shared#cli#call(cmd, args, log)
|
||||||
for line in split(res, "\n")
|
for line in split(res, "\n")
|
||||||
call himalaya#shared#log#err(line)
|
call himalaya#shared#log#err(line)
|
||||||
endfor
|
endfor
|
||||||
throw ""
|
if a:should_throw
|
||||||
|
throw res
|
||||||
|
endif
|
||||||
endtry
|
endtry
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
Loading…
Reference in a new issue