aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjdhao <jdhao@hotmail.com>2021-10-15 23:20:35 +0800
committerjdhao <jdhao@hotmail.com>2021-10-15 23:20:35 +0800
commit421111f474add93d16e66529b286fc07ecfb63af (patch)
treed00127e8961bc6c063614868f1d119ea5ce5ba46
parentb4f58504a757dc7b8cf0c0694faf9fb535b9be8f (diff)
refactor: load colorschemes randomly
In Emacs, there is `seq-random-elt` which randomly selects an element from a sequence. We mimic it here in Neovim. Ref: https://www.gnu.org/software/emacs/manual/html_node/elisp/Sequence-Functions.html.
-rw-r--r--autoload/utils.vim7
-rw-r--r--core/ui.vim14
2 files changed, 18 insertions, 3 deletions
diff --git a/autoload/utils.vim b/autoload/utils.vim
index d1a4a47..9ab0991 100644
--- a/autoload/utils.vim
+++ b/autoload/utils.vim
@@ -49,6 +49,13 @@ function! utils#RandInt(Low, High) abort
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
diff --git a/core/ui.vim b/core/ui.vim
index 63a3128..acefa70 100644
--- a/core/ui.vim
+++ b/core/ui.vim
@@ -76,14 +76,22 @@ function! s:my_theme_dict.everforest() dict abort
colorscheme everforest
endfunction
+function! s:my_theme_dict.nightfox() dict abort
+ packadd! nightfox
+
+ colorscheme nordfox
+endfunction
+
let s:candidate_theme = ['gruvbox8', 'deus', 'solarized8', 'onedark',
- \ 'edge', 'sonokai', 'gruvbox_material', 'nord', 'doom_one', 'everforest']
-let s:idx = utils#RandInt(0, len(s:candidate_theme)-1)
-let s:theme = s:candidate_theme[s:idx]
+ \ 'edge', 'sonokai', 'gruvbox_material', 'nord', 'doom_one', 'everforest',
+ \ 'nightfox']
+let s:theme = utils#RandElement(s:candidate_theme)
let s:colorscheme_func = printf('s:my_theme_dict.%s()', s:theme)
+
if has_key(s:my_theme_dict, s:theme)
execute 'call ' . s:colorscheme_func
+ echomsg "Currently loaded theme:" s:theme
else
let s:msg = "Invalid colorscheme function: " . s:colorscheme_func
call v:lua.vim.notify(s:msg, 'error', {'title': 'nvim-config', 'timeout': 2500})