summaryrefslogtreecommitdiffstats
path: root/lua/inbox/config.lua
blob: 2f7f47da71edf315a6c23099acd206ba386fd341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local utils = require("inbox.utils")

---@type inbox.Config
local default_config = {
	indexer_config = "notmuch",
	buf_options = {},
	win_options = {
		wrap = false,
		signcolumn = "yes:1",
		cursorcolumn = false,
		colorcolumn = false,
		foldcolumn = "0",
		spell = false,
		list = false,
	},
	columns = {
		15,
		30,
	},
	headers = {
		"From",
		"Date",
		"Subject",
	},
	flags = {
		unread = {
			text = "O",
			texthl = "Special",
			linehl = "Special",
		},
		flagged = {
			text = "F",
			texthl = "Search",
		},
		attachment = {
			text = "A",
			texthl = "Constant",
		},
		replied = {
			text = "R",
			texthl = "Directory",
		},
		signed = {
			text = "S",
			texthl = "Directory",
		},
		draft = {
			text = "D",
		},
		passed = {
			text = "P",
		},
	},
}

---@class inbox.Config
local M = {}

function M.try_resolve(value, module)
	if type(value) == "string" then
		local is_ok, result = pcall(require, ("inbox.%s.%s"):format(module, value))
		if is_ok then
			value = result
		end
	end
	return value
end

function M.parse_filters(filter_config)
	local filters = {}
	for content_type, items in pairs(filter_config) do
		filters[content_type] = {}
		for _, value in pairs(items) do
			table.insert(filters[content_type], M.try_resolve(value, "filters"))
		end
	end
	return filters
end

---@param opts inbox.Config
function M.setup(opts)
	local config = vim.tbl_deep_extend("keep", opts or {}, default_config)

	for k, v in pairs(config) do
		if k == "filters" then
			M[k] = M.parse_filters(v)
		elseif k == "indexer" then
			M[k] = M.try_resolve(v, k)
		else
			M[k] = v
		end
	end
end

return M