summaryrefslogtreecommitdiffstatshomepage
path: root/nvim/.config/nvim/lua/tobyvin/plugins/alpha.lua
blob: 569f3ceefc090d653ddf6565adf31f1ae800463f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
local Icons = require("nvim-web-devicons")
local utils = require("tobyvin.utils")

local file_button = function(filename, sc)
	local short_fn = utils.fs.shorten_path(filename, 60)
	local hl = {}

	local filetype, _ = vim.filetype.match({ filename = filename })
	filetype = vim.F.if_nil(filetype, "")
	local ico, ico_hl = Icons.get_icon_by_filetype(filetype, { default = true })
	table.insert(hl, { ico_hl, 0, 3 })
	local ico_txt = ico .. "  "
	local fn_start = short_fn:match(".*[/\\]")
	if fn_start ~= nil then
		table.insert(hl, { "Comment", #ico_txt - 2, #fn_start + #ico_txt })
	end

	local keybind = "<Cmd>e " .. filename .. " <CR>"
	local button = {
		type = "button",
		val = ico_txt .. short_fn,
		on_press = function()
			local key = vim.api.nvim_replace_termcodes(keybind .. "<Ignore>", true, false, true)
			vim.api.nvim_feedkeys(key, "t", false)
		end,
		opts = {
			position = "center",
			shortcut = "[" .. sc .. "]",
			cursor = 60,
			width = 60,
			align_shortcut = "right",
			hl = hl,
			hl_shortcut = { { "Special", 0, 1 }, { "Number", 1, #sc + 1 }, { "Special", #sc + 1, #sc + 2 } },
			shrink_margin = false,
			keymap = { "n", sc:gsub("%s", ""), keybind, { desc = "oldfile_" .. sc } },
		},
	}
	return button
end

local mru_filter = function(filename)
	local ignored_ft = { "gitcommit" }
	local cwd = vim.fn.getcwd()
	local filetype, _ = vim.filetype.match({ filename = filename })
	filetype = vim.F.if_nil(filetype, "")
	local ignored = false
	for _, pattern in pairs(ignored_ft) do
		ignored = ignored or filetype:match(pattern) ~= nil
	end
	return not ignored and (vim.fn.filereadable(filename) == 1) and vim.startswith(filename, cwd)
end

local mru_cache = nil
local get_mru = function()
	if mru_cache == nil then
		local oldfiles = { unpack(vim.tbl_filter(mru_filter, vim.v.oldfiles), 1, 20) }
		local tbl = {}
		for i, filename in ipairs(oldfiles) do
			tbl[i] = file_button(filename, tostring(i % 10))
		end
		mru_cache = { {
			type = "group",
			val = tbl,
		} }
	end
	return mru_cache
end

local status_ok, alpha = pcall(require, "alpha")
if not status_ok then
	vim.notify("Failed to load module 'alpha'", vim.log.levels.ERROR)
	return
end

alpha.keymaps_element.button = function(el, _, state)
	if el.opts and el.opts.keymap then
		if type(el.opts.keymap[1]) == "table" then
			for _, map in el.opts.keymap do
				map[4].buffer = state.buffer
				vim.keymap.set(unpack(map))
			end
		else
			local map = el.opts.keymap
			map[4].buffer = state.buffer
			vim.keymap.set(unpack(map))
		end
	end
end

local fortune = require("alpha.fortune")

local function info_value()
	local total_plugins = #vim.tbl_keys(packer_plugins)
	local v = vim.F.if_nil(vim.version(), {})
	return string.format("VIM: v%d.%d.%d PLUGINS: %d", v.major, v.minor, v.patch, total_plugins)
end

local config = {
	layout = {
		{
			type = "text",
			val = {
				"                                                    ",
				" ███╗   ██╗███████╗ ██████╗ ██╗   ██╗██╗███╗   ███╗ ",
				" ████╗  ██║██╔════╝██╔═══██╗██║   ██║██║████╗ ████║ ",
				" ██╔██╗ ██║█████╗  ██║   ██║██║   ██║██║██╔████╔██║ ",
				" ██║╚██╗██║██╔══╝  ██║   ██║╚██╗ ██╔╝██║██║╚██╔╝██║ ",
				" ██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ██║██║ ╚═╝ ██║ ",
				" ╚═╝  ╚═══╝╚══════╝ ╚═════╝   ╚═══╝  ╚═╝╚═╝     ╚═╝ ",
			},
			opts = {
				position = "center",
				hl = "DevIconVim",
			},
		},
		{
			type = "text",
			val = info_value(),
			opts = {
				hl = "DevIconVim",
				position = "center",
			},
		},
		{
			type = "text",
			val = fortune({ max_width = 60 }),
			opts = {
				position = "center",
				hl = "Statement",
			},
		},
		{ type = "padding", val = 1 },
		{
			type = "group",
			val = get_mru,
		},
	},
}

alpha.setup(config)

vim.api.nvim_create_autocmd("User", {
	group = vim.api.nvim_create_augroup("alpha_user", { clear = true }),
	pattern = "BDeleteLast",
	callback = function(args)
		local bufnr = vim.F.if_nil(args.data.buf, args.buf)
		if vim.api.nvim_buf_get_option(bufnr, "filetype") ~= "alpha" then
			alpha.start(false)
		end
	end,
	desc = "Run Alpha when last buffer closed",
})