dotfiles-guix/home/config/nvim/lua/menu.lua

234 lines
5.2 KiB
Lua
Raw Normal View History

2024-06-22 02:52:21 +00:00
local path_ok, plenary_path = pcall(require, "plenary.path")
if not path_ok then
2024-10-18 04:04:03 +00:00
return
2024-06-22 02:52:21 +00:00
end
local dashboard = require("alpha.themes.dashboard")
local cdir = vim.fn.getcwd()
local if_nil = vim.F.if_nil
local nvim_web_devicons = {
2024-10-18 04:04:03 +00:00
enabled = true,
highlight = true,
2024-06-22 02:52:21 +00:00
}
local function get_extension(fn)
2024-10-18 04:04:03 +00:00
local match = fn:match("^.+(%..+)$")
local ext = ""
if match ~= nil then
ext = match:sub(2)
end
return ext
2024-06-22 02:52:21 +00:00
end
local function icon(fn)
2024-10-18 04:04:03 +00:00
local nwd = require("nvim-web-devicons")
local ext = get_extension(fn)
return nwd.get_icon(fn, ext, { default = true })
2024-06-22 02:52:21 +00:00
end
local function file_button(fn, sc, short_fn,autocd)
2024-10-18 04:04:03 +00:00
short_fn = short_fn or fn
local ico_txt
local fb_hl = {}
if nvim_web_devicons.enabled then
local ico, hl = icon(fn)
local hl_option_type = type(nvim_web_devicons.highlight)
if hl_option_type == "boolean" then
if hl and nvim_web_devicons.highlight then
table.insert(fb_hl, { hl, 0, #ico })
end
end
if hl_option_type == "string" then
table.insert(fb_hl, { nvim_web_devicons.highlight, 0, #ico })
end
ico_txt = ico .. " "
else
ico_txt = ""
end
local cd_cmd = (autocd and " | cd %:p:h" or "")
local file_button_el = dashboard.button(sc, ico_txt .. short_fn, "<cmd>e " .. vim.fn.fnameescape(fn) .. cd_cmd .." <CR>")
local fn_start = short_fn:match(".*[/\\]")
if fn_start ~= nil then
table.insert(fb_hl, { "Comment", #ico_txt - 2, #fn_start + #ico_txt })
end
file_button_el.opts.hl = fb_hl
return file_button_el
2024-06-22 02:52:21 +00:00
end
local default_mru_ignore = { "gitcommit" }
local mru_opts = {
2024-10-18 04:04:03 +00:00
ignore = function(path, ext)
return (string.find(path, "COMMIT_EDITMSG")) or (vim.tbl_contains(default_mru_ignore, ext))
end,
autocd = false
2024-06-22 02:52:21 +00:00
}
--- @param start number
--- @param cwd string? optional
--- @param items_number number? optional number of items to generate, default = 10
local function mru(start, cwd, items_number, opts)
2024-10-18 04:04:03 +00:00
opts = opts or mru_opts
items_number = if_nil(items_number, 10)
local oldfiles = {}
for _, v in pairs(vim.v.oldfiles) do
if #oldfiles == items_number then
break
end
local cwd_cond
if not cwd then
cwd_cond = true
else
cwd_cond = vim.startswith(v, cwd)
end
local ignore = (opts.ignore and opts.ignore(v, get_extension(v))) or false
if (vim.fn.filereadable(v) == 1) and cwd_cond and not ignore then
oldfiles[#oldfiles + 1] = v
end
end
local target_width = 35
local tbl = {}
for i, fn in ipairs(oldfiles) do
local short_fn
if cwd then
short_fn = vim.fn.fnamemodify(fn, ":.")
else
short_fn = vim.fn.fnamemodify(fn, ":~")
end
if #short_fn > target_width then
short_fn = plenary_path.new(short_fn):shorten(1, { -2, -1 })
if #short_fn > target_width then
short_fn = plenary_path.new(short_fn):shorten(1, { -1 })
end
end
local shortcut = tostring(i + start - 1)
local file_button_el = file_button(fn, shortcut, short_fn,opts.autocd)
tbl[i] = file_button_el
end
return {
type = "group",
val = tbl,
opts = {},
}
2024-06-22 02:52:21 +00:00
end
local cats = {
{
2024-10-18 04:04:03 +00:00
[[ ,-. _,---._ __ / \]],
[[ / ) .-' `./ / \]],
[[( ( ,' `/ /|]],
[[ \ `-" \'\ / |]],
[[ `. , \ \ / |]],
[[ /`. ,'-`----Y |]],
[[ ( ; | ']],
[[ | ,-. ,-' | /]],
[[ | | ( | hjw | /]],
2024-06-22 02:52:21 +00:00
[[ ) | \ `.___________|/]],
[[ `--' `--']],
},
{
[[ _]],
2024-10-18 04:04:03 +00:00
[[ \`*-. ]],
[[ ) _`-. ]],
[[ . : `. . ]],
[[ : _ ' \ ]],
[[ ; *` _. `*-._ ]],
[[ `-.-' `-. ]],
[[ ; ` `. ]],
[[ :. . \ ]],
[[ . \ . : .-' . ]],
[[ ' `+.; ; ' : ]],
[[ : ' | ; ;-. ]],
[[ ; ' : :`-: _.`* ;]],
2024-06-22 02:52:21 +00:00
[[[bug] .*' / .*' ; .*`- +' `*' ]],
2024-10-18 04:04:03 +00:00
[[ `*-* `*-* `*-*']],
2024-06-22 02:52:21 +00:00
},
}
math.randomseed(os.time())
local header = {
2024-10-18 04:04:03 +00:00
type = "text",
2024-06-22 02:52:21 +00:00
val = cats[math.random(1, #cats)],
2024-10-18 04:04:03 +00:00
opts = {
position = "center",
hl = "Type",
},
2024-06-22 02:52:21 +00:00
}
local section_mru = {
2024-10-18 04:04:03 +00:00
type = "group",
val = {
{
type = "text",
val = "Recent files",
opts = {
hl = "SpecialComment",
shrink_margin = false,
position = "center",
},
},
{ type = "padding", val = 1 },
{
type = "group",
val = function()
return { mru(0, cdir) }
end,
opts = { shrink_margin = false },
},
},
2024-06-22 02:52:21 +00:00
}
local buttons = {
2024-10-18 04:04:03 +00:00
type = "group",
val = {
{ type = "text", val = "Quick links", opts = { hl = "SpecialComment", position = "center" } },
{ type = "padding", val = 1 },
dashboard.button("e", " New file", "<cmd>ene<CR>"),
dashboard.button("SPC f f", "󰈞 Find file"),
dashboard.button("SPC f g", "󰊄 Live grep"),
dashboard.button("SPC p", " Update plugins", "<cmd>PlugUpdate<CR>"),
dashboard.button("c", " Configuration", "<cmd>cd ~/.config/nvim/ <CR>"),
dashboard.button("q", "󰅚 Quit", "<cmd>qa<CR>"),
},
position = "center",
2024-06-22 02:52:21 +00:00
}
local config = {
2024-10-18 04:04:03 +00:00
layout = {
{ type = "padding", val = 2 },
header,
{ type = "padding", val = 2 },
section_mru,
{ type = "padding", val = 2 },
buttons,
},
opts = {
margin = 5,
setup = function()
vim.api.nvim_create_autocmd('DirChanged', {
pattern = '*',
group = "alpha_temp",
callback = function () require('alpha').redraw() end,
})
end,
},
2024-06-22 02:52:21 +00:00
}
return {
2024-10-18 04:04:03 +00:00
header = header,
buttons = buttons,
mru = mru,
config = config,
-- theme specific config
mru_opts = mru_opts,
leader = dashboard.leader,
nvim_web_devicons = nvim_web_devicons,
2024-06-22 02:52:21 +00:00
}