goolord / alpha-nvim

a lua powered greeter like vim-startify / dashboard-nvim
MIT License
1.84k stars 109 forks source link

Dynamic top/bottom padding #93

Closed siduck closed 1 year ago

siduck commented 2 years ago

I have to manually adjust the top padding in order to make the dashboard look vertically centered, is there any way to make this dynamic? Many user my config (nvchad) so I'd want alpha to look the same for others too

my config :

local alpha = require "alpha"
local function button(sc, txt, keybind)
   local sc_ = sc:gsub("%s", ""):gsub("SPC", "<leader>")

   local opts = {
      position = "center",
      text = txt,
      shortcut = sc,
      cursor = 5,
      width = 36,
      align_shortcut = "right",
      hl = "AlphaButtons",
   }

   if keybind then
      opts.keymap = { "n", sc_, keybind, { noremap = true, silent = true } }
   end

   return {
      type = "button",
      val = txt,
      on_press = function()
         local key = vim.api.nvim_replace_termcodes(sc_, true, false, true)
         vim.api.nvim_feedkeys(key, "normal", false)
      end,
      opts = opts,
   }
end

local default = {}

default.ascii = {
   "   ⣴⣶⣤⡤⠦⣤⣀⣤⠆     ⣈⣭⣿⣶⣿⣦⣼⣆          ",
   "    ⠉⠻⢿⣿⠿⣿⣿⣶⣦⠤⠄⡠⢾⣿⣿⡿⠋⠉⠉⠻⣿⣿⡛⣦       ",
   "          ⠈⢿⣿⣟⠦ ⣾⣿⣿⣷    ⠻⠿⢿⣿⣧⣄     ",
   "           ⣸⣿⣿⢧ ⢻⠻⣿⣿⣷⣄⣀⠄⠢⣀⡀⠈⠙⠿⠄    ",
   "          ⢠⣿⣿⣿⠈    ⣻⣿⣿⣿⣿⣿⣿⣿⣛⣳⣤⣀⣀   ",
   "   ⢠⣧⣶⣥⡤⢄ ⣸⣿⣿⠘  ⢀⣴⣿⣿⡿⠛⣿⣿⣧⠈⢿⠿⠟⠛⠻⠿⠄  ",
   "  ⣰⣿⣿⠛⠻⣿⣿⡦⢹⣿⣷   ⢊⣿⣿⡏  ⢸⣿⣿⡇ ⢀⣠⣄⣾⠄   ",
   " ⣠⣿⠿⠛ ⢀⣿⣿⣷⠘⢿⣿⣦⡀ ⢸⢿⣿⣿⣄ ⣸⣿⣿⡇⣪⣿⡿⠿⣿⣷⡄  ",
   " ⠙⠃   ⣼⣿⡟  ⠈⠻⣿⣿⣦⣌⡇⠻⣿⣿⣷⣿⣿⣿ ⣿⣿⡇ ⠛⠻⢷⣄ ",
   "      ⢻⣿⣿⣄   ⠈⠻⣿⣿⣿⣷⣿⣿⣿⣿⣿⡟ ⠫⢿⣿⡆     ",
   "       ⠻⣿⣿⣿⣿⣶⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⡟⢀⣀⣤⣾⡿⠃     ",
}

default.header = {
   type = "text",
   val = default.ascii,
   opts = {
      position = "center",
      hl = "AlphaHeader",
   },
}

default.buttons = {
   type = "group",
   val = {
      button("SPC f f", "  Find File  ", ":Telescope find_files<CR>"),
      button("SPC f o", "  Recent File  ", ":Telescope oldfiles<CR>"),
      button("SPC f w", "  Find Word  ", ":Telescope live_grep<CR>"),
      button("SPC b m", "  Bookmarks  ", ":Telescope marks<CR>"),
      button("SPC t h", "  Themes  ", ":Telescope themes<CR>"),
      button("SPC e s", "  Settings", ":e $MYVIMRC | :cd %:p:h <CR>"),
   },
   opts = {
      spacing = 1,
   },
}

alpha.setup {
   layout = {
      { type = "padding", val = 9 },
      default.header,
      { type = "padding", val = 2 },
      default.buttons,
   },
   opts = {},
}
siduck commented 2 years ago

https://github.com/NvChad/NvChad/pull/889

goolord commented 2 years ago

yeah i'll work something out for this. it will probably end up looking like { type = "padding", val = function () math.min(3, calcVerticalCenter()) end }

adoyle-h commented 2 years ago

@siduck You can implement dynamic padding like that,

    local alpha = require('alpha')
    local dashboard = require('alpha.themes.dashboard')
    local section = dashboard.section
    local fn = vim.fn
    local config = dashboard.config

    local marginTopPercent = 0.3
    local headerPadding = fn.max({2, fn.floor(fn.winheight(0) * marginTopPercent) })

    config.layout = {
        { type = 'padding', val = headerPadding },
        section.header,
        { type = 'padding', val = 2 },
        section.buttons,
        section.footer,
    }

    alpha.setup(config)
siduck commented 2 years ago

@adoyle-h thx for this! I'll try it and let you know