goolord / alpha-nvim

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

Expected table got nil error #122

Closed ArcticOc closed 2 years ago

ArcticOc commented 2 years ago

I want to imitate this nice theme, which can show the number of plugins and date inside the footer.
SO I insert the following code into my alpha.lua.

local function footer()
  local plugins = #vim.tbl_keys(packer_plugins)
  local v = vim.version()
  local datetime = os.date " %d-%m-%Y   %H:%M:%S"
  return string.format(" %d   v%d.%d.%d %s  %s", plugins, v.major, v.minor, v.patch, datetime)
end

Then nvim casts the expected table got nil error. Screenshot_20220712_001959

goolord commented 2 years ago

i can't tell without seeing any of your configuration's code, but i suspect that you are setting dashboard.section.footer = footer() instead of dashboard.section.footer.val = footer()

ArcticOc commented 2 years ago

Unluckily, I'm just setting dashboard.section.footer.val = footer()
Here is the whole normal alpha.lua

local status_ok, alpha = pcall(require, "alpha")
if not status_ok then
  return
end

local dashboard = require "alpha.themes.dashboard"
dashboard.section.header.val = {}

dashboard.section.buttons.val = {
  dashboard.button("f", " " .. " Find file", ":Telescope find_files <CR>"),
  dashboard.button("e", " " .. " New file", ":ene <BAR> startinsert <CR>"),
  dashboard.button("p", " " .. " Find project", ":lua require('telescope').extensions.projects.projects()<CR>"),
  dashboard.button("r", " " .. " Recent files", ":Telescope oldfiles <CR>"),
  dashboard.button("t", " " .. " Find text", ":Telescope live_grep <CR>"),
  dashboard.button("u", " " .. " Update Plugins", ":PackerSync<CR>"),
  dashboard.button("c", " " .. " Config", ":e ~/.config/nvim/init.lua <CR>"),
  dashboard.button("q", " " .. " Quit", ":qa<CR>"),
}

--footer

local function footer()
  return {}
end

dashboard.section.footer.val = footer()

dashboard.section.footer.opts.hl = "Type"
dashboard.section.header.opts.hl = "Include"
dashboard.section.buttons.opts.hl = "Keyword"

dashboard.opts.opts.noautocmd = true
alpha.setup(dashboard.opts)
goolord commented 2 years ago

footer.val should have the lua type string here but that footer function returns a table

ArcticOc commented 2 years ago

Sorry, I'm a beginner. What should I do? Just like this?

local function footer()
  return string.format()
end
ArcticOc commented 2 years ago

Sorry, I'm a beginner. What should I do? Just like this?

local function footer()
  return string.format()
end

I've try it. It fails to work with the same error.

goolord commented 2 years ago

no problem, so there are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.

alpha at its core has its startup screen functionality wrapped around a little language for expressing layouts with lua tables.

the footer for dashboard is a table where the alpha type has been set to "text", which has 3 documented acceptable value types. https://github.com/goolord/alpha-nvim/blob/main/doc/alpha.txt#L102 val = "string" | { "array" } | function (array being a table where the keys are numbers)

so you set

local function footer()
  local plugins = #vim.tbl_keys(packer_plugins)
  local v = vim.version()
  local datetime = os.date " %d-%m-%Y   %H:%M:%S"
  return string.format(" %d   v%d.%d.%d %s  %s", plugins, v.major, v.minor, v.patch, datetime)
end
dashboard.section.footer.val = footer()

your problem upon closer inspection is that the packer_plugins you pass to tbl_keys here

  local plugins = #vim.tbl_keys(packer_plugins)

doesn't exist, and is thus nil, and the function vim.tbl_keys makes an assertion that the argument you pass is of type table.

ArcticOc commented 2 years ago

I just read the packer's manual, packer_plugins should be indeed a global table. But something went wrong with packer_plugins. Thanks for your help.

ArcticOc commented 2 years ago

I figure that out, just require ("packer_compiled")