Skip to content

Commit

Permalink
Merge Dev (#6)
Browse files Browse the repository at this point in the history
* feat(utils): ✨started adding oldfiles

* fix(oldfiles): 🐛temporary fix for testing

* feat(settings): ✨started skeleton

* feat(themes): ✨added default

* feat(plugin): ✨new settings structure

* feat(buildingblocks): ✨added first headers

* feat(buildingblocks): ✨added first functions

* fix(utils): 🐛longest lines uses displaywidth now

* refactor(buildingblocks): ♻️ stylua and formatting

* feat(plugin): ✨more buildingblocks and some improvements

* cleanup(buildingblocks): 🗑️removed unused stuff and added new function

* feat(themes): ✨added startify

* fix(defaults): 🐛added oldfiles_amount

* feat(buildingblocks): ✨moved oldfiles to utils and new function

* feat(utils): ✨some fixes

* fix(theme): 🐛use default settings in startify

* docs(readme): 📚started adding Customization

* feat(gitignore): ✨added notes

* feat(sections): ✨started adding sections

* feat(themes): ✨added evil-startup

* fix(themes): 🐛added parts option to allow more parts

* feat(utils): ✨added function for oldfiles of directory

* cleanup(sections): 🗑️removed type table

* feat: ✨check for undefined titles and use table for paddings

* feat(themes): ✨added new section to evil

* fix(thems): 🐛updated paddings to new format

* feat(plugin): ✨some fixes and fold instead of section

* feat(utils): ✨added function to position cursor

* fix(themes): 🐛use fold instead of section and some new settings

* fix(utils): 🐛align oldfiles and remove debugging stuff

* fix(plugin): 🐛renamed fold to fold_section

* docs(readme): 📚added structure

* docs(readme): 📚added examples

* fix(readme): 🐛fixed codeblocks

* fix(readme): 🐛typo

* fix(readme): 🐛codeblocks

* cleanup(plugin): 🗑️removed duplicate functions

* fix(cursor_positioning): 🐛added timeout to prevent function from
triggering itself

* refactor(plugin): 🔄set lines in mapping names with empty and fix column
not being an int

* cleanup(functions): 🗑️removed duplicate function

* feat(themes): ✨added clock to evil

* docs(readme): 📚added more examples
  • Loading branch information
max397574 authored Oct 23, 2021
1 parent 3922302 commit 94dde12
Show file tree
Hide file tree
Showing 11 changed files with 1,231 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test
notes.md
326 changes: 251 additions & 75 deletions lua/startup.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,99 @@
local M = {}
local ns = vim.api.nvim_create_namespace "startup"
-- tables with tables: {line, align, virtual_text, move on}
M.lines = {}
M.formatted_text = {}
M.sections = {}
M.open_sections = {}

local limited_space = false
local section_alignments = {}

local current_section = ""

local opts = { noremap = true, silent = true }
local settings = require "startup.config"

local utils = require "startup.utils"
local spaces = utils.spaces

local function create_mappings()
function M.open_section()
vim.api.nvim_buf_set_option(0, "modifiable", true)
local line_nr = vim.api.nvim_win_get_cursor(0)[1]
local section_name = vim.trim(vim.api.nvim_get_current_line())
local section_align = section_alignments[section_name]
local section_entries = M.sections[section_name]
if section_name == "" then
return
end
local valid_section = false
for title, _ in pairs(M.sections) do
if section_name == title then
valid_section = true
end
end
if not valid_section then
return
end
section_entries = require("startup").align(section_entries, section_align)
for i, section in ipairs(M.open_sections) do
if section == section_name then
vim.api.nvim_buf_set_lines(
0,
line_nr,
line_nr + #section_entries,
false,
{}
)
table.remove(M.open_sections, i)
return
end
end
vim.api.nvim_buf_set_lines(0, line_nr, line_nr, false, section_entries)
table.insert(M.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) })
vim.api.nvim_buf_set_option(0, "modifiable", false)
end

local function create_mappings(mappings)
vim.api.nvim_buf_set_keymap(
0,
"n",
"<CR>",
":lua require'startup'.check_line()<CR>",
opts
)
for _, cmd in pairs(settings.tools) do
vim.api.nvim_buf_set_keymap(
0,
"n",
cmd[2],
"<cmd>" .. cmd[1] .. "<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
0,
"n",
"o",
"<cmd>lua require('startup').open_file()<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
0,
"n",
"<tab>",
"<cmd>lua require'startup'.open_section()<CR>",
opts
)
vim.api.nvim_buf_set_keymap(
0,
"n",
"<c-o>",
"<cmd>lua require('startup').open_file_vsplit()<CR>",
opts
)
if mappings ~= {} then
for _, cmd in pairs(mappings) do
vim.api.nvim_buf_set_keymap(
0,
"n",
cmd[2],
"<cmd>" .. cmd[1] .. "<CR>",
opts
)
end
end
end

Expand All @@ -33,112 +102,219 @@ function M.new_file()
vim.cmd("e " .. name)
end

local sections_with_mappings = {}

function M.check_line()
local line = vim.api.nvim_get_current_line()
for name, command in pairs(settings.tools) do
if line:match(name) then
vim.cmd(command[1])
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

local function align(dict)
local padding = 0
if settings.options.padding < 1 then
padding = vim.o.columns * padding
function M.open_file()
local line = vim.api.nvim_get_current_line()
local filename = line
print(filename)
vim.cmd("e " .. filename)
end

function M.open_file_vsplit()
local line = vim.api.nvim_get_current_line()
local filename = line
print(filename)
vim.cmd("vsplit " .. filename)
end
function M.align(dict, alignment)
local margin_calculated = 0
if settings[current_section].margin < 1 then
margin_calculated = vim.o.columns * settings[current_section].margin
else
padding = settings.options.padding
margin_calculated = settings[current_section].margin
end
local aligned = {}
local max_len = utils.longest_line(dict)
if settings.options.align == "center" then
if alignment == "center" then
local space_left = vim.o.columns - max_len
for _, line in ipairs(dict) do
table.insert(aligned, spaces(space_left / 2) .. line)
end
elseif settings.options.align == "left" then
elseif alignment == "left" then
for _, line in ipairs(dict) do
table.insert(aligned, spaces(settings.options.padding) .. line)
table.insert(aligned, spaces(margin_calculated) .. line)
end
elseif settings.options.align == "right" then
elseif alignment == "right" then
for _, line in ipairs(dict) do
table.insert(
aligned,
spaces(vim.o.columns - max_len - settings.options.padding - 10) .. line
spaces(vim.o.columns - max_len - margin_calculated - 10) .. line
)
end
end
margin_calculated = 0
return aligned
end

local count = 1
local function set_lines(len, text, hi, pass)
vim.api.nvim_buf_set_lines(0, count, count + len, false, align(text))
vim.api.nvim_win_set_cursor(0, { count, 0 })
if pass then
vim.g.section_length = count
end
for i = count, count + len do
vim.api.nvim_buf_add_highlight(0, ns, hi, i, 1, -1)
local function empty(amount)
for _ = 1, amount, 1 do
table.insert(M.lines, { " ", "center", true, "normal" })
end
count = count + len
end

local function empty()
set_lines(1, { " " }, "StartupTools")
end

local function body()
local toolnames = {}
for name, cmd in pairs(settings.tools) do
if not limited_space then
table.insert(toolnames, " ")
function M.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
if settings.options.mapping_names then
table.insert(toolnames, name .. " " .. cmd[2])
end
local length = utils.longest_line(strings) + 18
-- local length = vim.o.columns * 0.4
for name, cmd in pairs(mappings) do
if settings.options.empty_lines_between_mappings then
table.insert(mapnames, " ")
end
if settings.options.mapping_keys then
local space = utils.spaces(length - #cmd[2] - #name)
table.insert(mapnames, name .. space .. cmd[2])
else
table.insert(toolnames, name)
local space = utils.spaces(length - #name)
table.insert(mapnames, name .. space)
end
end

return toolnames
return mapnames
end

function M.display()
local rly_limited_space = false

if vim.o.lines < (#settings.header + (#settings.tools * 2) + 20) then
limited_space = true
end

create_mappings()
utils.create_hls()
-- vim.api.nvim_buf_set_keymap(0, "n", "j", "2j", opts)
-- vim.api.nvim_buf_set_keymap(0, "n", "k", "2k", opts)
if not limited_space then
empty()
end
set_lines(#settings.header, settings.header, "StartupHeading")

local toolnames = body()
empty()
set_lines(#toolnames, toolnames, "StartupTools")

vim.cmd [[silent! %s/\s\+$//]] -- clear trailing whitespace
U.set_buf_options()
if limited_space then
vim.api.nvim_win_set_cursor(
0,
{ #settings.header + 3, math.floor(vim.o.columns / 2) }
)
end
vim.schedule(function()
local padding_nr = 1
U.set_buf_options()
local parts = settings.parts
for _, part in ipairs(parts) do
empty(settings.options.paddings[padding_nr])
padding_nr = padding_nr + 1
current_section = part
local options = settings[part]
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
M.sections[vim.trim(options.title)] = options.content
table.insert(
M.lines,
{ options.title, options.align, false, options.highlight }
)
else
for _, line in ipairs(options.content) do
table.insert(
M.lines,
{ line, options.align, true, options.highlight }
)
end
end
elseif options.type == "mapping" then
if options.fold_section then
section_alignments[vim.trim(options.title)] = options.align
M.sections[vim.trim(options.title)] =
require("startup").mapping_names(
options.content
)
table.insert(
M.lines,
{ options.title, options.align, false, options.highlight }
)
else
for _, line in ipairs(require("startup").mapping_names(options.content)) do
table.insert(
M.lines,
{ line, options.align, false, options.highlight }
)
if settings.options.empty_lines_between_mappings then
empty(1)
end
end
end
table.insert(sections_with_mappings, part)
create_mappings(options.content)
elseif options.type == "oldfiles" then
local oldfiles = {}
if options.oldfiles_directory then
old_files = utils.get_oldfiles_directory(options.oldfiles_amount)
else
old_files = utils.get_oldfiles(options.oldfiles_amount)
end
if options.fold_section then
section_alignments[vim.trim(options.title)] = options.align
M.sections[vim.trim(options.title)] = old_files
table.insert(
M.lines,
{ options.title, options.align, false, options.highlight }
)
else
for _, line in ipairs(old_files) do
table.insert(
M.lines,
{ line, options.align, false, options.highlight }
)
end
end
end
create_mappings {}
vim.cmd(options.command)
end
-- current_section = ""
for _, line in ipairs(M.lines) do
table.insert(
M.formatted_text,
require("startup").align({ line[1] }, line[2])[1]
)
end
vim.api.nvim_buf_set_option(0, "modifiable", true)
vim.api.nvim_buf_set_lines(0, 0, -1, true, {})
vim.api.nvim_buf_set_lines(0, 0, -1, false, M.formatted_text)
vim.cmd [[silent! %s/\s\+$//]] -- clear trailing whitespace
for linenr, line in ipairs(M.lines) do
vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1)
end
vim.api.nvim_buf_set_option(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),
})
end)
vim.cmd [[autocmd CursorMoved * lua require"startup.utils".reposition_cursor()]]
end

function M.setup(update)
if vim.g.startup_nvim_loaded then
return
end
vim.g.startup_nvim_loaded = true

settings = vim.tbl_deep_extend("force", settings, update or {})
vim.cmd [[
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * lua if vim.fn.argc() == 0 and vim.fn.exists('std_in') then require"startup".display() end
autocmd VimEnter * lua if vim.fn.argc() == 0 then require"startup".display() end
]]
end

Expand Down
Loading

0 comments on commit 94dde12

Please sign in to comment.