Persistent colorscheme #1239
Replies: 4 comments 21 replies
-
SimpleTry using these first: AdvancedBut personally, I use a more advanced approach with smart preloading of only the target colorscheme plugin and lazy-loading all others: -- lua/custom/save-colors.lua
local M = {}
M.registery = {}
function M.sync_colorscheme()
pcall(vim.cmd.rshada)
end
---@param fallback? string
---@return string|nil
function M.get_colorscheme(fallback)
if not vim.g.COLORS_NAME then
M.sync_colorscheme()
end
if vim.g.COLORS_NAME and vim.g.COLORS_NAME ~= '' then
return vim.g.COLORS_NAME
else
return fallback
end
end
---@param colorscheme? string
function M.save_colorscheme(colorscheme)
colorscheme = colorscheme or vim.g.colors_name
if M.get_colorscheme() == colorscheme then
return
end
vim.g.COLORS_NAME = colorscheme
vim.cmd.wshada()
end
function M.load_colorscheme()
return pcall(vim.cmd.colorscheme, M.get_colorscheme())
end
---@module 'lazy.types'
---@class ColorschemePluginSpec : LazyPluginSpec
---@field pattern? string|string[]
---@alias ColorschemeSpec string|ColorschemePluginSpec
local tbl_wrap = function(v)
return type(v) == 'table' and v or { v }
end
---@param plugins ColorschemeSpec[]
---@return LazyPluginSpec[]
function M.tune_colorscheme_plugins(plugins)
plugins = vim.iter(plugins):map(tbl_wrap)
---@param plug LazyPluginSpec
local get_name = function(plug)
local get_name = require('lazy.core.plugin').Spec.get_name
local name = plug.name --[[@as string]]
or plug[1] and get_name(plug[1])
or plug.url and get_name(plug.url)
or plug.dir and get_name(plug.dir)
return string.gsub(name, '[-.]nvim$', '')
end
local in_registery = function(plug, colorscheme)
local name = plug[1] or plug.url or plug.dir
local reg = M.registery[colorscheme]
if colorscheme:match 'base16%-' and name == 'RRethy/base16-nvim' then
return true
end
if reg == name then
return true
end
end
local match_colorscheme = function(plug, colorscheme)
if in_registery(plug, colorscheme) then
return true
end
local pattern = plug.pattern
if not pattern then
local name = get_name(plug)
if name then
pattern = string.gsub(name, '-', '%%-')
end
end
return vim.iter(tbl_wrap(pattern)):any(function(pat)
return string.match(colorscheme, pat)
end)
end
local colorscheme = M.get_colorscheme()
-- local make_config = function(plug)
-- local config = plug.config
-- return function(lazy_plug, opts)
-- if type(config) == 'function' then
-- config(lazy_plug, opts)
-- end
-- if config == true then
-- require(get_name(plug)).setup(lazy_plug, opts)
-- end
-- M.load_colorscheme()
-- end
-- end
plugins = plugins:map(function(plug)
if match_colorscheme(plug, colorscheme) then
return vim.tbl_extend('force', plug, {
lazy = false,
priority = 1000,
-- config = make_config(plug),
})
else
return vim.tbl_extend('keep', plug, { lazy = true })
end
end)
return plugins:totable()
end
---@param plugins ColorschemeSpec[]
---@return LazyPluginSpec[]
function M.lazy_setup(plugins)
local aug = vim.api.nvim_create_augroup('save_colors', { clear = true })
local on_enter = function()
if vim.g.colors_name == M.get_colorscheme() then
return true
else
M.load_colorscheme()
end
end
vim.api.nvim_create_autocmd('User', {
pattern = 'LazyDone',
once = true,
group = aug,
callback = on_enter,
})
vim.api.nvim_create_autocmd('VimEnter', {
once = true,
group = aug,
callback = on_enter,
})
vim.api.nvim_create_autocmd('ColorScheme', {
group = aug,
callback = function(event)
M.save_colorscheme(event.match)
end,
})
vim.api.nvim_create_autocmd('VimLeavePre', {
group = aug,
callback = function()
M.sync_colorscheme()
M.save_colorscheme(M.get_colorscheme())
end,
})
return M.tune_colorscheme_plugins(plugins)
end
return M -- init.lua
require('lazy').setup {
spec = {
{ import = 'plugins' },
},
install = {
colorscheme = { require('custom.save-colors').get_colorscheme 'default' },
},
} Warning Take a look at the Note The plugin will try to guess the colorscheme name from the link. -- lua/plugins/colorscheme.lua
return {
require('custom.save-colors').lazy_setup {
-- List all colorscheme plugins here.
{ 'folke/tokyonight.nvim' },
{ 'RRethy/base16-nvim', pattern = 'base16%-' }, -- <- `pattern`
{ 'rebelot/kanagawa.nvim', opts = {
commentStyle = { italic = false },
keywordStyle = { italic = false },
} },
{ 'rose-pine/neovim', name = 'rose-pine', opts = { -- <- `name`
styles = { italic = false },
} },
{ 'EdenEast/nightfox.nvim' },
},
-- Other plugins
} |
Beta Was this translation helpful? Give feedback.
-
Hi, Thank you for taking the time answering my question! I tried your code without but the colorscheme is not restore if I restart neovim after changing it using Where is it supposed to save and restore the colorscheme ? |
Beta Was this translation helpful? Give feedback.
-
Hi all, Is there a reason no solution is based upon setting a custom function for the option |
Beta Was this translation helpful? Give feedback.
-
I feel like the discussion is slowly falling apart because I introduced the harder solution first. If you don’t mind preloading colorscheme plugins, you can use this approach: -- lua/plugins/snacks.lua
return {
{ 'folke/tokyonight.nvim' },
{ 'RRethy/base16-nvim' },
{
'folke/snacks.nvim',
priority = 1000,
lazy = false,
init = function()
vim.api.nvim_create_autocmd('User', {
pattern = 'LazyDone',
callback = function()
vim.cmd.colorscheme(get_colorscheme 'default')
end,
})
end,
opts = {
picker = {
sources = {
colorschemes = {
confirm = function(picker, item)
local source = require('snacks.picker.config.sources').colorschemes
source.confirm(picker, item)
save_colorscheme(item.text)
end,
},
},
},
},
},
} -- init.lua
---@param fallback? string
_G.get_colorscheme = function(fallback)
if not vim.g.COLORS_NAME then
vim.cmd.rshada()
end
if not vim.g.COLORS_NAME or vim.g.COLORS_NAME == '' then
return fallback or 'default'
end
return vim.g.COLORS_NAME
end
---@param colorscheme? string
_G.save_colorscheme = function(colorscheme)
colorscheme = colorscheme or vim.g.colors_name
if get_colorscheme() == colorscheme then
return
end
vim.g.COLORS_NAME = colorscheme
vim.cmd.wshada()
end
require('lazy').setup {
spec = {
{ import = 'plugins' },
},
install = {
-- Set the colorscheme for the `:Lazy` UI
colorscheme = { get_colorscheme 'default' },
},
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
First of all, thank you for this amazing plugin!
I used to use this telescope extension for my theme picker: https://github.com/andrewberty/telescope-themes/
It had the same colorscheme picker feature but it stores the selected colorscheme in a persistente way. Is there a way to achieve the same using snacks colorscheme picker ?
Beta Was this translation helpful? Give feedback.
All reactions