From 89b948652f0808bde17c3532d9d82f39d3ec9352 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 10 Oct 2023 19:49:12 -0400 Subject: [PATCH] nvim cat menu --- home-config/nvim/lua/menu.lua | 261 ++++++++++++++++++++++++++++++++ home-config/nvim/lua/plugin.lua | 2 +- home-config/nvim/lua/theme.lua | 3 + 3 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 home-config/nvim/lua/menu.lua diff --git a/home-config/nvim/lua/menu.lua b/home-config/nvim/lua/menu.lua new file mode 100644 index 0000000..ff8e1e3 --- /dev/null +++ b/home-config/nvim/lua/menu.lua @@ -0,0 +1,261 @@ +local path_ok, plenary_path = pcall(require, "plenary.path") +if not path_ok then + return +end + +local dashboard = require("alpha.themes.dashboard") +local cdir = vim.fn.getcwd() +local if_nil = vim.F.if_nil + +local nvim_web_devicons = { + enabled = true, + highlight = true, +} + +local function get_extension(fn) + local match = fn:match("^.+(%..+)$") + local ext = "" + if match ~= nil then + ext = match:sub(2) + end + return ext +end + +local function icon(fn) + local nwd = require("nvim-web-devicons") + local ext = get_extension(fn) + return nwd.get_icon(fn, ext, { default = true }) +end + +local function file_button(fn, sc, short_fn,autocd) + 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, "e " .. vim.fn.fnameescape(fn) .. cd_cmd .." ") + 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 +end + +local default_mru_ignore = { "gitcommit" } + +local mru_opts = { + ignore = function(path, ext) + return (string.find(path, "COMMIT_EDITMSG")) or (vim.tbl_contains(default_mru_ignore, ext)) + end, + autocd = false +} + +--- @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) + 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 = {}, + } +end + +local cats = { + { + [[ ,-. _,---._ __ / \]], + [[ / ) .-' `./ / \]], + [[( ( ,' `/ /|]], + [[ \ `-" \'\ / |]], + [[ `. , \ \ / |]], + [[ /`. ,'-`----Y |]], + [[ ( ; | ']], + [[ | ,-. ,-' | /]], + [[ | | ( | hjw | /]], + [[ ) | \ `.___________|/]], + [[ `--' `--']], + }, + { + [[ _]], + [[ \`*-. ]], + [[ ) _`-. ]], + [[ . : `. . ]], + [[ : _ ' \ ]], + [[ ; *` _. `*-._ ]], + [[ `-.-' `-. ]], + [[ ; ` `. ]], + [[ :. . \ ]], + [[ . \ . : .-' . ]], + [[ ' `+.; ; ' : ]], + [[ : ' | ; ;-. ]], + [[ ; ' : :`-: _.`* ;]], + [[[bug] .*' / .*' ; .*`- +' `*' ]], + [[ `*-* `*-* `*-*']], + }, + { + [[ .-o=o-.]], + [[ , /=o=o=o=\ .--.]], + [[ _|\|=o=O=o=O=| \]], + [[ __.' a`\=o=o=o=(`\ /]], + [[ '. a 4/`|.-""'`\ \ ;'`) .---.]], + [[ \ .' / .--' |_.' / .-._)]], + [[ `) _.' / /`-.__.' /]], + [[ jgs `'-.____; /'-.___.-']], + [[ `"""`]], + }, + { + [[ (`.]], + [[ ) )]], + [[ ( (]], + [[ \ \]], + [[ \ \]], + [[ .-' `-.]], + [[ / `.]], + [[ ( ) `-._ , _]], + [[ ) ,' (.\--'(]], + [[ \ ( ) / \]], + [[ \ \_( / ( <0 (0]], + [[ \_)))\ ( `._ ::Y)__]], + [[ ''' \ `-._.'`---^_)))]], + [[ `-._ ))) ```]], + [[ ``` hjw]], + } +} + +math.randomseed(os.time()) +local header = { + type = "text", + val = cats[math.random(1, #cats)], + opts = { + position = "center", + hl = "Type", + }, +} + +local section_mru = { + 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 }, + }, + }, +} + +local buttons = { + type = "group", + val = { + { type = "text", val = "Quick links", opts = { hl = "SpecialComment", position = "center" } }, + { type = "padding", val = 1 }, + dashboard.button("e", " New file", "ene"), + dashboard.button("SPC f f", "󰈞 Find file"), + dashboard.button("SPC f g", "󰊄 Live grep"), + dashboard.button("SPC p", " Update plugins", "PlugUpdate"), + dashboard.button("c", " Configuration", "cd ~/.config/nvim/ "), + dashboard.button("q", "󰅚 Quit", "qa"), + }, + position = "center", +} + +local config = { + 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, + }, +} + +return { + header = header, + buttons = buttons, + mru = mru, + config = config, + -- theme specific config + mru_opts = mru_opts, + leader = dashboard.leader, + nvim_web_devicons = nvim_web_devicons, +} diff --git a/home-config/nvim/lua/plugin.lua b/home-config/nvim/lua/plugin.lua index bc3af68..335ba01 100644 --- a/home-config/nvim/lua/plugin.lua +++ b/home-config/nvim/lua/plugin.lua @@ -9,7 +9,6 @@ Plug('ryanoasis/vim-devicons') Plug('SirVer/ultisnips') Plug('honza/vim-snippets') Plug('preservim/nerdcommenter') -Plug('mhinz/vim-startify') Plug('neoclide/coc.nvim', { branch = 'release' }) Plug('nvim-telescope/telescope.nvim', { tag = '0.1.3' }) Plug('nvim-treesitter/nvim-treesitter', { run = ':TSUpdate' }) @@ -34,5 +33,6 @@ Plug('rcarriga/nvim-notify') Plug('folke/trouble.nvim') Plug('kylechui/nvim-surround') Plug('akinsho/toggleterm.nvim', {tag = '*'}) +Plug('goolord/alpha-nvim') vim.call('plug#end') diff --git a/home-config/nvim/lua/theme.lua b/home-config/nvim/lua/theme.lua index 5d58a18..7c7e0a7 100644 --- a/home-config/nvim/lua/theme.lua +++ b/home-config/nvim/lua/theme.lua @@ -53,3 +53,6 @@ require("fidget").setup { blend = 0, }, } + +local theme = require('menu') +require('alpha').setup(theme.config)