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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
local startup = {}
local ns = vim.api.nvim_create_namespace("startup")
-- tables with tables: {line, align, cursor should move on, highlight}
startup.lines = {}
startup.formatted_text = {}
startup.sections = {}
startup.section_highlights = {}
startup.open_sections = {}
startup.good_lines = {}
startup.settings = require("startup.themes.dashboard")
local get_cur_line = vim.api.nvim_get_current_line
---set option in buffer
local set_buf_opt = vim.api.nvim_buf_set_option
local section_alignments = {}
local sections_with_mappings = {}
local startup_nvim_displayed
local startup_nvim_loaded
local current_section = ""
local opts = { noremap = true, silent = true }
local settings = require("startup.config")
---@type startup.utils
local utils = require("startup.utils")
---creates a mapping for the current buffer
---@param mapping string the mapping to use
---@param command string the command to be mapped
local buf_map = function(mapping, command)
vim.api.nvim_buf_set_keymap(0, "n", mapping, command, opts)
end
---open fold under cursor
function startup.open_section()
set_buf_opt(0, "modifiable", true)
local line_nr = vim.api.nvim_win_get_cursor(0)[1]
local section_name = vim.trim(get_cur_line())
local section_align = section_alignments[section_name]
local section_highlight = startup.section_highlights[section_name]
local section_entries = startup.sections[section_name]
if section_entries == nil or section_name == "" then
return
end
section_entries = startup.align(section_entries, section_align)
for i, section in ipairs(startup.open_sections) do
if section == section_name then
vim.api.nvim_buf_set_lines(
0,
line_nr,
line_nr + #section_entries,
false,
{}
)
table.remove(startup.open_sections, i)
return
end
end
vim.api.nvim_buf_set_lines(0, line_nr, line_nr, false, section_entries)
for number, _ in ipairs(section_entries) do
vim.api.nvim_buf_add_highlight(
0,
ns,
section_highlight,
line_nr + number - 1,
0,
-1
)
end
table.insert(startup.open_sections, section_name)
vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace
vim.api.nvim_win_set_cursor(0, { line_nr, math.floor(vim.o.columns / 2) })
set_buf_opt(0, "modifiable", false)
end
local function create_mappings(mappings)
buf_map(
settings.mappings.execute_command,
":lua require'startup'.check_line()<CR>"
)
buf_map(
settings.mappings.open_file,
"<cmd>lua require('startup').open_file()<CR>"
)
buf_map(
settings.mappings.open_section,
"<cmd>lua require'startup'.open_section()<CR>"
)
buf_map(
settings.mappings.open_file_split,
"<cmd>lua require('startup').open_file_vsplit()<CR>"
)
buf_map(
settings.mappings.open_help,
"<cmd>lua require'startup.utils'.key_help()<CR>"
)
if mappings ~= {} then
for _, cmd in pairs(mappings) do
buf_map(cmd[2], "<cmd>" .. cmd[1] .. "<CR>")
end
end
end
---ask for a filename and create file
function startup.new_file()
local name = vim.fn.input("Filename: > ")
vim.cmd("e " .. name)
end
---check if current line is one of the commands
function startup.check_line()
local line = get_cur_line()
for _, section in ipairs(sections_with_mappings) do
for name, command in pairs(settings[section].content) do
if line:match(name) then
vim.cmd(command[1])
end
end
end
end
---open file under cursor
function startup.open_file()
local filename = get_cur_line()
vim.cmd("e " .. filename)
end
---open file under cursor in split
function startup.open_file_vsplit()
local filename = get_cur_line()
vim.cmd("vsplit " .. filename)
end
---creates a table with the strings in it aligned
---@param dict table the strings to be aligned
---@param alignment string the alignement(left,center or right)
---@return table aligned the aligned strings
function startup.align(dict, alignment)
local margin_calculated = 0
local margin = settings.margin and type(settings.margin) == "number" or 5
if margin == 0 then
margin_calculated = 0
elseif margin < 1 then
margin_calculated = vim.o.columns * margin
else
margin_calculated = margin
end
local aligned = {}
local max_len = utils.longest_line(dict)
if alignment == "center" then
local space_left = vim.o.columns - max_len
for _, line in ipairs(dict) do
table.insert(aligned, utils.spaces(space_left / 2) .. line)
end
elseif alignment == "left" then
for _, line in ipairs(dict) do
table.insert(aligned, utils.spaces(margin_calculated) .. line)
end
elseif alignment == "right" then
for _, line in ipairs(dict) do
table.insert(
aligned,
utils.spaces(vim.o.columns - max_len - margin_calculated - 10) .. line
)
end
end
margin_calculated = 0
return aligned
end
---creates mapping names from table of mappings
---@param mappings table
---@return table
function startup.mapping_names(mappings)
local mapnames = {}
local strings = {}
for title, command in pairs(mappings) do
if settings.options.mapping_keys then
table.insert(strings, title .. command[2])
else
table.insert(strings, title)
end
end
local length = utils.longest_line(strings) + 18
for name, cmd in pairs(mappings) do
if settings.options.mapping_keys then
local space = utils.spaces(length - #cmd[2] - #name)
table.insert(mapnames, name .. space .. cmd[2])
else
local space = utils.spaces(length - #name)
table.insert(mapnames, name .. space)
end
end
return mapnames
end
function startup.display()
if startup_nvim_displayed then
return
end
startup_nvim_displayed = true
local padding_nr = 1
utils.set_buf_options()
local parts = settings.parts
vim.cmd([[hi link StartupFoldedSection Special]])
for _, part in ipairs(parts) do
utils.empty(settings.options.paddings[padding_nr])
padding_nr = padding_nr + 1
current_section = part
local options = settings[part]
utils.validate_settings(options)
if type(options.content) == "function" then
options.content = options.content()
end
if options.highlight == "" then
vim.cmd(
"highlight Startup"
.. part
.. " guifg="
.. options.default_color
.. " guibg="
.. settings.colors.background
)
options.highlight = "Startup" .. part
end
if options.type == "text" then
if options.fold_section then
section_alignments[vim.trim(options.title)] = options.align
startup.sections[vim.trim(options.title)] = options.content
startup.section_highlights[vim.trim(options.title)] = options.highlight
startup.good_lines[#startup.good_lines + 1] = vim.trim(options.title)
table.insert(
startup.lines,
{ options.title, options.align, true, "StartupFoldedSection" }
)
else
for _, line in ipairs(options.content) do
table.insert(
startup.lines,
{ line, options.align, false, options.highlight }
)
end
end
elseif options.type == "mapping" then
if options.fold_section then
section_alignments[vim.trim(options.title)] = options.align
for _, line in ipairs(startup.mapping_names(options.content)) do
startup.good_lines[#startup.good_lines + 1] = vim.trim(line)
end
startup.sections[vim.trim(options.title)] =
startup.mapping_names(
options.content
)
startup.section_highlights[vim.trim(options.title)] = options.highlight
startup.good_lines[#startup.good_lines + 1] = vim.trim(options.title)
table.insert(
startup.lines,
{ options.title, options.align, true, "StartupFoldedSection" }
)
for _, line in ipairs(options.content) do
startup.good_lines[#startup.good_lines + 1] = vim.trim(line)
end
else
for _, line in ipairs(startup.mapping_names(options.content)) do
startup.good_lines[#startup.good_lines + 1] = vim.trim(line)
table.insert(
startup.lines,
{ line, options.align, true, options.highlight }
)
if settings.options.empty_lines_between_mappings then
utils.empty(1)
end
end
end
table.insert(sections_with_mappings, part)
create_mappings(options.content)
elseif options.type == "oldfiles" then
local old_files
if options.oldfiles_directory then
old_files = utils.get_oldfiles_directory(options.oldfiles_amount or 5)
else
old_files = utils.get_oldfiles(options.oldfiles_amount or 5)
end
if options.fold_section then
section_alignments[vim.trim(options.title)] = options.align
startup.sections[vim.trim(options.title)] = old_files
startup.section_highlights[vim.trim(options.title)] = options.highlight
startup.good_lines[#startup.good_lines + 1] = vim.trim(options.title)
for _, line in ipairs(old_files) do
startup.good_lines[#startup.good_lines + 1] = vim.trim(line)
end
table.insert(
startup.lines,
{ options.title, options.align, true, "StartupFoldedSection" }
)
else
for _, line in ipairs(old_files) do
startup.good_lines[#startup.good_lines + 1] = vim.trim(line)
table.insert(
startup.lines,
{ line, options.align, true, options.highlight }
)
end
end
end
create_mappings({})
end
if settings.folded_section_color ~= "" then
vim.cmd(
[[highlight StartupFoldedSection guifg=]]
.. settings.colors.folded_section
)
end
for _, line in ipairs(startup.lines) do
table.insert(
startup.formatted_text,
startup.align({ line[1] }, line[2])[1]
)
end
set_buf_opt(0, "modifiable", true)
vim.api.nvim_buf_set_lines(0, 0, -1, true, {})
vim.api.nvim_buf_set_lines(0, 0, -1, false, startup.formatted_text)
vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace
for linenr, line in ipairs(startup.lines) do
vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1)
end
if settings.options.after and settings.options.after ~= "" then
settings.options.after()
end
set_buf_opt(0, "modifiable", false)
vim.api.nvim_win_set_cursor(0, { 1, 1 })
vim.api.nvim_win_set_cursor(0, {
#settings.header.content + settings.options.paddings[1] + 1,
math.floor(vim.o.columns / 2),
})
vim.cmd(
[[autocmd CursorMoved * lua require"startup.utils".reposition_cursor()]]
)
end
---Create autocmds for startup.nvim and update settings with update
---@param update table the settings to use
function startup.setup(update)
if startup_nvim_loaded then
return
end
startup_nvim_loaded = true
settings = vim.tbl_deep_extend("force", settings, update or {})
startup.settings = settings
vim.cmd("command! Startup :lua require('startup').display()")
vim.cmd(
[[autocmd VimEnter * lua if vim.fn.argc() == 0 then require("startup").display() end]],
[[autocmd BufRead * lua if vim.fn.argc() == 0 then require("startup").display() end]]
)
vim.cmd(
[[autocmd VimResized * lua if vim.bo.ft == "startup" then require"startup".redraw() end]]
)
end
---Clears the screen and redraws the whole startup screen
function startup.redraw()
startup.formatted_text = {}
for _, line in ipairs(startup.lines) do
table.insert(
startup.formatted_text,
startup.align({ line[1] }, line[2])[1]
)
end
set_buf_opt(0, "modifiable", true)
vim.api.nvim_buf_set_lines(0, 0, -1, true, {})
vim.api.nvim_buf_set_lines(0, 0, -1, false, startup.formatted_text)
vim.cmd([[silent! %s/\s\+$//]]) -- clear trailing whitespace
for linenr, line in ipairs(startup.lines) do
vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1)
end
set_buf_opt(0, "modifiable", false)
end
return startup
|