Skip to content

Commit

Permalink
add custom theme support and plugin themes to Colorscheme command
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nehrig <[email protected]>
  • Loading branch information
danielnehrig committed Jul 2, 2024
1 parent 467b83b commit f5ded89
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 16 deletions.
30 changes: 29 additions & 1 deletion lua/config/plugins/modules/themes.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
---@module 'lazy.types'

---@class PluginColorschemes: LazyPluginSpec
---@field colorscheme_name string[] this is needed because there is no real way to know the colorscheme name

---@class themes
---@field theme table<string, LazyPluginSpec>
---@field ts_themes table<string, PluginColorschemes>
local M = {}

-- globals
Expand Down Expand Up @@ -91,22 +95,37 @@ M.theme = {

M.ts_themes = {
["sainnhe/sonokai"] = {
colorscheme_name = { "sonokai" },
lazy = true,
priority = 1000,
opts = {},
},
["ray-x/aurora"] = {
colorscheme_name = { "aurora" },
lazy = true,
priority = 1000,
opts = {},
},
["Mofiqul/vscode.nvim"] = {
colorscheme_name = { "vscode" },
lazy = true,
priority = 1000,
opts = {},
},
["marko-cerovac/material.nvim"] = {
colorscheme_name = { "material" },
lazy = true,
priority = 1000,
opts = {},
config = function()
require("material").setup()
require("material").setup({})
end,
},
["Murtaza-Udaipurwala/gruvqueen"] = {
colorscheme_name = { "gruvqueen" },
lazy = true,
priority = 1000,
opts = {},
config = function()
vim.o.background = "dark"
require("gruvqueen").setup({
Expand All @@ -125,7 +144,16 @@ M.ts_themes = {
end,
},
["folke/tokyonight.nvim"] = {
colorscheme_name = {
"tokyonight",
"tokyonight-night",
"tokyonight-storm",
"tokyonight-day",
"tokyonight-moon",
},
lazy = true,
priority = 1000,
opts = {},
},
}

Expand Down
3 changes: 1 addition & 2 deletions lua/config/themes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ M.get_theme_tb = function(type)

if present1 then
return default_theme[type]
else
error("No such theme! " .. default_path)
-- error("No such theme! " .. default_path)
end
end

Expand Down
84 changes: 71 additions & 13 deletions lua/config/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,67 @@ end
--- @param arg string
M.switch_theme = function(arg)
local colorscheme = nil
local colorscheme_type = nil

-- local project themes
local hl_dir = vim.fn.stdpath("config") .. "/lua/config/themes/hl"
local hl_files = require("plenary.scandir").scan_dir(hl_dir, {})
local hl_dir_main = vim.fn.stdpath("config") .. "/lua/config/themes/hl"
local hl_dir_custom = vim.fn.stdpath("config")
.. "/lua/config/custom/themes/hl"
local hl_files_main = require("plenary.scandir").scan_dir(hl_dir_main, {})
local hl_files_custom = require("plenary.scandir").scan_dir(hl_dir_custom, {})

if vim.fn.isdirectory(hl_dir_custom) == 0 then
vim.fn.mkdir(hl_dir_custom, "p")
end

for _, file in ipairs(hl_files) do
for _, file in ipairs(hl_files_main) do
local a = vim.fn.fnamemodify(file, ":t")
a = vim.fn.fnamemodify(a, ":r")
if arg == a then
colorscheme = a
colorscheme_type = "integrated"
end
end

if not colorscheme then
for _, file in ipairs(hl_files_custom) do
local a = vim.fn.fnamemodify(file, ":t")
a = vim.fn.fnamemodify(a, ":r")
if arg == a then
colorscheme = a
colorscheme_type = "custom"
end
end
end

if not colorscheme then
for _, data in pairs(themes) do
for _, name in pairs(data.colorscheme_name) do
if arg == name then
colorscheme = name
colorscheme_type = "plugin"
end
end
end
end

if colorscheme then
require("config.core.config").config.ui.colorscheme.name = colorscheme
require("config.themes").load_theme()

require("plenary.reload").reload_module(
"config.plugins.configs.statusline.theme." .. config.ui.statusline.name
)
require("config.plugins.configs.statusline.windline").switch_theme(
config.ui.statusline.name
)

if colorscheme_type == "integrated" or colorscheme_type == "custom" then
require("config.themes").load_theme()
require("plenary.reload").reload_module(
"config.plugins.configs.statusline.theme." .. config.ui.statusline.name
)
require("config.plugins.configs.statusline.windline").switch_theme(
config.ui.statusline.name
)
end

if colorscheme_type == "plugin" then
vim.cmd("colorscheme " .. colorscheme)
end

return
end

Expand All @@ -89,6 +127,16 @@ M.get_themes = function()
---@type string[]
local hl_files = require("plenary.scandir").scan_dir(hl_dir, {})

local hl_dir_custom = vim.fn.stdpath("config")
.. "/lua/config/custom/themes/hl"

if vim.fn.isdirectory(hl_dir_custom) == 0 then
vim.fn.mkdir(hl_dir_custom, "p")
end

---@type string[]
local hl_files_custom = require("plenary.scandir").scan_dir(hl_dir_custom, {})

for _, file in ipairs(hl_files) do
local a = vim.fn.fnamemodify(file, ":t")
a = vim.fn.fnamemodify(a, ":r")
Expand All @@ -97,8 +145,18 @@ M.get_themes = function()
end
end

for theme_name, _ in pairs(themes) do
table.insert(res, theme_name)
for _, file in ipairs(hl_files_custom) do
local a = vim.fn.fnamemodify(file, ":t")
a = vim.fn.fnamemodify(a, ":r")
if a ~= "types" then
table.insert(res, a)
end
end

for _, data in pairs(themes) do
for _, name in pairs(data.colorscheme_name) do
table.insert(res, name)
end
end

return res
Expand Down

0 comments on commit f5ded89

Please sign in to comment.