alpha.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. local function configure_additional_autocmds()
  59. local group = "_dashboard_settings"
  60. vim.api.nvim_create_augroup(group, {})
  61. vim.api.nvim_create_autocmd("FileType", {
  62. group = group,
  63. pattern = "alpha",
  64. command = "set showtabline=0 | autocmd BufLeave <buffer> set showtabline=" .. vim.opt.showtabline._value,
  65. })
  66. if not lvim.builtin.lualine.options.globalstatus then
  67. -- https://github.com/goolord/alpha-nvim/issues/42
  68. vim.api.nvim_create_autocmd("FileType", {
  69. group = group,
  70. pattern = "alpha",
  71. command = "set laststatus=0 | autocmd BufUnload <buffer> set laststatus=" .. vim.opt.laststatus._value,
  72. })
  73. end
  74. end
  75. function M.setup()
  76. local status_ok, alpha = pcall(require, "alpha")
  77. if not status_ok then
  78. return
  79. end
  80. local mode = lvim.builtin.alpha.mode
  81. local config = lvim.builtin.alpha[mode].config
  82. -- this makes it easier to use a completely custom configuration
  83. if vim.tbl_isempty(config) then
  84. config = resolve_config(mode)
  85. end
  86. alpha.setup(config)
  87. configure_additional_autocmds()
  88. end
  89. return M