From bada866032ea4a79e2ea757864bc310873194f14 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 10 Jan 2023 19:59:41 -0600 Subject: refactor(nvim): rename start to dashboard --- nvim/.config/nvim/lua/tobyvin.lua | 2 +- nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua | 175 ++++++++++++++++++++++ nvim/.config/nvim/lua/tobyvin/utils/start.lua | 175 ---------------------- 3 files changed, 176 insertions(+), 176 deletions(-) create mode 100644 nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua delete mode 100644 nvim/.config/nvim/lua/tobyvin/utils/start.lua diff --git a/nvim/.config/nvim/lua/tobyvin.lua b/nvim/.config/nvim/lua/tobyvin.lua index bd755bc..2e625fd 100644 --- a/nvim/.config/nvim/lua/tobyvin.lua +++ b/nvim/.config/nvim/lua/tobyvin.lua @@ -1,7 +1,7 @@ require("tobyvin.options") require("tobyvin.keymaps") require("tobyvin.lazy") -require("tobyvin.utils.start") +require("tobyvin.utils.dashboard") vim.api.nvim_create_autocmd("User", { pattern = "VeryLazy", diff --git a/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua b/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua new file mode 100644 index 0000000..a954886 --- /dev/null +++ b/nvim/.config/nvim/lua/tobyvin/utils/dashboard.lua @@ -0,0 +1,175 @@ +---@type string[][] +local dashboard = {} + +---@type table +local _index = {} + +---@type string[] +local sections = setmetatable({}, { + __index = function(_, k) + return dashboard[_index[k]] + end, + + __newindex = function(_, k, v) + if _index[k] then + dashboard[_index[k]] = v + else + table.insert(dashboard, v) + _index[k] = #dashboard + end + vim.api.nvim_exec_autocmds("User", { pattern = "DashboardUpdate" }) + end, +}) + +local function should_skip() + -- don't start when opening a file + if vim.fn.argc() > 0 then + return true + end + + -- skip stdin + if vim.fn.line2byte(vim.fn.line("$")) ~= -1 then + return true + end + + -- Handle nvim -M + if not vim.o.modifiable then + return true + end + + for _, arg in pairs(vim.v.argv) do + -- whitelisted arguments + -- always open + if arg == "--startuptime" then + return false + end + + -- blacklisted arguments + -- always skip + if + arg == "-b" + -- commands, typically used for scripting + or arg == "-c" + or vim.startswith(arg, "+") + or arg == "-S" + then + return true + end + end + + -- base case: don't skip + return false +end + +local function max_len(lines) + local max = 0 + for _, line in ipairs(lines) do + max = math.max(max, vim.fn.strdisplaywidth(line)) + end + return max +end + +local function pad_lines(lines) + local max_line_len = max_len(lines) + local width = vim.api.nvim_win_get_width(0) + local padded = {} + for _, line in ipairs(lines) do + local line_len = max_line_len + local pad_len = math.floor((width / 2) - (line_len / 2)) + table.insert(padded, string.rep(" ", pad_len) .. line) + end + return padded +end + +local function render(buf) + local rendered = {} + for _, lines in pairs(dashboard) do + vim.list_extend(rendered, pad_lines(lines)) + end + vim.bo[buf].modifiable = true + vim.api.nvim_buf_set_lines(buf, 0, -1, false, rendered) + vim.bo[buf].modifiable = false +end + +if should_skip() then + return +end + +local buf = vim.api.nvim_create_buf(false, true) + +vim.api.nvim_set_current_buf(buf) + +vim.opt_local.textwidth = 0 +vim.opt_local.bufhidden = "wipe" +vim.opt_local.buflisted = false +vim.opt_local.matchpairs = "" +vim.opt_local.swapfile = false +vim.opt_local.buftype = "nofile" +vim.opt_local.filetype = "dashboard" +vim.opt_local.synmaxcol = 0 +vim.opt_local.wrap = false +vim.opt_local.colorcolumn = "" +vim.opt_local.foldlevel = 999 +vim.opt_local.foldcolumn = "0" +vim.opt_local.cursorcolumn = false +vim.opt_local.cursorline = false +vim.opt_local.number = false +vim.opt_local.relativenumber = false +vim.opt_local.list = false +vim.opt_local.spell = false +vim.opt_local.signcolumn = "no" + +local augroup = vim.api.nvim_create_augroup("dashboard", { clear = true }) + +vim.api.nvim_create_autocmd("User", { + group = augroup, + pattern = { "DashboardUpdate" }, + callback = function() + if vim.api.nvim_get_current_buf() == buf then + render(buf) + end + end, + desc = "render dashboard on updates", +}) + +vim.api.nvim_create_autocmd("BufHidden", { + group = augroup, + pattern = { "*" }, + callback = function(args) + if args.buf == buf then + vim.api.nvim_del_autocmd(augroup) + return true + end + end, + desc = "clear dashboard autocmds", +}) + +sections.header = { + " ", + " ███╗ ██╗███████╗ ██████╗ ██╗ ██╗██╗███╗ ███╗ ", + " ████╗ ██║██╔════╝██╔═══██╗██║ ██║██║████╗ ████║ ", + " ██╔██╗ ██║█████╗ ██║ ██║██║ ██║██║██╔████╔██║ ", + " ██║╚██╗██║██╔══╝ ██║ ██║╚██╗ ██╔╝██║██║╚██╔╝██║ ", + " ██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ██║██║ ╚═╝ ██║ ", + " ╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═══╝ ╚═╝╚═╝ ╚═╝ ", + " ", +} + +vim.api.nvim_create_autocmd("User", { + group = augroup, + pattern = { "Lazy*" }, + callback = function() + local updates = nil + if require("lazy.status").has_updates() then + updates = string.format("updates: %s", require("lazy.status").updates()) + end + + local stats = require("lazy").stats() + sections.lazy = { + string.format("startup: %s ms", stats.startuptime), + string.format("plugins: %s (%s loaded)", stats.count, stats.loaded), + updates, + } + end, + desc = "dashboard lazy stats", +}) diff --git a/nvim/.config/nvim/lua/tobyvin/utils/start.lua b/nvim/.config/nvim/lua/tobyvin/utils/start.lua deleted file mode 100644 index e012bec..0000000 --- a/nvim/.config/nvim/lua/tobyvin/utils/start.lua +++ /dev/null @@ -1,175 +0,0 @@ ----@type string[][] -local dashboard = {} - ----@type table -local _index = {} - ----@type string[] -local sections = setmetatable({}, { - __index = function(_, k) - return dashboard[_index[k]] - end, - - __newindex = function(_, k, v) - if _index[k] then - dashboard[_index[k]] = v - else - table.insert(dashboard, v) - _index[k] = #dashboard - end - vim.api.nvim_exec_autocmds("User", { pattern = "DashboardUpdate" }) - end, -}) - -local function should_skip() - -- don't start when opening a file - if vim.fn.argc() > 0 then - return true - end - - -- skip stdin - if vim.fn.line2byte(vim.fn.line("$")) ~= -1 then - return true - end - - -- Handle nvim -M - if not vim.o.modifiable then - return true - end - - for _, arg in pairs(vim.v.argv) do - -- whitelisted arguments - -- always open - if arg == "--startuptime" then - return false - end - - -- blacklisted arguments - -- always skip - if - arg == "-b" - -- commands, typically used for scripting - or arg == "-c" - or vim.startswith(arg, "+") - or arg == "-S" - then - return true - end - end - - -- base case: don't skip - return false -end - -local function max_len(lines) - local max = 0 - for _, line in ipairs(lines) do - max = math.max(max, vim.fn.strdisplaywidth(line)) - end - return max -end - -local function pad_lines(lines) - local max_line_len = max_len(lines) - local width = vim.api.nvim_win_get_width(0) - local padded = {} - for _, line in ipairs(lines) do - local line_len = max_line_len - local pad_len = math.floor((width / 2) - (line_len / 2)) - table.insert(padded, string.rep(" ", pad_len) .. line) - end - return padded -end - -local function render(buf) - local rendered = {} - for _, lines in pairs(dashboard) do - vim.list_extend(rendered, pad_lines(lines)) - end - vim.bo[buf].modifiable = true - vim.api.nvim_buf_set_lines(buf, 0, -1, false, rendered) - vim.bo[buf].modifiable = false -end - -if should_skip() then - return -end - -local buf = vim.api.nvim_create_buf(false, true) - -vim.api.nvim_set_current_buf(buf) - -vim.opt_local.textwidth = 0 -vim.opt_local.bufhidden = "wipe" -vim.opt_local.buflisted = false -vim.opt_local.matchpairs = "" -vim.opt_local.swapfile = false -vim.opt_local.buftype = "nofile" -vim.opt_local.filetype = "start" -vim.opt_local.synmaxcol = 0 -vim.opt_local.wrap = false -vim.opt_local.colorcolumn = "" -vim.opt_local.foldlevel = 999 -vim.opt_local.foldcolumn = "0" -vim.opt_local.cursorcolumn = false -vim.opt_local.cursorline = false -vim.opt_local.number = false -vim.opt_local.relativenumber = false -vim.opt_local.list = false -vim.opt_local.spell = false -vim.opt_local.signcolumn = "no" - -local augroup = vim.api.nvim_create_augroup("dashboard", { clear = true }) - -vim.api.nvim_create_autocmd("User", { - group = augroup, - pattern = { "DashboardUpdate" }, - callback = function() - if vim.api.nvim_get_current_buf() == buf then - render(buf) - end - end, - desc = "render dashboard on updates", -}) - -vim.api.nvim_create_autocmd("BufHidden", { - group = augroup, - pattern = { "*" }, - callback = function(args) - if args.buf == buf then - vim.api.nvim_del_autocmd(augroup) - return true - end - end, - desc = "clear dashboard autocmds", -}) - -sections.header = { - " ", - " ███╗ ██╗███████╗ ██████╗ ██╗ ██╗██╗███╗ ███╗ ", - " ████╗ ██║██╔════╝██╔═══██╗██║ ██║██║████╗ ████║ ", - " ██╔██╗ ██║█████╗ ██║ ██║██║ ██║██║██╔████╔██║ ", - " ██║╚██╗██║██╔══╝ ██║ ██║╚██╗ ██╔╝██║██║╚██╔╝██║ ", - " ██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ██║██║ ╚═╝ ██║ ", - " ╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═══╝ ╚═╝╚═╝ ╚═╝ ", - " ", -} - -vim.api.nvim_create_autocmd("User", { - group = augroup, - pattern = { "Lazy*" }, - callback = function() - local updates = nil - if require("lazy.status").has_updates() then - updates = string.format("updates: %s", require("lazy.status").updates()) - end - - local stats = require("lazy").stats() - sections.lazy = { - string.format("startup: %s ms", stats.startuptime), - string.format("plugins: %s (%s loaded)", stats.count, stats.loaded), - updates, - } - end, - desc = "dashboard lazy stats", -}) -- cgit v1.2.3-70-g09d2