-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathstartify.lua
265 lines (246 loc) · 7.52 KB
/
startify.lua
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
local utils = require("alpha.utils")
local if_nil = vim.F.if_nil
local fnamemodify = vim.fn.fnamemodify
local filereadable = vim.fn.filereadable
local default_header = {
type = "text",
val = {
[[ __]],
[[ ___ ___ ___ __ __ /\_\ ___ ___]],
[[ / _ `\ / __`\ / __`\/\ \/\ \\/\ \ / __` __`\]],
[[ /\ \/\ \/\ __//\ \_\ \ \ \_/ |\ \ \/\ \/\ \/\ \]],
[[ \ \_\ \_\ \____\ \____/\ \___/ \ \_\ \_\ \_\ \_\]],
[[ \/_/\/_/\/____/\/___/ \/__/ \/_/\/_/\/_/\/_/]],
},
opts = {
hl = "Type",
shrink_margin = false,
-- wrap = "overflow";
},
}
local leader = "SPC"
--- @param sc string
--- @param txt string
--- @param keybind string? optional
--- @param keybind_opts table? optional
local function button(sc, txt, keybind, keybind_opts)
local sc_ = sc:gsub("%s", ""):gsub(leader, "<leader>")
local opts = {
position = "left",
shortcut = "[" .. sc .. "] ",
cursor = 1,
-- width = 50,
align_shortcut = "left",
hl_shortcut = { { "Operator", 0, 1 }, { "Number", 1, #sc + 1 }, { "Operator", #sc + 1, #sc + 2 } },
shrink_margin = false,
}
if keybind then
keybind_opts = if_nil(keybind_opts, { noremap = true, silent = true, nowait = true })
opts.keymap = { "n", sc_, keybind, keybind_opts }
end
local function on_press()
local key = vim.api.nvim_replace_termcodes(keybind .. "<Ignore>", true, false, true)
vim.api.nvim_feedkeys(key, "t", false)
end
return {
type = "button",
val = txt,
on_press = on_press,
opts = opts,
}
end
local file_icons = {
enabled = true,
highlight = true,
-- available: devicons, mini, to use nvim-web-devicons or mini.icons
-- if provider not loaded and enabled is true, it will try to use another provider
provider = "mini",
}
local function icon(fn)
if file_icons.provider ~= "devicons" and file_icons.provider ~= "mini" then
vim.notify("Alpha: Invalid file icons provider: " .. file_icons.provider .. ", disable file icons", vim.log.levels.WARN)
file_icons.enabled = false
return "", ""
end
local ico, hl = utils.get_file_icon(file_icons.provider, fn)
if ico == "" then
file_icons.enabled = false
vim.notify("Alpha: Mini icons or devicons get icon failed, disable file icons", vim.log.levels.WARN)
end
return ico, hl
end
local function file_button(fn, sc, short_fn, autocd)
short_fn = if_nil(short_fn, fn)
local ico_txt
local fb_hl = {}
if file_icons.enabled then
local ico, hl = icon(fn)
local hl_option_type = type(file_icons.highlight)
if hl_option_type == "boolean" then
if hl and file_icons.highlight then
table.insert(fb_hl, { hl, 0, #ico })
end
end
if hl_option_type == "string" then
table.insert(fb_hl, { file_icons.highlight, 0, #ico })
end
ico_txt = ico .. " "
else
ico_txt = ""
end
local cd_cmd = (autocd and " | cd %:p:h" or "")
local file_button_el = 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, #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, utils.get_extension(v))) or false
if (filereadable(v) == 1) and cwd_cond and not ignore then
oldfiles[#oldfiles + 1] = v
end
end
local tbl = {}
for i, fn in ipairs(oldfiles) do
local short_fn
if cwd then
short_fn = fnamemodify(fn, ":.")
else
short_fn = fnamemodify(fn, ":~")
end
local file_button_el = file_button(fn, tostring(i + start - 1), short_fn, opts.autocd)
tbl[i] = file_button_el
end
return {
type = "group",
val = tbl,
opts = {},
}
end
local function mru_title()
return "MRU " .. vim.fn.getcwd()
end
local section = {
header = default_header,
top_buttons = {
type = "group",
val = {
button("e", "New file", "<cmd>ene <CR>"),
},
},
-- note about MRU: currently this is a function,
-- since that means we can get a fresh mru
-- whenever there is a DirChanged. this is *really*
-- inefficient on redraws, since mru does a lot of I/O.
-- should probably be cached, or maybe figure out a way
-- to make it a reference to something mutable
-- and only mutate that thing on DirChanged
mru = {
type = "group",
val = {
{ type = "padding", val = 1 },
{ type = "text", val = "MRU", opts = { hl = "SpecialComment" } },
{ type = "padding", val = 1 },
{
type = "group",
val = function()
return { mru(10) }
end,
},
},
},
mru_cwd = {
type = "group",
val = {
{ type = "padding", val = 1 },
{ type = "text", val = mru_title, opts = { hl = "SpecialComment", shrink_margin = false } },
{ type = "padding", val = 1 },
{
type = "group",
val = function()
return { mru(0, vim.fn.getcwd()) }
end,
opts = { shrink_margin = false },
},
},
},
bottom_buttons = {
type = "group",
val = {
button("q", "Quit", "<cmd>q <CR>"),
},
},
footer = {
type = "group",
val = {},
},
}
local config = {
layout = {
{ type = "padding", val = 1 },
section.header,
{ type = "padding", val = 2 },
section.top_buttons,
section.mru_cwd,
section.mru,
{ type = "padding", val = 1 },
section.bottom_buttons,
section.footer,
},
opts = {
margin = 3,
redraw_on_resize = false,
setup = function()
vim.api.nvim_create_autocmd('DirChanged', {
pattern = '*',
group = "alpha_temp",
callback = function ()
require('alpha').redraw()
vim.cmd('AlphaRemap')
end,
})
end,
},
}
return {
icon = icon,
button = button,
file_button = file_button,
mru = mru,
mru_opts = mru_opts,
section = section,
config = config,
-- theme config
file_icons = file_icons,
-- deprecated
nvim_web_devicons = file_icons,
leader = leader,
-- deprecated
opts = config,
}