Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local M = {}
- -- Default welcome message
- local welcome_lines = {
- '',
- ' ███╗ ██╗███████╗ ██████╗ ██╗ ██╗██╗███╗ ███╗',
- ' ████╗ ██║██╔════╝██╔═══██╗██║ ██║██║████╗ ████║',
- ' ██╔██╗ ██║█████╗ ██║ ██║██║ ██║██║██╔████╔██║',
- ' ██║╚██╗██║██╔══╝ ██║ ██║╚██╗ ██╔╝██║██║╚██╔╝██║',
- ' ██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ██║██║ ╚═╝ ██║',
- ' ╚═╝ ╚═══╝╚══════╝ ╚═════╝ ╚═══╝ ╚═╝╚═╝ ╚═╝',
- '',
- ' Welcome to Neovim!',
- '',
- ' Press any key to continue...',
- '',
- }
- local function should_show_welcome()
- local buffers = vim.api.nvim_list_bufs()
- for _, buf in ipairs(buffers) do
- if vim.api.nvim_buf_is_loaded(buf) then
- local name = vim.api.nvim_buf_get_name(buf)
- local buftype = vim.bo[buf].buftype
- local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
- if buftype == '' and vim.bo[buf].buflisted then
- if name ~= '' or (#lines > 1 or (#lines == 1 and lines[1] ~= '')) then return false end
- end
- end
- end
- return true
- end
- local function create_welcome_buffer()
- local buf = vim.api.nvim_create_buf(false, true)
- vim.bo[buf].buftype = 'nofile'
- vim.bo[buf].bufhidden = 'wipe'
- vim.bo[buf].swapfile = false
- vim.bo[buf].buflisted = false
- vim.bo[buf].modifiable = false
- -- Calculate centering
- local win_height = vim.api.nvim_win_get_height(0)
- local win_width = vim.api.nvim_win_get_width(0)
- local content_height = #welcome_lines
- local start_row = math.max(0, math.floor((win_height - content_height) / 2))
- -- Find the longest line for consistent centering
- local max_width = 0
- for _, line in ipairs(welcome_lines) do
- max_width = math.max(max_width, vim.fn.strdisplaywidth(line))
- end
- -- Calculate horizontal padding for the entire block
- local horizontal_padding = math.max(0, math.floor((win_width - max_width) / 2))
- -- Add empty lines to center vertically
- local centered_lines = {}
- for _ = 1, start_row do
- table.insert(centered_lines, '')
- end
- -- Add each line with consistent horizontal padding (but not for empty lines)
- for _, line in ipairs(welcome_lines) do
- if line == '' then
- table.insert(centered_lines, '') -- Keep empty lines truly empty
- else
- table.insert(centered_lines, string.rep(' ', horizontal_padding) .. line)
- end
- end
- vim.bo[buf].modifiable = true
- vim.api.nvim_buf_set_lines(buf, 0, -1, false, centered_lines)
- return buf
- end
- local function close_welcome()
- local buf = vim.api.nvim_get_current_buf()
- if vim.bo[buf].buftype == 'nofile' and vim.bo[buf].bufhidden == 'wipe' then vim.cmd('enew') end
- end
- local function setup_welcome_listener(buf)
- local listener_id
- listener_id = vim.on_key(function(key)
- if vim.api.nvim_get_current_buf() == buf then
- vim.on_key(nil, listener_id)
- vim.defer_fn(close_welcome, 1)
- end
- end)
- -- Clean up listener if buffer is deleted
- vim.api.nvim_create_autocmd('BufDelete', {
- buffer = buf,
- callback = function() vim.on_key(nil, listener_id) end,
- once = true,
- })
- end
- function M.show_welcome()
- if not should_show_welcome() then return end
- local buf = create_welcome_buffer()
- vim.api.nvim_set_current_buf(buf)
- setup_welcome_listener(buf)
- local win = vim.api.nvim_get_current_win()
- local original_opts = {}
- local opts_to_hide = {
- number = false,
- relativenumber = false,
- signcolumn = 'no',
- foldcolumn = '0',
- statuscolumn = '',
- }
- for opt, new_value in pairs(opts_to_hide) do
- original_opts[opt] = vim.wo[win][opt]
- vim.wo[win][opt] = new_value
- end
- vim.opt.guicursor = 'a:hor1-Cursor'
- vim.api.nvim_create_autocmd('BufLeave', {
- buffer = buf,
- callback = function()
- vim.opt.guicursor = ''
- for opt, original_value in pairs(original_opts) do
- vim.wo[win][opt] = original_value
- end
- end,
- once = true,
- })
- end
- function M.setup(opts)
- opts = opts or {}
- if opts.message then welcome_lines = opts.message end
- vim.api.nvim_create_autocmd('VimEnter', {
- callback = function() M.show_welcome() end,
- })
- end
- return M
Advertisement
Add Comment
Please, Sign In to add comment