summaryrefslogtreecommitdiffstats
path: root/lua/inbox/view.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/inbox/view.lua')
-rw-r--r--lua/inbox/view.lua73
1 files changed, 59 insertions, 14 deletions
diff --git a/lua/inbox/view.lua b/lua/inbox/view.lua
index a79bf17..5c40d5f 100644
--- a/lua/inbox/view.lua
+++ b/lua/inbox/view.lua
@@ -126,11 +126,13 @@ function M.initialize_entry(maildir, id, content_type)
vim.keymap.set("n", "<C-h>", require("inbox").toggle_headers, { buffer = bufnr })
vim.keymap.set("n", "<C-n>", function()
- local k, v = next(vim.b[bufnr].inbox_parts, vim.b[bufnr].inbox_part_index)
- if k == nil then
- k, v = next(vim.b[bufnr].inbox_parts)
+ local k
+ if vim.b[bufnr].inbox_content_type then
+ k = next(vim.b[bufnr].inbox_parts, vim.b[bufnr].inbox_content_type)
+ else
+ k = next(vim.b[bufnr].inbox_parts)
end
- M.render_entry(bufnr, id, v["content-type"])
+ M.render_entry(bufnr, id, k)
end, { buffer = bufnr })
vim.b[bufnr].inbox_id = id
@@ -182,20 +184,63 @@ function M.render_headers(bufnr, id)
end
function M.render_entry(bufnr, id, content_type)
+ local Job = require("plenary.job")
local indexer = indexers.get_indexer()
+ local entry = indexer.get_entry(id)
+
+ if entry == nil then
+ utils.error("Failed to get entry", { id = id })
+ return
+ end
+
+ if content_type == nil then
+ content_type = next(entry.parts)
+ end
+
+ local part = entry.parts[content_type]
+
+ if part == nil then
+ utils.error("Failed to find message part for entry", { id = id, ["content-type"] = content_type })
+ return
+ end
- indexer.get_part(id, content_type, function(entry, index, lines)
+ vim.b[bufnr].inbox_parts = entry.parts
+ vim.b[bufnr].inbox_content_type = content_type
+
+ if vim.tbl_isempty(config.filters[content_type] or {}) then
+ local lines = vim.split(part.content, "\n")
M.set_buffer_content(bufnr, lines, vim.b[bufnr].header_count)
- vim.b[bufnr].inbox_parts = entry.parts
- vim.b[bufnr].inbox_part_index = index
+ else
+ local job
+
+ for i, filter in pairs(config.filters[content_type]) do
+ if i == 1 then
+ job = Job:new({
+ command = filter.command,
+ args = filter.args,
+ writer = part.content,
+ })
+ elseif i < #config.filters[content_type] then
+ job = Job:new({
+ command = filter.command,
+ args = filter.args,
+ writer = job,
+ })
+ else
+ job = Job:new({
+ command = filter.command,
+ args = filter.args,
+ writer = job,
+ on_exit = vim.schedule_wrap(function(self)
+ vim.print(self)
+ M.set_buffer_content(bufnr, self:result(), vim.b[bufnr].header_count)
+ end),
+ })
+ end
+ end
- local winid = vim.api.nvim_get_current_win()
- vim.api.nvim_set_option_value(
- "winbar",
- vim.b[bufnr].inbox_parts[vim.b[bufnr].inbox_part_index]["content-type"],
- { scope = "local", win = winid }
- )
- end)
+ job:start()
+ end
end
return M