mirror of
https://github.com/jdhao/nvim-config.git
synced 2025-06-08 14:14:33 +02:00
refactor: theme.vim --> theme.lua
This commit is contained in:
parent
6539e72a5d
commit
59525c5577
@ -27,21 +27,6 @@ function! utils#HasColorscheme(name) abort
|
||||
return !empty(globpath(&runtimepath, l:pat))
|
||||
endfunction
|
||||
|
||||
" Generate random integers in the range [Low, High] in pure vim script,
|
||||
" adapted from https://stackoverflow.com/a/12739441/6064933
|
||||
function! utils#RandInt(Low, High) abort
|
||||
" Use lua to generate random int. It is faster. Ref: https://stackoverflow.com/a/20157671/6064933
|
||||
call v:lua.math.randomseed(localtime())
|
||||
return v:lua.math.random(a:Low, a:High)
|
||||
endfunction
|
||||
|
||||
" Selection a random element from a sequence/list
|
||||
function! utils#RandElement(seq) abort
|
||||
let l:idx = utils#RandInt(0, len(a:seq)-1)
|
||||
|
||||
return a:seq[l:idx]
|
||||
endfunction
|
||||
|
||||
" Custom fold expr, adapted from https://vi.stackexchange.com/a/9094/15292
|
||||
function! utils#VimFolds(lnum) abort
|
||||
" get content of current line and the line below
|
||||
@ -198,14 +183,3 @@ function! utils#MultiEdit(patterns) abort
|
||||
endfor
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! utils#add_pack(name) abort
|
||||
let l:status = v:true
|
||||
try
|
||||
execute printf("packadd! %s", a:name)
|
||||
catch /^Vim\%((\a\+)\)\=:E919/
|
||||
let l:status = v:false
|
||||
endtry
|
||||
|
||||
return l:status
|
||||
endfunction
|
||||
|
||||
123
core/themes.lua
Normal file
123
core/themes.lua
Normal file
@ -0,0 +1,123 @@
|
||||
local utils = require('utils')
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Theme to directory name mapping, because theme repo name is not necessarily
|
||||
-- the same as the theme name itself.
|
||||
M.theme2dir = {
|
||||
gruvbox8 = "vim-gruvbox8",
|
||||
onedark = 'onedark.nvim',
|
||||
edge = 'edge',
|
||||
sonokai = 'sonokai',
|
||||
gruvbox_material = 'gruvbox-material',
|
||||
nord = 'nord.nvim',
|
||||
doom_one = 'doom-one.nvim',
|
||||
everforest = 'everforest',
|
||||
nightfox = 'nightfox.nvim',
|
||||
kanagawa = 'kanagawa.nvim',
|
||||
catppuccin = 'catppuccin'
|
||||
}
|
||||
|
||||
M.gruvbox8 = function()
|
||||
-- Italic options should be put before colorscheme setting,
|
||||
-- see https://github.com/morhetz/gruvbox/wiki/Terminal-specific#1-italics-is-disabled
|
||||
vim.g.gruvbox_italics = 1
|
||||
vim.g.gruvbox_italicize_strings = 1
|
||||
vim.g.gruvbox_filetype_hi_groups = 1
|
||||
vim.g.gruvbox_plugin_hi_groups = 1
|
||||
|
||||
vim.cmd [[colorscheme gruvbox8_hard]]
|
||||
end
|
||||
|
||||
M.onedark = function()
|
||||
vim.cmd [[colorscheme onedark]]
|
||||
end
|
||||
|
||||
M.edge = function()
|
||||
vim.g.edge_enable_italic = 1
|
||||
vim.g.edge_better_performance = 1
|
||||
|
||||
vim.cmd [[colorscheme edge]]
|
||||
end
|
||||
|
||||
M.sonokai = function()
|
||||
vim.g.sonokai_enable_italic = 1
|
||||
vim.g.sonokai_better_performance = 1
|
||||
|
||||
vim.cmd [[colorscheme sonokai]]
|
||||
end
|
||||
|
||||
M.gruvbox_material = function()
|
||||
vim.g.gruvbox_material_enable_italic = 1
|
||||
vim.g.gruvbox_material_better_performance = 1
|
||||
|
||||
vim.cmd [[colorscheme gruvbox-material]]
|
||||
end
|
||||
|
||||
M.nord = function()
|
||||
vim.cmd [[colorscheme nord]]
|
||||
end
|
||||
|
||||
M.doom_one = function()
|
||||
|
||||
vim.cmd [[colorscheme doom-one]]
|
||||
end
|
||||
|
||||
M.everforest = function()
|
||||
vim.g.everforest_enable_italic = 1
|
||||
vim.g.everforest_better_performance = 1
|
||||
|
||||
vim.cmd [[colorscheme everforest]]
|
||||
end
|
||||
|
||||
M.nightfox = function()
|
||||
vim.cmd [[colorscheme nordfox]]
|
||||
end
|
||||
|
||||
|
||||
M.kanagawa = function()
|
||||
vim.cmd [[colorscheme kanagawa]]
|
||||
end
|
||||
|
||||
M.catppuccin = function()
|
||||
-- available option: latte, frappe, macchiato, mocha
|
||||
vim.g.catppuccin_flavour = "frappe"
|
||||
|
||||
require("catppuccin").setup()
|
||||
|
||||
vim.cmd [[colorscheme catppuccin]]
|
||||
end
|
||||
|
||||
|
||||
--- Use a random theme from the pre-defined list of themes.
|
||||
M.rand_theme = function ()
|
||||
local theme = utils.rand_element(vim.tbl_keys(M.theme2dir))
|
||||
|
||||
if not vim.tbl_contains(vim.tbl_keys(M), theme) then
|
||||
local msg = "Invalid theme: " .. theme
|
||||
vim.notify(msg, vim.log.levels.ERROR, { title = 'nvim-config' })
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
-- Load the theme, because all the themes are declared as opt plugins, the theme isn't loaded yet.
|
||||
local status = utils.add_pack(M.theme2dir[theme])
|
||||
|
||||
if not status then
|
||||
local msg = string.format("Theme %s is not installed. Run PackerSync to install.", theme)
|
||||
vim.notify(msg, vim.log.levels.ERROR, { title = 'nvim-config' })
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
-- Set the theme.
|
||||
M[theme]()
|
||||
|
||||
if vim.g.logging_level == 'debug' then
|
||||
local msg = "Colorscheme: " .. theme
|
||||
|
||||
vim.notify(msg, vim.log.levels.DEBUG, { title = 'nvim-config' })
|
||||
end
|
||||
end
|
||||
|
||||
M.rand_theme()
|
||||
@ -1,99 +0,0 @@
|
||||
let s:theme_setup_dict = {}
|
||||
|
||||
function! s:theme_setup_dict.gruvbox8() dict abort
|
||||
" Italic options should be put before colorscheme setting,
|
||||
" see https://github.com/morhetz/gruvbox/wiki/Terminal-specific#1-italics-is-disabled
|
||||
let g:gruvbox_italics=1
|
||||
let g:gruvbox_italicize_strings=1
|
||||
let g:gruvbox_filetype_hi_groups = 1
|
||||
let g:gruvbox_plugin_hi_groups = 1
|
||||
colorscheme gruvbox8_hard
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.onedark() dict abort
|
||||
colorscheme onedark
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.edge() dict abort
|
||||
let g:edge_enable_italic = 1
|
||||
let g:edge_better_performance = 1
|
||||
colorscheme edge
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.sonokai() dict abort
|
||||
let g:sonokai_enable_italic = 1
|
||||
let g:sonokai_better_performance = 1
|
||||
colorscheme sonokai
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.gruvbox_material() dict abort
|
||||
let g:gruvbox_material_enable_italic = 1
|
||||
let g:gruvbox_material_better_performance = 1
|
||||
colorscheme gruvbox-material
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.nord() dict abort
|
||||
colorscheme nord
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.doom_one() dict abort
|
||||
colorscheme doom-one
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.everforest() dict abort
|
||||
let g:everforest_enable_italic = 1
|
||||
let g:everforest_better_performance = 1
|
||||
colorscheme everforest
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.nightfox() dict abort
|
||||
colorscheme nordfox
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.kanagawa() dict abort
|
||||
colorscheme kanagawa
|
||||
endfunction
|
||||
|
||||
function! s:theme_setup_dict.catppuccin() dict abort
|
||||
let g:catppuccin_flavour = "frappe" " latte, frappe, macchiato, mocha
|
||||
|
||||
lua require("catppuccin").setup()
|
||||
colorscheme catppuccin
|
||||
endfunction
|
||||
|
||||
" Theme to directory name mapping, because theme repo name is not necessarily
|
||||
" the same as the theme name itself.
|
||||
let s:theme2dir = {
|
||||
\ 'gruvbox8' : 'vim-gruvbox8',
|
||||
\ 'onedark': 'onedark.nvim',
|
||||
\ 'edge' : 'edge',
|
||||
\ 'sonokai': 'sonokai',
|
||||
\ 'gruvbox_material': 'gruvbox-material',
|
||||
\ 'nord': 'nord.nvim',
|
||||
\ 'doom_one': 'doom-one.nvim',
|
||||
\ 'everforest' :'everforest',
|
||||
\ 'nightfox': 'nightfox.nvim',
|
||||
\ 'kanagawa': 'kanagawa.nvim',
|
||||
\ 'catppuccin': 'catppuccin'
|
||||
\ }
|
||||
|
||||
let s:theme = utils#RandElement(keys(s:theme2dir))
|
||||
let s:colorscheme_func = printf('s:theme_setup_dict.%s()', s:theme)
|
||||
|
||||
if !has_key(s:theme_setup_dict, s:theme)
|
||||
let s:msg = "Invalid colorscheme function: " . s:colorscheme_func
|
||||
call v:lua.vim.notify(s:msg, 'error', {'title': 'nvim-config'})
|
||||
finish
|
||||
endif
|
||||
|
||||
let s:status = utils#add_pack(s:theme2dir[s:theme])
|
||||
if !s:status
|
||||
echomsg printf("Theme %s not installed. Run PackerSync to install.", s:theme)
|
||||
finish
|
||||
endif
|
||||
|
||||
execute 'call ' . s:colorscheme_func
|
||||
if g:logging_level == 'debug'
|
||||
let s:msg1 = "Colorscheme: " . s:theme
|
||||
call v:lua.vim.notify(s:msg1, 'info', {'title': 'nvim-config'})
|
||||
endif
|
||||
2
init.lua
2
init.lua
@ -26,7 +26,7 @@ local core_conf_files = {
|
||||
"autocommands.vim",
|
||||
"mappings.vim",
|
||||
"plugins.vim",
|
||||
"themes.vim",
|
||||
"themes.lua",
|
||||
}
|
||||
|
||||
-- source all the core config files
|
||||
|
||||
@ -31,4 +31,30 @@ function M.get_nvim_version()
|
||||
return nvim_ver_str
|
||||
end
|
||||
|
||||
|
||||
--- Generate random integers in the range [Low, High], inclusive,
|
||||
--- adapted from https://stackoverflow.com/a/12739441/6064933
|
||||
--- @low: the lower value for this range
|
||||
--- @high: the upper value for this range
|
||||
function M.rand_int(low, high)
|
||||
-- Use lua to generate random int, see also: https://stackoverflow.com/a/20157671/6064933
|
||||
math.randomseed(os.time())
|
||||
|
||||
return math.random(low, high)
|
||||
end
|
||||
|
||||
--- Select a random element from a sequence/list.
|
||||
--- @seq: the sequence to choose an element
|
||||
function M.rand_element(seq)
|
||||
local idx = M.rand_int(1, #seq)
|
||||
|
||||
return seq[idx]
|
||||
end
|
||||
|
||||
function M.add_pack(name)
|
||||
local status, error = pcall(vim.cmd, "packadd " .. name)
|
||||
|
||||
return status
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user