How to represnt this lua table? tbl = { ["x"] = "xxx", ["y"] = "yyy", ["z"] = "zzz"} #324
-
Hi all, I'm trying to use NuiTable to represent the following table but can't manage to: local tbl = {
["1"] = "foo",
["2"] = "bar",
["3"] = "baz"
}
dada_table = NuiTable({
bufnr = dada_pane.bufnr,
columns = {
{
align = "center",
header = "DADA",
columns = {
{ "key", header = "ID" },
{ "value", header = "Name" },
},
},
},
data = { tbl },
}) What would be the code to do it please? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 34 replies
-
First convert your data shape: local tbl = {
["1"] = "foo",
["2"] = "bar",
["3"] = "baz",
}
local data = {}
for id, name in pairs(tbl) do
table.insert(data, { id = id, name = name })
end
--[[
local data = {
{ id = "1", name = "foo" },
{ id = "2", name = "bar" },
{ id = "3", name = "baz" },
}
--]] Then update your table definition: dada_table = NuiTable({
bufnr = dada_pane.bufnr,
columns = {
{
align = "center",
header = "DADA",
columns = {
{ accessor_key = "id", header = "ID" },
{ accessor_key = "name", header = "Name" },
},
},
},
data = data,
}) Ref: https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/table/README.md |
Beta Was this translation helpful? Give feedback.
-
Hi @MunifTanjim :) firstly: thanks for Nui! secondly: didn't think of that! lastly, one more question please:
Please and thank you |
Beta Was this translation helpful? Give feedback.
-
@MunifTanjim two more questions please :) this is what I'm trying to achieve but I'm not happy with my design: code: local Layout = require("nui.layout")
local NuiSplit = require("nui.split")
local NuiTable = require("nui.table")
-- local NuiText = require("nui.text")
-- local shreds = require("chuck-nvim.core.shreds").table
local M = {}
local shreds = {
{ id = "1", name = "footer.ck" },
{ id = "2", name = "baritalia.ck" },
{ id = "3", name = "bazzing.ck" },
}
M.event = require("nui.utils.autocmd").event
M.shred_pane = NuiSplit({
ns_id = "shred_pane",
enter = true,
})
M.chuck_pane = NuiSplit({
ns_id = "chuck_pane",
enter = true,
})
M.shreds_table = NuiTable({
bufnr = M.shred_pane.bufnr,
columns = {
{
align = "center",
header = "Shreds",
columns = {
{ align = "center", width = 26, accessor_key = "id", header = "Shred ID" },
{ align = "center", width = 26, accessor_key = "name", header = "Shred Name" },
},
},
},
data = shreds,
})
M.chuck_layout = Layout(
{
position = "right",
relative = "editor",
size = "30%",
},
Layout.Box({
Layout.Box(M.chuck_pane, { size = "55%" }),
Layout.Box(M.shred_pane, { size = "45%" }),
}, {
dir = "col",
})
)
M.update_layout = (
Layout.Box({
Layout.Box(M.shred_pane, { size = "45%" }),
Layout.Box(M.chuck_pane, { size = "55%" }),
}, {
dir = "col",
})
)
return M Questions:
Please and thank you |
Beta Was this translation helpful? Give feedback.
-
@MunifTanjim not sure you've seen this in all the messages. Reposting:
not necessarily a NuiTable. I'd be happy to use a for loop on NuiLine or NuiText to display "1 foo.ck" and so on on their own lines. the table was my first idea, but I can ditch it for better layout. |
Beta Was this translation helpful? Give feedback.
-
Hi @MunifTanjim, after all I think I need something repopulating the tree each time the table is updated: #71 (comment) but can't figure out how to implement it. Can I please ask for one more help? Fingers crossed this should be it. Please and thank you EDIT: this is where I'd use it from https://github.com/gacallea/chuck-nvim/blob/main/lua/chuck-nvim/ui/layout.lua [...]
M.set_expanded_nodes = function(tree)
local nodes = mknodes()
for _, id in ipairs(nodes or {}) do
local node = tree:get_node(id)
if node ~= nil then
node:expand()
end
end
end
[...] from https://github.com/gacallea/chuck-nvim/blob/main/lua/chuck-nvim/core/utils.lua local layout = require("chuck-nvim.ui.layout")
local shreds = require("chuck-nvim.core.shreds")
[...]
local function shred_lines(logfile)
local file = assert(io.open(logfile, "r"))
file:seek("end")
local it = file:lines()
while vim.fn.getchar(0) == 0 do
local line = it()
if line then
shreds.set_table(line)
layout.shreds_tree.set_expanded_nodes(layout.shreds_tree)
layout.shreds_tree:render()
end
end
file:close()
end
[...] |
Beta Was this translation helpful? Give feedback.
First convert your data shape:
Then update your table definition:
Ref: https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/table/REA…