himalaya/vim/autoload/himalaya/mbox.vim
Axel Dahlberg e15acc34c1
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
2021-04-08 17:41:13 +02:00

61 lines
1.4 KiB
VimL

let s:dir = expand("<sfile>:h")
let s:cli = function("himalaya#shared#cli#call")
" Pagination
let s:curr_page = 0
function! himalaya#mbox#curr_page()
return s:curr_page
endfunction
function! himalaya#mbox#prev_page()
let s:curr_page = max([0, s:curr_page - 1])
call himalaya#msg#list()
endfunction
function! himalaya#mbox#next_page()
let s:curr_page = s:curr_page + 1
call himalaya#msg#list()
endfunction
" Mailbox
let s:curr_mbox = "INBOX"
function! himalaya#mbox#curr_mbox()
return s:curr_mbox
endfunction
function! himalaya#mbox#input()
try
let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name")
if &rtp =~ "telescope"
execute printf("luafile %s/mbox.lua", s:dir)
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
let choice = map(copy(mboxes), "printf('%s (%d)', v:val, v:key)")
let choice = input(join(choice, ", ") . ": ")
redraw | echo
call himalaya#mbox#post_input(mboxes[choice])
endif
catch
if !empty(v:exception)
redraw | call himalaya#shared#log#err(v:exception)
endif
endtry
endfunction
function! himalaya#mbox#post_input(mbox)
let s:curr_mbox = a:mbox
let s:curr_page = 0
call himalaya#msg#list()
endfunction