Refactored theme funcs

This commit is contained in:
2024-10-14 19:54:21 +02:00
parent d893e759da
commit 89214830e4
2 changed files with 64 additions and 50 deletions

View File

@@ -0,0 +1,60 @@
local oldstring = 'M.colorscheme = .*"'
local getAllThemes = function()
vim.api.nvim_exec_autocmds("User", { pattern = "ThemeSwitcher", modeline = false })
local colors = vim.fn.globpath(vim.o.rtp, "colors/*", 1, 1)
local dataDir = vim.fn.stdpath("data")
local newpath = string.gsub(dataDir, "%-", "%%-")
local themes = {}
for i, v in ipairs(colors) do
if string.find(v, "lazy") then
local filename = ""
if vim.loop.os_uname().sysname == "Windows_NT" then
filename = string.gsub(v, newpath .. "\\lazy\\.*\\", "")
else
filename = string.gsub(v, newpath .. "/lazy/.*/", "")
end
local themename = string.gsub(filename, ".vim", "")
local themename2 = string.gsub(themename, ".lua", "")
table.insert(themes, themename2)
end
end
return themes
end
local change_theme = function(old, new, prefsFile)
-- function from nvchad
local file = io.open(prefsFile, "r")
local added_pattern = string.gsub(old, "-", "%%-")
local new_content = file:read("*all"):gsub(added_pattern, new)
file = io.open(prefsFile, "w")
file:write(new_content)
file:close()
end
local exec = function(choice, prefsFile)
if choice == nil then
vim.notify("No theme selected!", 4)
return
end
vim.cmd("colorscheme " .. choice)
local newstring = 'M.colorscheme = "' .. choice .. '"'
change_theme(oldstring, newstring, prefsFile)
local cursorcolumncolor = vim.fn.synIDattr(vim.fn.hlID("CursorColumn"), "bg")
if cursorcolumncolor ~= "" then
vim.cmd("hi CursorLine guibg=" .. cursorcolumncolor)
end
end
return {
getAllThemes = getAllThemes,
change_theme = change_theme,
exec = exec,
}

View File

@@ -1,6 +1,7 @@
local M = {} local M = {}
function M.themeselector(opts) function M.themeselector(opts)
local themefuncs = require("taken.utils.themefuncs")
local pickers = require("telescope.pickers") local pickers = require("telescope.pickers")
local finders = require("telescope.finders") local finders = require("telescope.finders")
local conf = require("telescope.config").values local conf = require("telescope.config").values
@@ -10,56 +11,9 @@ function M.themeselector(opts)
local configDir = vim.fn.stdpath("config") local configDir = vim.fn.stdpath("config")
local prefsFile = configDir .. "/lua/taken/prefs.lua" local prefsFile = configDir .. "/lua/taken/prefs.lua"
local oldstring = 'M.colorscheme = .*"'
vim.api.nvim_exec_autocmds("User", { pattern = "ThemeSwitcher", modeline = false }) local themes = themefuncs.getAllThemes()
local exec = themefuncs.exec
local colors = vim.fn.globpath(vim.o.rtp, "colors/*", 1, 1)
local dataDir = vim.fn.stdpath("data")
local newpath = string.gsub(dataDir, "%-", "%%-")
local themes = {}
for i, v in ipairs(colors) do
if string.find(v, "lazy") then
local filename = ""
if vim.loop.os_uname().sysname == "Windows_NT" then
filename = string.gsub(v, newpath .. "\\lazy\\.*\\", "")
else
filename = string.gsub(v, newpath .. "/lazy/.*/", "")
end
local themename = string.gsub(filename, ".vim", "")
local themename2 = string.gsub(themename, ".lua", "")
table.insert(themes, themename2)
end
end
local function change_theme(old, new)
-- function from nvchad
local file = io.open(prefsFile, "r")
local added_pattern = string.gsub(old, "-", "%%-")
local new_content = file:read("*all"):gsub(added_pattern, new)
file = io.open(prefsFile, "w")
file:write(new_content)
file:close()
end
local exec = function(choice)
if choice == nil then
vim.notify("No theme selected!", 4)
return
end
vim.cmd("colorscheme " .. choice)
local newstring = 'M.colorscheme = "' .. choice .. '"'
change_theme(oldstring, newstring)
local cursorcolumncolor = vim.fn.synIDattr(vim.fn.hlID("CursorColumn"), "bg")
if cursorcolumncolor ~= "" then
vim.cmd("hi CursorLine guibg=" .. cursorcolumncolor)
end
end
opts = opts or {} opts = opts or {}
pickers pickers
@@ -73,7 +27,7 @@ function M.themeselector(opts)
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
exec(selection.value) exec(selection.value, prefsFile)
end) end)
action_set.shift_selection:enhance({ action_set.shift_selection:enhance({
post = function() post = function()