/
init.lua
202 lines (171 loc) · 5.68 KB
/
init.lua
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
---@tag notify.config
local Config = {}
local util = require("notify.util")
require("notify.config.highlights")
local BUILTIN_RENDERERS = {
DEFAULT = "default",
MINIMAL = "minimal",
}
local BUILTIN_STAGES = {
FADE = "fade",
SLIDE = "slide",
FADE_IN_SLIDE_OUT = "fade_in_slide_out",
STATIC = "static",
}
local default_config = {
level = vim.log.levels.INFO,
timeout = 5000,
max_width = nil,
max_height = nil,
stages = BUILTIN_STAGES.FADE_IN_SLIDE_OUT,
render = BUILTIN_RENDERERS.DEFAULT,
background_colour = "NotifyBackground",
on_open = nil,
on_close = nil,
minimum_width = 50,
fps = 30,
top_down = true,
time_formats = {
notification_history = "%FT%T",
notification = "%T",
},
icons = {
ERROR = "",
WARN = "",
INFO = "",
DEBUG = "",
TRACE = "✎",
},
}
---@class notify.Config
---@field level string|integer Minimum log level to display. See vim.log.levels.
---@field timeout number Default timeout for notification
---@field max_width number|function Max number of columns for messages
---@field max_height number|function Max number of lines for a message
---@field stages string|function[] Animation stages
---@field background_colour string For stages that change opacity this is treated as the highlight behind the window. Set this to either a highlight group, an RGB hex value e.g. "#000000" or a function returning an RGB code for dynamic values
---@field icons table Icons for each level (upper case names)
---@field time_formats table Time formats for different kind of notifications
---@field on_open function Function called when a new window is opened, use for changing win settings/config
---@field on_close function Function called when a window is closed
---@field render function|string Function to render a notification buffer or a built-in renderer name
---@field minimum_width integer Minimum width for notification windows
---@field fps integer Frames per second for animation stages, higher value means smoother animations but more CPU usage
---@field top_down boolean whether or not to position the notifications at the top or not
local opacity_warned = false
local function validate_highlight(colour_or_group, needs_opacity)
if type(colour_or_group) == "function" then
return colour_or_group
end
if colour_or_group:sub(1, 1) == "#" then
return function()
return colour_or_group
end
end
return function()
local group = vim.api.nvim_get_hl_by_name(colour_or_group, true)
if not group or not group.background then
if needs_opacity and not opacity_warned then
opacity_warned = true
vim.schedule(function()
vim.notify("Highlight group '" .. colour_or_group .. [[' has no background highlight
Please provide an RGB hex value or highlight group with a background value for 'background_colour' option.
This is the colour that will be used for 100% transparency.
```lua
require("notify").setup({
background_colour = "#000000",
})
```
Defaulting to #000000]], "warn", {
title = "nvim-notify",
on_open = function(win)
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
end,
})
end)
end
return "#000000"
end
return string.format("#%x", group.background)
end
end
function Config._format_default()
local lines = { "Default values:", ">lua" }
for line in vim.gsplit(vim.inspect(default_config), "\n", true) do
table.insert(lines, " " .. line)
end
table.insert(lines, "<")
return lines
end
function Config.setup(custom_config)
local user_config = vim.tbl_deep_extend("keep", custom_config or {}, default_config)
local config = {}
function config.merged()
return user_config
end
function config.level()
local level = user_config.level
if type(level) == "number" then
return level
end
return vim.log.levels[vim.fn.toupper(level)] or vim.log.levels.INFO
end
function config.fps()
return user_config.fps
end
function config.background_colour()
return tonumber(user_config.background_colour():gsub("#", "0x"), 16)
end
function config.time_formats()
return user_config.time_formats
end
function config.icons()
return user_config.icons
end
function config.stages()
return user_config.stages
end
function config.default_timeout()
return user_config.timeout
end
function config.on_open()
return user_config.on_open
end
function config.top_down()
return user_config.top_down
end
function config.on_close()
return user_config.on_close
end
function config.render()
return user_config.render
end
function config.minimum_width()
return user_config.minimum_width
end
function config.max_width()
return util.is_callable(user_config.max_width) and user_config.max_width()
or user_config.max_width
end
function config.max_height()
return util.is_callable(user_config.max_height) and user_config.max_height()
or user_config.max_height
end
local stages = config.stages()
local needs_opacity =
vim.tbl_contains({ BUILTIN_STAGES.FADE_IN_SLIDE_OUT, BUILTIN_STAGES.FADE }, stages)
if needs_opacity and not vim.opt.termguicolors:get() and vim.fn.has("nvim-0.10") == 0 then
user_config.stages = BUILTIN_STAGES.STATIC
vim.schedule(function()
vim.notify(
"Opacity changes require termguicolors to be set.\nChange to different animation stages or set termguicolors to disable this warning",
"warn",
{ title = "nvim-notify" }
)
end)
end
user_config.background_colour = validate_highlight(user_config.background_colour, needs_opacity)
return config
end
return Config