|
| 1 | +return { |
| 2 | + "goolord/alpha-nvim", |
| 3 | + cmd = "Alpha", |
| 4 | + specs = { |
| 5 | + { "folke/snacks.nvim", optional = true, opts = { dashboard = { enabled = false } } }, |
| 6 | + { |
| 7 | + "AstroNvim/astrocore", |
| 8 | + opts = function(_, opts) |
| 9 | + local maps = opts.mappings |
| 10 | + maps.n["<Leader>h"] = { |
| 11 | + function() |
| 12 | + local is_valid_win = function(win) |
| 13 | + local bufnr = vim.api.nvim_win_get_buf(win) |
| 14 | + return require("astrocore.buffer").is_valid(bufnr) or vim.bo[bufnr].filetype == "alpha" |
| 15 | + end |
| 16 | + local cur_win = vim.api.nvim_get_current_win() |
| 17 | + -- try to find the most suitable window for Alpha |
| 18 | + if not is_valid_win(cur_win) then |
| 19 | + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do |
| 20 | + if win ~= cur_win and is_valid_win(win) then |
| 21 | + vim.fn.win_gotoid(win) |
| 22 | + break |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + require("alpha").start(false) |
| 27 | + end, |
| 28 | + desc = "Home Screen", |
| 29 | + } |
| 30 | + opts.autocmds.alpha_settings = { |
| 31 | + { |
| 32 | + event = { "User", "BufWinEnter" }, |
| 33 | + desc = "Disable status, tablines, and cmdheight for alpha", |
| 34 | + callback = function(event) |
| 35 | + if |
| 36 | + ( |
| 37 | + (event.event == "User" and event.file == "AlphaReady") |
| 38 | + or (event.event == "BufWinEnter" and vim.bo[event.buf].filetype == "alpha") |
| 39 | + ) and not vim.g.before_alpha |
| 40 | + then |
| 41 | + vim.g.before_alpha = { |
| 42 | + showtabline = vim.opt.showtabline:get(), |
| 43 | + laststatus = vim.opt.laststatus:get(), |
| 44 | + cmdheight = vim.opt.cmdheight:get(), |
| 45 | + } |
| 46 | + vim.opt.showtabline, vim.opt.laststatus, vim.opt.cmdheight = 0, 0, 0 |
| 47 | + elseif vim.g.before_alpha and event.event == "BufWinEnter" and vim.bo[event.buf].buftype ~= "nofile" then |
| 48 | + vim.opt.laststatus, vim.opt.showtabline, vim.opt.cmdheight = |
| 49 | + vim.g.before_alpha.laststatus, vim.g.before_alpha.showtabline, vim.g.before_alpha.cmdheight |
| 50 | + vim.g.before_alpha = nil |
| 51 | + end |
| 52 | + end, |
| 53 | + }, |
| 54 | + } |
| 55 | + opts.autocmds.alpha_autostart = { |
| 56 | + { |
| 57 | + event = "VimEnter", |
| 58 | + desc = "Start Alpha when vim is opened with no arguments", |
| 59 | + callback = function() |
| 60 | + local should_skip |
| 61 | + local lines = vim.api.nvim_buf_get_lines(0, 0, 2, false) |
| 62 | + if |
| 63 | + vim.fn.argc() > 0 -- don't start when opening a file |
| 64 | + or #lines > 1 -- don't open if current buffer has more than 1 line |
| 65 | + or (#lines == 1 and lines[1]:len() > 0) -- don't open the current buffer if it has anything on the first line |
| 66 | + or #vim.tbl_filter(function(bufnr) return vim.bo[bufnr].buflisted end, vim.api.nvim_list_bufs()) > 1 -- don't open if any listed buffers |
| 67 | + or not vim.o.modifiable -- don't open if not modifiable |
| 68 | + then |
| 69 | + should_skip = true |
| 70 | + else |
| 71 | + for _, arg in pairs(vim.v.argv) do |
| 72 | + if arg == "-b" or arg == "-c" or vim.startswith(arg, "+") or arg == "-S" then |
| 73 | + should_skip = true |
| 74 | + break |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + if should_skip then return end |
| 79 | + require("lazy").load { plugins = { "alpha-nvim" } } |
| 80 | + require("alpha").start(true) |
| 81 | + vim.schedule(function() vim.cmd.doautocmd "FileType" end) |
| 82 | + end, |
| 83 | + }, |
| 84 | + } |
| 85 | + end, |
| 86 | + }, |
| 87 | + }, |
| 88 | + opts = function() |
| 89 | + local dashboard = require "alpha.themes.dashboard" |
| 90 | + |
| 91 | + dashboard.leader = "LDR" |
| 92 | + |
| 93 | + --- @param shortcut string Shortcut string of a button mapping |
| 94 | + --- @param desc string Real text description of the mapping |
| 95 | + --- @param rhs string? Righthand side of mapping if defining a new mapping (_optional_) |
| 96 | + --- @param map_opts table? `keymap.set` options used during keymap creating (_optional_) |
| 97 | + dashboard.button = function(shortcut, desc, rhs, map_opts) |
| 98 | + -- HACK: fixes leader customization, remove after fixed upstream |
| 99 | + -- https://github.com/goolord/alpha-nvim/pull/271 |
| 100 | + local lhs = shortcut:gsub("%s", ""):gsub(dashboard.leader, "<Leader>") |
| 101 | + local default_map_opts = { noremap = true, silent = true, nowait = true, desc = desc } |
| 102 | + |
| 103 | + local leader = vim.g.mapleader |
| 104 | + if leader == " " then leader = "SPC" end |
| 105 | + |
| 106 | + return { |
| 107 | + type = "button", |
| 108 | + val = desc, |
| 109 | + on_press = function() |
| 110 | + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(rhs or lhs .. "<Ignore>", true, false, true), "t", false) |
| 111 | + end, |
| 112 | + opts = { |
| 113 | + position = "center", |
| 114 | + shortcut = shortcut:gsub(dashboard.leader, leader), |
| 115 | + cursor = -2, |
| 116 | + width = 36, |
| 117 | + align_shortcut = "right", |
| 118 | + hl = "DashboardCenter", |
| 119 | + hl_shortcut = "DashboardShortcut", |
| 120 | + keymap = rhs and { "n", lhs, rhs, require("astrocore").extend_tbl(default_map_opts, map_opts) }, |
| 121 | + }, |
| 122 | + } |
| 123 | + end |
| 124 | + |
| 125 | + dashboard.section.header.val = { |
| 126 | + " █████ ███████ ████████ ██████ ██████", |
| 127 | + "██ ██ ██ ██ ██ ██ ██ ██", |
| 128 | + "███████ ███████ ██ ██████ ██ ██", |
| 129 | + "██ ██ ██ ██ ██ ██ ██ ██", |
| 130 | + "██ ██ ███████ ██ ██ ██ ██████", |
| 131 | + " ", |
| 132 | + " ███ ██ ██ ██ ██ ███ ███", |
| 133 | + " ████ ██ ██ ██ ██ ████ ████", |
| 134 | + " ██ ██ ██ ██ ██ ██ ██ ████ ██", |
| 135 | + " ██ ██ ██ ██ ██ ██ ██ ██ ██", |
| 136 | + " ██ ████ ████ ██ ██ ██", |
| 137 | + } |
| 138 | + dashboard.section.header.opts.hl = "DashboardHeader" |
| 139 | + dashboard.section.footer.opts.hl = "DashboardFooter" |
| 140 | + |
| 141 | + local get_icon = require("astroui").get_icon |
| 142 | + dashboard.section.buttons.val = { |
| 143 | + dashboard.button("LDR n ", get_icon("FileNew", 2, true) .. "New File "), |
| 144 | + dashboard.button("LDR f f", get_icon("Search", 2, true) .. "Find File "), |
| 145 | + dashboard.button("LDR f o", get_icon("DefaultFile", 2, true) .. "Recents "), |
| 146 | + dashboard.button("LDR f w", get_icon("WordFile", 2, true) .. "Find Word "), |
| 147 | + dashboard.button("LDR f '", get_icon("Bookmarks", 2, true) .. "Bookmarks "), |
| 148 | + dashboard.button("LDR S l", get_icon("Refresh", 2, true) .. "Last Session "), |
| 149 | + } |
| 150 | + |
| 151 | + dashboard.config.layout = { |
| 152 | + { type = "padding", val = vim.fn.max { 2, vim.fn.floor(vim.fn.winheight(0) * 0.2) } }, |
| 153 | + dashboard.section.header, |
| 154 | + { type = "padding", val = 5 }, |
| 155 | + dashboard.section.buttons, |
| 156 | + { type = "padding", val = 3 }, |
| 157 | + dashboard.section.footer, |
| 158 | + } |
| 159 | + dashboard.config.opts.noautocmd = true |
| 160 | + return dashboard |
| 161 | + end, |
| 162 | + config = function(_, opts) |
| 163 | + require("alpha").setup(opts.config) |
| 164 | + vim.api.nvim_create_autocmd("User", { |
| 165 | + pattern = "LazyVimStarted", |
| 166 | + desc = "Add Alpha dashboard footer", |
| 167 | + once = true, |
| 168 | + callback = function() |
| 169 | + local stats = require("lazy").stats() |
| 170 | + local ms = math.floor(stats.startuptime * 100 + 0.5) / 100 |
| 171 | + opts.section.footer.val = { |
| 172 | + "AstroNvim loaded " .. stats.loaded .. "/" .. stats.count .. " plugins " .. require("astroui").get_icon( |
| 173 | + "Package", |
| 174 | + 1, |
| 175 | + true |
| 176 | + ) .. "in " .. ms .. "ms", |
| 177 | + } |
| 178 | + pcall(vim.cmd.AlphaRedraw) |
| 179 | + end, |
| 180 | + }) |
| 181 | + end, |
| 182 | +} |
0 commit comments