goolord / alpha-nvim

a lua powered greeter like vim-startify / dashboard-nvim
MIT License
1.92k stars 113 forks source link

Random colours #36

Closed quantum-booty closed 3 years ago

quantum-booty commented 3 years ago

I want to generate a new dashboard colour at every vim start. I wrote this script but apparently the random_colours function generates the same colour everytime I start vim.

function random_colours(colours)
   return colours[math.random(1, #colours)]
end
colours = {'cyan', 'red', 'yellow', 'orange', 'magenta', 'white', 'violet', 'lightyellow', 'seagreen', 'slateblue'}
vim.cmd(string.format('highlight dashboard guifg=%s guibg=bg', random_colours(colours)))
dashboard.section.header.opts.hl = 'dashboard'

The whole config is here:

vim.g.indentLine_fileTypeExclude = {'alpha'}

local alpha = require'alpha'
local dashboard = require'alpha.themes.dashboard'

dashboard.section.header.val = {
   "                                   ",
   "                                   ",
   "                                   ",
   "   ⣴⣶⣤⡤⠦⣤⣀⣤⠆     ⣈⣭⣿⣶⣿⣦⣼⣆          ",
   "    ⠉⠻⢿⣿⠿⣿⣿⣶⣦⠤⠄⡠⢾⣿⣿⡿⠋⠉⠉⠻⣿⣿⡛⣦       ",
   "          ⠈⢿⣿⣟⠦ ⣾⣿⣿⣷    ⠻⠿⢿⣿⣧⣄     ",
   "           ⣸⣿⣿⢧ ⢻⠻⣿⣿⣷⣄⣀⠄⠢⣀⡀⠈⠙⠿⠄    ",
   "          ⢠⣿⣿⣿⠈    ⣻⣿⣿⣿⣿⣿⣿⣿⣛⣳⣤⣀⣀   ",
   "   ⢠⣧⣶⣥⡤⢄ ⣸⣿⣿⠘  ⢀⣴⣿⣿⡿⠛⣿⣿⣧⠈⢿⠿⠟⠛⠻⠿⠄  ",
   "  ⣰⣿⣿⠛⠻⣿⣿⡦⢹⣿⣷   ⢊⣿⣿⡏  ⢸⣿⣿⡇ ⢀⣠⣄⣾⠄   ",
   " ⣠⣿⠿⠛ ⢀⣿⣿⣷⠘⢿⣿⣦⡀ ⢸⢿⣿⣿⣄ ⣸⣿⣿⡇⣪⣿⡿⠿⣿⣷⡄  ",
   " ⠙⠃   ⣼⣿⡟  ⠈⠻⣿⣿⣦⣌⡇⠻⣿⣿⣷⣿⣿⣿ ⣿⣿⡇ ⠛⠻⢷⣄ ",
   "      ⢻⣿⣿⣄   ⠈⠻⣿⣿⣿⣷⣿⣿⣿⣿⣿⡟ ⠫⢿⣿⡆     ",
   "       ⠻⣿⣿⣿⣿⣶⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⡟⢀⣀⣤⣾⡿⠃     ",
   "                                   ",
}
-- # TODO: get this shit working
-- random coloured dragon fuck yeah!!!
function random_colours(colours)
   return colours[math.random(1, #colours)]
end
colours = {'cyan', 'red', 'yellow', 'orange', 'magenta', 'white', 'violet', 'lightyellow', 'seagreen', 'slateblue'}
vim.cmd(string.format('highlight dashboard guifg=%s guibg=bg', random_colours(colours)))

dashboard.section.header.opts.hl = 'dashboard'

dashboard.section.buttons.val = {
    dashboard.button( "n", "  New file", ":ene <BAR> startinsert <CR>"),
    dashboard.button( "f", "  Find File", ":Telescope find_files<CR>"),
    dashboard.button( "r", "  Recents", ":Telescope frecency<CR>"),
    dashboard.button( "w", "  Find Word", ":Telescope live_grep<CR>"),
    dashboard.button( "b", "  Bookmarks", ":Telescope marks<CR>"),
    dashboard.button( "s", "  Load Last Session", ":SessionLoad<CR>"),
    dashboard.button( "q", "  Quit NVIM", ":qa<CR>"),
    }

local handle = io.popen('fortune')
local fortune = handle:read("*a")
handle:close()
dashboard.section.footer.val = fortune
alpha.setup(dashboard.opts)
goolord commented 3 years ago

it turns out lua's rng sucks. you can fix this behavior by updating the seed with some external source of entropy, this snippet uses os time

    math.randomseed(os.time())
    local colours = {'cyan', 'red', 'yellow', 'orange', 'magenta', 'white', 'violet', 'lightyellow', 'seagreen', 'slateblue'}
    local function random_colours(colours_list)
       return math.random(1, #colours_list)
    end
quantum-booty commented 3 years ago

it turns out lua's rng sucks. you can fix this behavior buy updating the seed with some external source of entropy, this snippet uses os time

    math.randomseed(os.time())
    local colours = {'cyan', 'red', 'yellow', 'orange', 'magenta', 'white', 'violet', 'lightyellow', 'seagreen', 'slateblue'}
    local function random_colours(colours_list)
       return math.random(1, #colours_list)
    end

Thank you this works :)