Guest User

welcome.lua

a guest
Aug 18th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.64 KB | None | 0 0
  1. local M = {}
  2.  
  3. -- Default welcome message
  4. local welcome_lines = {
  5.   '',
  6.   '  ███╗   ██╗███████╗ ██████╗ ██╗   ██╗██╗███╗   ███╗',
  7.   '  ████╗  ██║██╔════╝██╔═══██╗██║   ██║██║████╗ ████║',
  8.   '  ██╔██╗ ██║█████╗  ██║   ██║██║   ██║██║██╔████╔██║',
  9.   '  ██║╚██╗██║██╔══╝  ██║   ██║╚██╗ ██╔╝██║██║╚██╔╝██║',
  10.   '  ██║ ╚████║███████╗╚██████╔╝ ╚████╔╝ ██║██║ ╚═╝ ██║',
  11.   '  ╚═╝  ╚═══╝╚══════╝ ╚═════╝   ╚═══╝  ╚═╝╚═╝     ╚═╝',
  12.   '',
  13.   '              Welcome to Neovim!',
  14.   '',
  15.   '           Press any key to continue...',
  16.   '',
  17. }
  18.  
  19. local function should_show_welcome()
  20.   local buffers = vim.api.nvim_list_bufs()
  21.  
  22.   for _, buf in ipairs(buffers) do
  23.     if vim.api.nvim_buf_is_loaded(buf) then
  24.       local name = vim.api.nvim_buf_get_name(buf)
  25.       local buftype = vim.bo[buf].buftype
  26.       local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
  27.  
  28.       if buftype == '' and vim.bo[buf].buflisted then
  29.         if name ~= '' or (#lines > 1 or (#lines == 1 and lines[1] ~= '')) then return false end
  30.       end
  31.     end
  32.   end
  33.  
  34.   return true
  35. end
  36.  
  37. local function create_welcome_buffer()
  38.   local buf = vim.api.nvim_create_buf(false, true)
  39.  
  40.   vim.bo[buf].buftype = 'nofile'
  41.   vim.bo[buf].bufhidden = 'wipe'
  42.   vim.bo[buf].swapfile = false
  43.   vim.bo[buf].buflisted = false
  44.   vim.bo[buf].modifiable = false
  45.  
  46.   -- Calculate centering
  47.   local win_height = vim.api.nvim_win_get_height(0)
  48.   local win_width = vim.api.nvim_win_get_width(0)
  49.   local content_height = #welcome_lines
  50.   local start_row = math.max(0, math.floor((win_height - content_height) / 2))
  51.  
  52.   -- Find the longest line for consistent centering
  53.   local max_width = 0
  54.   for _, line in ipairs(welcome_lines) do
  55.     max_width = math.max(max_width, vim.fn.strdisplaywidth(line))
  56.   end
  57.  
  58.   -- Calculate horizontal padding for the entire block
  59.   local horizontal_padding = math.max(0, math.floor((win_width - max_width) / 2))
  60.  
  61.   -- Add empty lines to center vertically
  62.   local centered_lines = {}
  63.   for _ = 1, start_row do
  64.     table.insert(centered_lines, '')
  65.   end
  66.  
  67.   -- Add each line with consistent horizontal padding (but not for empty lines)
  68.   for _, line in ipairs(welcome_lines) do
  69.     if line == '' then
  70.       table.insert(centered_lines, '') -- Keep empty lines truly empty
  71.     else
  72.       table.insert(centered_lines, string.rep(' ', horizontal_padding) .. line)
  73.     end
  74.   end
  75.  
  76.   vim.bo[buf].modifiable = true
  77.   vim.api.nvim_buf_set_lines(buf, 0, -1, false, centered_lines)
  78.  
  79.   return buf
  80. end
  81.  
  82. local function close_welcome()
  83.   local buf = vim.api.nvim_get_current_buf()
  84.  
  85.   if vim.bo[buf].buftype == 'nofile' and vim.bo[buf].bufhidden == 'wipe' then vim.cmd('enew') end
  86. end
  87.  
  88. local function setup_welcome_listener(buf)
  89.   local listener_id
  90.  
  91.   listener_id = vim.on_key(function(key)
  92.     if vim.api.nvim_get_current_buf() == buf then
  93.       vim.on_key(nil, listener_id)
  94.       vim.defer_fn(close_welcome, 1)
  95.     end
  96.   end)
  97.  
  98.   -- Clean up listener if buffer is deleted
  99.   vim.api.nvim_create_autocmd('BufDelete', {
  100.     buffer = buf,
  101.     callback = function() vim.on_key(nil, listener_id) end,
  102.     once = true,
  103.   })
  104. end
  105.  
  106. function M.show_welcome()
  107.   if not should_show_welcome() then return end
  108.  
  109.   local buf = create_welcome_buffer()
  110.   vim.api.nvim_set_current_buf(buf)
  111.  
  112.   setup_welcome_listener(buf)
  113.  
  114.   local win = vim.api.nvim_get_current_win()
  115.  
  116.   local original_opts = {}
  117.   local opts_to_hide = {
  118.     number = false,
  119.     relativenumber = false,
  120.     signcolumn = 'no',
  121.     foldcolumn = '0',
  122.     statuscolumn = '',
  123.   }
  124.  
  125.   for opt, new_value in pairs(opts_to_hide) do
  126.     original_opts[opt] = vim.wo[win][opt]
  127.     vim.wo[win][opt] = new_value
  128.   end
  129.  
  130.   vim.opt.guicursor = 'a:hor1-Cursor'
  131.  
  132.   vim.api.nvim_create_autocmd('BufLeave', {
  133.     buffer = buf,
  134.     callback = function()
  135.       vim.opt.guicursor = ''
  136.       for opt, original_value in pairs(original_opts) do
  137.         vim.wo[win][opt] = original_value
  138.       end
  139.     end,
  140.     once = true,
  141.   })
  142. end
  143.  
  144. function M.setup(opts)
  145.   opts = opts or {}
  146.  
  147.   if opts.message then welcome_lines = opts.message end
  148.  
  149.   vim.api.nvim_create_autocmd('VimEnter', {
  150.     callback = function() M.show_welcome() end,
  151.   })
  152. end
  153.  
  154. return M
Advertisement
Add Comment
Please, Sign In to add comment