alpha.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. -- account for different icon byte sizes in cursor offset
  36. local _, icon_length = button_element.val:find "[\128-\255]*"
  37. button_element.opts.cursor = icon_length + 2
  38. button_element.opts = vim.tbl_extend("force", button_element.opts, entry[4] or button_section.opts or {})
  39. table.insert(val, button_element)
  40. end
  41. return val
  42. end
  43. local function resolve_config(theme_name)
  44. local selected_theme = require("alpha.themes." .. theme_name)
  45. local resolved_section = selected_theme.section
  46. local section = lvim.builtin.alpha[theme_name].section
  47. for name, el in pairs(section) do
  48. for k, v in pairs(el) do
  49. if name:match "buttons" and k == "entries" then
  50. resolved_section[name].val = resolve_buttons(theme_name, el)
  51. elseif v then
  52. resolved_section[name][k] = v
  53. end
  54. end
  55. resolved_section[name].opts = el.opts or {}
  56. end
  57. local opts = lvim.builtin.alpha[theme_name].opts or {}
  58. selected_theme.config.opts = vim.tbl_extend("force", selected_theme.config.opts, opts)
  59. return selected_theme.config
  60. end
  61. function M.setup()
  62. local status_ok, alpha = pcall(require, "alpha")
  63. if not status_ok then
  64. return
  65. end
  66. local mode = lvim.builtin.alpha.mode
  67. local config = lvim.builtin.alpha[mode].config
  68. -- this makes it easier to use a completely custom configuration
  69. if vim.tbl_isempty(config) then
  70. config = resolve_config(mode)
  71. end
  72. alpha.setup(config)
  73. end
  74. return M