aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormax397574 <[email protected]>2021-10-07 11:27:32 +0200
committermax397574 <[email protected]>2021-10-07 11:27:32 +0200
commita92dabd3e97a7bea6e393e6b5733613dcfdcc9c2 (patch)
tree282cef719391796f9df7969d0748e53ee23469ae
parent8c67e6444b26dbb77ad32f41e34e3bfdda2bedae (diff)
feat(plugin): ✨new settings structure
-rw-r--r--lua/startup.lua139
-rw-r--r--lua/startup/config.lua42
-rw-r--r--lua/startup/themes/default.lua70
-rw-r--r--lua/startup/utils.lua1
-rw-r--r--settings_skeleton.lua95
5 files changed, 206 insertions, 141 deletions
diff --git a/lua/startup.lua b/lua/startup.lua
index d895433..7c167b9 100644
--- a/lua/startup.lua
+++ b/lua/startup.lua
@@ -1,7 +1,7 @@
local M = {}
local ns = vim.api.nvim_create_namespace "startup"
-local limited_space = false
+local current_section = ""
local opts = { noremap = true, silent = true }
local settings = require "startup.config"
@@ -9,7 +9,7 @@ local settings = require "startup.config"
local utils = require "startup.utils"
local spaces = utils.spaces
-local function create_mappings()
+local function create_mappings(mappings)
vim.api.nvim_buf_set_keymap(
0,
"n",
@@ -17,7 +17,7 @@ local function create_mappings()
":lua require'startup'.check_line()<CR>",
opts
)
- for _, cmd in pairs(settings.tools) do
+ for _, cmd in pairs(mappings) do
vim.api.nvim_buf_set_keymap(
0,
"n",
@@ -33,47 +33,58 @@ 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
+local function align(dict, alignment)
+ local padding_calculated = 0
+ if settings[current_section].padding < 1 then
+ padding_calculated = vim.o.columns * settings[current_section].padding
else
- padding = settings.options.padding
+ padding_calculated = settings[current_section].padding
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(padding_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 - padding_calculated - 10) .. line
)
end
end
+ padding_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))
+local function set_lines(len, text, alignment, hi, pass)
+ vim.api.nvim_buf_set_lines(
+ 0,
+ count,
+ count + len,
+ false,
+ align(text, alignment)
+ )
vim.api.nvim_win_set_cursor(0, { count, 0 })
if pass then
vim.g.section_length = count
@@ -84,54 +95,76 @@ local function set_lines(len, text, hi, pass)
count = count + len
end
-local function empty()
- set_lines(1, { " " }, "StartupTools")
+local function empty(amount)
+ for _ = 1, amount, 1 do
+ set_lines(1, { " " }, "center", "StartupTools")
+ end
end
-local function body()
- local toolnames = {}
- for name, cmd in pairs(settings.tools) do
- if not limited_space then
- table.insert(toolnames, " ")
+local function mapping_names(mappings)
+ local mapnames = {}
+ for name, cmd in pairs(mappings) do
+ if settings.options.empty_lines_between_mappings then
+ table.insert(mapnames, " ")
end
- if settings.options.mapping_names then
- table.insert(toolnames, name .. " " .. cmd[2])
+ if settings.options.mapping_keys then
+ table.insert(mapnames, name .. " " .. cmd[2])
else
- table.insert(toolnames, name)
+ table.insert(mapnames, name)
end
end
- return toolnames
+ return mapnames
end
+-- TODO: put inside schedule()
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()
+ local parts = { "header", "body", "footer" }
+ for _, part in ipairs(parts) do
+ current_section = part
+ local options = settings[part]
+ 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
+ set_lines(
+ #options.content,
+ options.content,
+ options.align,
+ options.highlight
+ )
+ elseif options.type == "mapping" then
+ table.insert(sections_with_mappings, part)
+ create_mappings(options.content)
+ set_lines(
+ #mapping_names(options.content),
+ mapping_names(options.content),
+ options.align,
+ options.highlight
+ )
+ end
+ if part == "header" then
+ empty(settings.options.gap1)
+ elseif part == "body" then
+ empty(settings.options.gap2 + 1)
+ end
+ vim.cmd(options.command)
end
- set_lines(#settings.header, settings.header, "StartupHeading")
-
- local toolnames = body()
- empty()
- set_lines(#toolnames, toolnames, "StartupTools")
-
+ current_section = ""
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.api.nvim_win_set_cursor(0, {
+ #settings.header.content + settings.options.gap1 + 3,
+ math.floor(vim.o.columns / 2),
+ })
end
function M.setup(update)
diff --git a/lua/startup/config.lua b/lua/startup/config.lua
index 3a38f9c..8508de5 100644
--- a/lua/startup/config.lua
+++ b/lua/startup/config.lua
@@ -1,43 +1,3 @@
-local settings = {
- -- every line should be same width without escaped \
- -- header = {
- -- " /$$ ",
- -- " |__/ ",
- -- " /$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$$$$$/$$$$ ",
- -- "| $$__ $$ /$$__ $$ /$$__ $$| $$ /$$/| $$| $$_ $$_ $$",
- -- "| $$ \\ $$| $$$$$$$$| $$ \\ $$ \\ $$/$$/ | $$| $$ \\ $$ \\ $$",
- -- "| $$ | $$| $$_____/| $$ | $$ \\ $$$/ | $$| $$ | $$ | $$",
- -- "| $$ | $$| $$$$$$$| $$$$$$/ \\ $/ | $$| $$ | $$ | $$",
- -- "|__/ |__/ \\_______/ \\______/ \\_/ |__/|__/ |__/ |__/",
- -- },
- -- header = require("startup.utils").get_oldfiles(10),
- header = require("startup.utils").default_header(),
- -- name which will be displayed and command
- tools = {
- [" Find File"] = { "Telescope find_files", "<leader>ff" },
- [" Find Word"] = { "Telescope live_grep", "<leader>lg" },
- [" Recent Files"] = { "Telescope oldfiles", "<leader>of" },
- [" File Browser"] = { "Telescope file_browser", "<leader>fb" },
- [" Config Files"] = {
- 'lua require("telescope.builtin").find_files({cwd="~/.config"})',
- "<leader>cf",
- },
- [" Colorschemes"] = { "Telescope colorscheme", "<leader>cs" },
- [" New File"] = { "lua require'startup'.new_file()", "<leader>nf" },
- ["ﲉ Help Files"] = { "Telescope help_tags", "<leader>fh" },
- },
- options = {
- align = "center", -- center, left or right
- mapping_names = true,
- -- can be an integer (columns)
- -- float < 1 -> percentage of screen width
- padding = 5, -- only used if align left or right
- },
- colors = {
- background = "#1f2227",
- heading_fg = "#009900",
- tools_fg = "#009900",
- },
-}
+local settings = require "startup.themes.default"
return settings
diff --git a/lua/startup/themes/default.lua b/lua/startup/themes/default.lua
index 3db5eb5..3c7fc07 100644
--- a/lua/startup/themes/default.lua
+++ b/lua/startup/themes/default.lua
@@ -1,40 +1,58 @@
local settings = {
-- every line should be same width without escaped \
header = {
- " /$$ ",
- " |__/ ",
- " /$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$$$$$/$$$$ ",
- "| $$__ $$ /$$__ $$ /$$__ $$| $$ /$$/| $$| $$_ $$_ $$",
- "| $$ \\ $$| $$$$$$$$| $$ \\ $$ \\ $$/$$/ | $$| $$ \\ $$ \\ $$",
- "| $$ | $$| $$_____/| $$ | $$ \\ $$$/ | $$| $$ | $$ | $$",
- "| $$ | $$| $$$$$$$| $$$$$$/ \\ $/ | $$| $$ | $$ | $$",
- "|__/ |__/ \\_______/ \\______/ \\_/ |__/|__/ |__/ |__/",
+ type = "text",
+ align = "center",
+ padding = 5,
+ content = {
+ " /$$ ",
+ " |__/ ",
+ " /$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$$$$$/$$$$ ",
+ "| $$__ $$ /$$__ $$ /$$__ $$| $$ /$$/| $$| $$_ $$_ $$",
+ "| $$ \\ $$| $$$$$$$$| $$ \\ $$ \\ $$/$$/ | $$| $$ \\ $$ \\ $$",
+ "| $$ | $$| $$_____/| $$ | $$ \\ $$$/ | $$| $$ | $$ | $$",
+ "| $$ | $$| $$$$$$$| $$$$$$/ \\ $/ | $$| $$ | $$ | $$",
+ "|__/ |__/ \\_______/ \\______/ \\_/ |__/|__/ |__/ |__/",
+ },
+ highlight = "markdownH1",
+ default_color = "#FFFFFF",
+ command = "",
},
-- name which will be displayed and command
- tools = {
- [" Find File"] = { "Telescope find_files", "<leader>ff" },
- [" Find Word"] = { "Telescope live_grep", "<leader>lg" },
- [" Recent Files"] = { "Telescope oldfiles", "<leader>of" },
- [" File Browser"] = { "Telescope file_browser", "<leader>fb" },
- [" Config Files"] = {
- 'lua require("telescope.builtin").find_files({cwd="~/.config"})',
- "<leader>cf",
+ body = {
+ type = "mapping",
+ align = "center",
+ padding = 5,
+ content = {
+ [" Find File"] = { "Telescope find_files", "<leader>ff" },
+ [" Find Word"] = { "Telescope live_grep", "<leader>lg" },
+ [" Recent Files"] = { "Telescope oldfiles", "<leader>of" },
+ [" File Browser"] = { "Telescope file_browser", "<leader>fb" },
+ [" Colorschemes"] = { "Telescope colorscheme", "<leader>cs" },
+ [" New File"] = { "lua require'startup'.new_file()", "<leader>nf" },
},
- [" Colorschemes"] = { "Telescope colorscheme", "<leader>cs" },
- [" New File"] = { "lua require'startup'.new_file()", "<leader>nf" },
- ["ﲉ Help Files"] = { "Telescope help_tags", "<leader>fh" },
+ highlight = "TSString",
+ default_color = "#FFFFFF",
+ command = "",
+ },
+ footer = {
+ type = "text",
+ align = "center",
+ padding = 5,
+ content = { "startup.nvim" },
+ highlight = "TSString",
+ default_color = "#FFFFFF",
+ command = "",
},
+
options = {
- align = "center", -- center, left or right
- mapping_names = true,
- -- can be an integer (columns)
- -- float < 1 -> percentage of screen width
- padding = 5, -- only used if align left or right
+ mapping_keys = true,
+ empty_lines_between_mappings = true,
+ gap1 = 3,
+ gap2 = 4,
},
colors = {
background = "#1f2227",
- heading_fg = "#009900",
- tools_fg = "#009900",
},
}
return settings
diff --git a/lua/startup/utils.lua b/lua/startup/utils.lua
index 2bfe149..101e6b3 100644
--- a/lua/startup/utils.lua
+++ b/lua/startup/utils.lua
@@ -57,6 +57,7 @@ function U.set_buf_options()
vim.api.nvim_buf_set_option(0, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(0, "buftype", "nofile")
vim.api.nvim_buf_set_option(0, "filetype", "dashboard")
+ vim.api.nvim_buf_set_option(0, "modifiable", false)
vim.api.nvim_buf_set_option(0, "swapfile", false)
vim.cmd [[setlocal nonu nornu]]
end
diff --git a/settings_skeleton.lua b/settings_skeleton.lua
index cb0078d..7e1c9b5 100644
--- a/settings_skeleton.lua
+++ b/settings_skeleton.lua
@@ -1,7 +1,51 @@
--- theme template (kinda)
+local opts = { noremap = true, silent = true }
+local function example_settings()
+ vim.api.nvim_buf_set_keymap(0, "n", "x", "<cmd>lua print('test')<CR>", opts)
+ return { "entry_1", "entry_2" }
+end
+
+---[[
+local example_section = {
+ -- text: content is a table of string (directly or from function)
+ -- mapping: content is mapping in format from below
+ type = "text",
+ -- left: text on left side with padding
+ -- right: text on right side with padding
+ -- center: text will be in center
+ align = "left",
+ -- int: amount of columns,
+ -- float: fraction of screen
+ -- only if right or left align
+ padding = 5,
+ -- table: a table with strings
+ -- function: a call to a function which returns a table with strings and does some other things
+ -- mapping: ["<mapping name>"] = { "<command>", "<keys>"}
+ content = { "line 1", "line 2" }, --table or function or mapping
+ highlight = "example", -- highlight group
+ default_color = "#FFFFFF", -- hex color code
+ command = "echo 'test worked'", -- a command which will be exected when section gets loaded
+}
+local settings = {
+ header = {},
+ body = {},
+ footer = {},
+ options = {
+ mapping_keys = true, -- display keys for mappings (e.g. <leader>ff)
+ empty_lines_between_mappings = true, -- empty lines between mapping names
+ gap1 = 3, -- space between header and body -1
+ gap2 = 4, -- space between body and footer -1
+ },
+ colors = {
+ background = "#1f2227", -- the default background color
+ },
+}
+-- ]]--
local settings = {
+ -- every line should be same width without escaped \
header = {
type = "text",
+ align = "center",
+ padding = 5,
content = {
" /$$ ",
" |__/ ",
@@ -12,34 +56,43 @@ local settings = {
"| $$ | $$| $$$$$$$| $$$$$$/ \\ $/ | $$| $$ | $$ | $$",
"|__/ |__/ \\_______/ \\______/ \\_/ |__/|__/ |__/ |__/",
},
+ highlight = "TSString",
+ default_color = "#009900",
+ command = "echo 'header works'",
},
+ -- name which will be displayed and command
body = {
+ type = "mapping",
+ align = "left",
+ padding = 0.3,
content = {
[" Find File"] = { "Telescope find_files", "<leader>ff" },
[" Find Word"] = { "Telescope live_grep", "<leader>lg" },
[" Recent Files"] = { "Telescope oldfiles", "<leader>of" },
- [" File Browser"] = { "Telescope file_browser", "<leader>fb" },
- [" Config Files"] = {
- 'lua require("telescope.builtin").find_files({cwd="~/.config"})',
- "<leader>cf",
- },
- [" Colorschemes"] = { "Telescope colorscheme", "<leader>cs" },
[" New File"] = { "lua require'startup'.new_file()", "<leader>nf" },
- ["ﲉ Help Files"] = { "Telescope help_tags", "<leader>fh" },
},
+ highlight = "CoolHighlight",
+ default_color = "",
+ command = "highlight CoolHighlight guifg=#FF0000",
+ },
+ footer = {
+ type = "text",
+ align = "right",
+ padding = 15,
+ content = example_settings(),
+ highlight = "",
+ default_color = "#990000",
+ command = "",
+ },
+
+ options = {
+ mapping_keys = true, -- display keys for mappings (e.g. <leader>ff)
+ empty_lines_between_mappings = true, -- empty lines between mapping names
+ gap1 = 3, -- space between header and body -1
+ gap2 = 4, -- space between body and footer -1
+ },
+ colors = {
+ background = "#1f2227",
},
- footer = {},
}
return settings
-
---[[
-type can be text or mapping
-
-text -> table with strings in it, can also be from a function
-in function additional stuff can be done/defined
-mapping -> ["title that will be displayed"] = { "command", "keys"}
-e.g. [" Find File"] = { "Telescope find_files", "<leader>ff" },
-symbols can be found at
-https://www.nerdfonts.com/cheat-sheet
-]]
---