Skip to content

Commit

Permalink
feat: separate instances (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarriga committed Jun 4, 2022
1 parent 4ef4c13 commit 2cfd425
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 216 deletions.
12 changes: 12 additions & 0 deletions doc/nvim-notify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ notify.dismiss({opts}) *notify.dismiss()*
were dismissed.


notify.instance({user_config}, {inherit}) *notify.instance()*
Configure an instance of nvim-notify. You can use this to manage a separate
instance of nvim-notify with completely different configuration. The
returned instance will have the same functions as the notify module.


Parameters: ~
{user_config} (notify.Config)
{inherit} (boolean) Inherit the global configuration,
default true



================================================================================
*notify.config*
Expand Down
136 changes: 69 additions & 67 deletions lua/notify/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ local default_config = {
---@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

local user_config = default_config

local opacity_warned = false

local function validate_highlight(colour_or_group, needs_opacity)
Expand Down Expand Up @@ -88,30 +86,6 @@ local function validate_highlight(colour_or_group, needs_opacity)
end
end

function Config.setup(config)
local filled = vim.tbl_deep_extend("keep", config or {}, default_config)
user_config = filled
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() then
filled.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)
end

function Config._format_default()
local lines = { "<pre>", "Default values:" }
for line in vim.gsplit(vim.inspect(default_config), "\n", true) do
Expand All @@ -121,60 +95,88 @@ function Config._format_default()
return lines
end

---@param colour_or_group string
function Config.setup(custom_config)
local user_config = vim.tbl_deep_extend("keep", custom_config or {}, default_config)
local config = {}

function Config.level()
local level = user_config.level
if type(level) == "number" then
level = vim.lsp.log_levels[level] or vim.lsp.log_levels.INFO
function config.merged()
return user_config
end
return vim.lsp.log_levels[vim.fn.toupper(level)] or vim.lsp.log_levels.INFO
end

function Config.fps()
return user_config.fps
end
function config.level()
local level = user_config.level
if type(level) == "number" then
level = vim.lsp.log_levels[level] or vim.lsp.log_levels.INFO
end
return vim.lsp.log_levels[vim.fn.toupper(level)] or vim.lsp.log_levels.INFO
end

function Config.background_colour()
return tonumber(user_config.background_colour():gsub("#", "0x"), 16)
end
function config.fps()
return user_config.fps
end

function Config.icons()
return user_config.icons
end
function config.background_colour()
return tonumber(user_config.background_colour():gsub("#", "0x"), 16)
end

function Config.stages()
return user_config.stages
end
function config.icons()
return user_config.icons
end

function Config.default_timeout()
return user_config.timeout
end
function config.stages()
return user_config.stages
end

function Config.on_open()
return user_config.on_open
end
function config.default_timeout()
return user_config.timeout
end

function Config.on_close()
return user_config.on_close
end
function config.on_open()
return user_config.on_open
end

function Config.render()
return user_config.render
end
function config.on_close()
return user_config.on_close
end

function Config.minimum_width()
return user_config.minimum_width
end
function config.render()
return user_config.render
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.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() 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)

function Config.max_height()
return util.is_callable(user_config.max_height) and user_config.max_height()
or user_config.max_height
return config
end

return Config

0 comments on commit 2cfd425

Please sign in to comment.