alpha.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local M = {}
  2. function M.config()
  3. local lvim_dashboard = require "lvim.core.alpha.dashboard"
  4. local lvim_startify = require "lvim.core.alpha.startify"
  5. lvim.builtin.alpha = {
  6. dashboard = {
  7. config = {},
  8. section = lvim_dashboard.get_sections(),
  9. opts = { autostart = true },
  10. },
  11. startify = {
  12. config = {},
  13. section = lvim_startify.get_sections(),
  14. opts = { autostart = true },
  15. },
  16. active = true,
  17. mode = "dashboard",
  18. }
  19. end
  20. local function resolve_buttons(theme_name, button_section)
  21. if button_section.val and #button_section.val > 0 then
  22. return button_section.val
  23. end
  24. local selected_theme = require("alpha.themes." .. theme_name)
  25. local val = {}
  26. for _, entry in pairs(button_section.entries) do
  27. local on_press = function()
  28. local sc_ = entry[1]:gsub("%s", ""):gsub("SPC", "<leader>")
  29. local key = vim.api.nvim_replace_termcodes(sc_, true, false, true)
  30. vim.api.nvim_feedkeys(key, "normal", false)
  31. end
  32. local button_element = selected_theme.button(entry[1], entry[2], entry[3])
  33. -- this became necessary after recent changes in alpha.nvim (06ade3a20ca9e79a7038b98d05a23d7b6c016174)
  34. button_element.on_press = on_press
  35. button_element.opts = vim.tbl_extend("force", button_element.opts, entry[4] or button_section.opts or {})
  36. table.insert(val, button_element)
  37. end
  38. return val
  39. end
  40. local function resolve_config(theme_name)
  41. local selected_theme = require("alpha.themes." .. theme_name)
  42. local resolved_section = selected_theme.section
  43. local section = lvim.builtin.alpha[theme_name].section
  44. for name, el in pairs(section) do
  45. for k, v in pairs(el) do
  46. if name:match "buttons" and k == "entries" then
  47. resolved_section[name].val = resolve_buttons(theme_name, el)
  48. elseif v then
  49. resolved_section[name][k] = v
  50. end
  51. end
  52. resolved_section[name].opts = el.opts or {}
  53. end
  54. local opts = lvim.builtin.alpha[theme_name].opts or {}
  55. selected_theme.config.opts = vim.tbl_extend("force", selected_theme.config.opts, opts)
  56. return selected_theme.config
  57. end
  58. function M.setup()
  59. local status_ok, alpha = pcall(require, "alpha")
  60. if not status_ok then
  61. return
  62. end
  63. local mode = lvim.builtin.alpha.mode
  64. local config = lvim.builtin.alpha[mode].config
  65. -- this makes it easier to use a completely custom configuration
  66. if vim.tbl_isempty(config) then
  67. config = resolve_config(mode)
  68. end
  69. alpha.setup(config)
  70. end
  71. return M