alpha-nvim icon indicating copy to clipboard operation
alpha-nvim copied to clipboard

feat: allow term window_options to be a function

Open bennypowers opened this issue 1 year ago • 1 comments

following on from #185 , this allows users to calculate terminal window options like height and width dynamically

bennypowers avatar Feb 23 '23 10:02 bennypowers

example config which selects a colorscript based on vim window size, then calculates the term size to match

return { 'goolord/alpha-nvim',
  enabled = true,
  dependencies = {
    'nvim-lua/plenary.nvim',
    'nvim-tree/nvim-web-devicons',
  },
  config = function()
    local mru = require'utils'.mru
    local alpha = require 'alpha'
    local term = require 'alpha.term'
    local dashboard = require 'alpha.themes.dashboard'
    local nvim_web_devicons = require 'nvim-web-devicons'

    local width = 16

    local height = 16

    local heights = {
      [16] = 12,
      [32] = 16,
      [48] = 28,
      [64] = 32,
    }

    local custom_heights = {
      ['ness.bike'] = 24,
      nessy = 30,
      runaway5 = 24,
      phasedistorter = 24,
      skyrunner = 24,
    }

    alpha.setup {
      layout = {
        {
          type = 'terminal',
          command = function()
            local cats = {16}
      
            local winwidth = vim.fn.winwidth'%'
            local winheight = vim.fn.winheight'%'
      
            if winheight >= 40 then table.insert(cats, 32) end
            if winheight >= 60 then table.insert(cats, 48) end
            if winwidth >= 100 and winheight >= 60 then table.insert(cats, 64) end
      
            local headers_dir = vim.fn.expand'~/.config/nvim/headers/'
      
            local headers = {}
      
            for _, size in ipairs(cats) do
              for _, filename in ipairs(vim.fn.readdir(headers_dir .. size)) do
                table.insert(headers, { size, headers_dir .. size .. '/' .. filename })
              end
            end
      
            local size, path = unpack(headers[math.random(1, #headers)])
            local name = path:match'/([%w%.]+)%.sh$'
            width = size
            height = custom_heights[name] or heights[size] or 16
      
            return 'cat | ' .. path
          end,
          opts = {
            redraw = true,
            window_config = function()
              return {
                noautocmd = true,
                focusable = false,
                zindex = 1,
                width = width,
                height = height,
              }
            end,
          },
        },

        { type = 'padding', val = function() return height + 4 end },

        {
          type = 'group',
          val = function()
            -- mru
          end,
        },

        { type = 'padding', val = 2 },

        {
          type = 'group',
          val = { }, -- buttons
      },
    }
  end
}

bennypowers avatar Feb 23 '23 10:02 bennypowers