2024-08-24 01:58:03 +02:00
|
|
|
local renderer = require("neo-tree.ui.renderer")
|
|
|
|
local buf_manager = require("codemp.buffers")
|
2024-09-08 06:24:34 +02:00
|
|
|
---@module 'nui.tree'
|
2024-08-24 01:58:03 +02:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@param workspace string workspace name
|
|
|
|
---@param path string buffer relative path
|
2024-09-08 06:24:34 +02:00
|
|
|
---@return NuiTree.Node
|
2024-08-24 01:58:03 +02:00
|
|
|
local function new_item(workspace, path)
|
|
|
|
return {
|
|
|
|
id = string.format("codemp://%s/%s", workspace, path),
|
|
|
|
name = path,
|
|
|
|
type = "buffer",
|
|
|
|
extra = {},
|
|
|
|
children = {},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param workspace string workspace name
|
|
|
|
---@param username string user display name
|
2024-09-08 06:24:34 +02:00
|
|
|
---@return NuiTree.Node
|
2024-08-24 01:58:03 +02:00
|
|
|
local function new_user(workspace, username)
|
|
|
|
return {
|
|
|
|
id = string.format("codemp://%s@%s", username, workspace),
|
|
|
|
name = username,
|
|
|
|
type = "user",
|
|
|
|
extra = {},
|
|
|
|
children = {},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param name string workspace name
|
|
|
|
---@param owned boolean true if this workspace is owned by us
|
|
|
|
---@param expanded? boolean if node should be pre-expanded
|
2024-09-08 06:24:34 +02:00
|
|
|
---@return NuiTree.Node
|
2024-08-24 01:58:03 +02:00
|
|
|
local function new_workspace(name, owned, expanded)
|
|
|
|
return {
|
|
|
|
id = "codemp://" .. name,
|
|
|
|
name = name,
|
|
|
|
type = "workspace",
|
|
|
|
extra = {
|
|
|
|
owned = owned,
|
|
|
|
},
|
|
|
|
children = {},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-07 04:23:24 +02:00
|
|
|
|
|
|
|
---@param key string
|
|
|
|
---@param value string
|
2024-09-08 06:24:34 +02:00
|
|
|
---@return NuiTree.Node
|
2024-09-07 04:23:24 +02:00
|
|
|
local function new_entry(key, value)
|
|
|
|
return {
|
|
|
|
id = "codemp-entry-" .. key .. "-" .. value,
|
|
|
|
name = key .. ": " .. value,
|
|
|
|
type = "entry",
|
|
|
|
extra = {},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-07 03:36:16 +02:00
|
|
|
local function new_root(name)
|
|
|
|
return {
|
|
|
|
id = "codemp-tree-" .. name,
|
|
|
|
name = name,
|
|
|
|
type = "root",
|
2024-09-08 06:24:34 +02:00
|
|
|
expanded = true,
|
|
|
|
expand = true,
|
2024-09-07 03:36:16 +02:00
|
|
|
extra = {},
|
|
|
|
children = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-07 04:23:24 +02:00
|
|
|
local function new_button(name)
|
|
|
|
return {
|
|
|
|
id = "codemp-button-" .. name,
|
|
|
|
name = name,
|
|
|
|
type = "button",
|
|
|
|
extra = {},
|
|
|
|
children = {}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-07 05:06:52 +02:00
|
|
|
local counter = 0;
|
|
|
|
|
2024-09-08 06:24:34 +02:00
|
|
|
---@return NuiTree.Node
|
2024-08-24 01:58:03 +02:00
|
|
|
local function spacer()
|
2024-09-07 05:06:52 +02:00
|
|
|
counter = counter + 1
|
2024-08-24 01:58:03 +02:00
|
|
|
return {
|
2024-09-07 05:06:52 +02:00
|
|
|
id = "codemp-ws-spacer-" .. counter,
|
2024-08-24 01:58:03 +02:00
|
|
|
name = "",
|
|
|
|
type = "spacer",
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-09-08 06:24:34 +02:00
|
|
|
local last_state = "N/A"
|
|
|
|
|
|
|
|
---@param tree NuiTree
|
|
|
|
local function expand(tree)
|
|
|
|
---@param node? NuiTree.Node
|
|
|
|
local function process(node)
|
|
|
|
local id = nil
|
|
|
|
if node ~= nil then id = node:get_id() end
|
|
|
|
for _, node in ipairs(tree:get_nodes(id)) do
|
|
|
|
node:expand()
|
|
|
|
if node:has_children() then
|
|
|
|
process(node)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
process()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2024-08-24 01:58:03 +02:00
|
|
|
M.update_state = function(state)
|
2024-09-08 06:24:34 +02:00
|
|
|
---@type NuiTree.Node[]
|
2024-09-07 04:01:48 +02:00
|
|
|
local root = {
|
2024-09-07 04:45:45 +02:00
|
|
|
{
|
|
|
|
id = "codemp",
|
|
|
|
name = "codemp",
|
|
|
|
type = "title",
|
|
|
|
extra = {},
|
|
|
|
}
|
2024-09-07 04:01:48 +02:00
|
|
|
}
|
2024-08-24 01:58:03 +02:00
|
|
|
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.workspace ~= nil then
|
|
|
|
local ws_section = new_root("#" .. CODEMP.workspace.name)
|
|
|
|
for i, path in ipairs(CODEMP.workspace:filetree()) do
|
|
|
|
table.insert(ws_section.children, new_item(CODEMP.workspace.name, path))
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
2024-09-07 04:01:48 +02:00
|
|
|
|
|
|
|
local usr_section = new_root("users")
|
2024-08-24 01:58:03 +02:00
|
|
|
for user, buffer in pairs(buf_manager.users) do
|
2024-09-17 17:26:23 +02:00
|
|
|
table.insert(usr_section.children, new_user(CODEMP.workspace.name, user))
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
2024-09-07 04:30:40 +02:00
|
|
|
table.insert(ws_section.children, spacer())
|
|
|
|
table.insert(ws_section.children, usr_section)
|
2024-09-08 06:24:34 +02:00
|
|
|
|
|
|
|
table.insert(root, spacer())
|
2024-09-07 04:45:45 +02:00
|
|
|
table.insert(root, ws_section)
|
2024-09-07 04:01:48 +02:00
|
|
|
end
|
|
|
|
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client ~= nil then
|
2024-09-07 04:01:48 +02:00
|
|
|
local ws_section = new_root("workspaces")
|
2024-09-17 17:26:23 +02:00
|
|
|
for _, ws in ipairs(CODEMP.available) do
|
2024-09-07 04:01:48 +02:00
|
|
|
table.insert(ws_section.children, new_workspace(ws.name, ws.owned))
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
2024-09-08 06:24:34 +02:00
|
|
|
table.insert(root, spacer())
|
2024-09-07 04:45:45 +02:00
|
|
|
table.insert(root, ws_section)
|
2024-09-07 04:23:24 +02:00
|
|
|
|
|
|
|
local status_section = new_root("client")
|
2024-09-17 17:26:23 +02:00
|
|
|
table.insert(status_section.children, new_entry("id", CODEMP.client.id))
|
|
|
|
table.insert(status_section.children, new_entry("name", CODEMP.client.username))
|
2024-09-08 06:24:34 +02:00
|
|
|
|
|
|
|
table.insert(root, spacer())
|
2024-09-07 04:45:45 +02:00
|
|
|
table.insert(root, status_section)
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client == nil then
|
2024-09-07 04:45:45 +02:00
|
|
|
table.insert(root, spacer())
|
|
|
|
table.insert(root, new_button("[connect]"))
|
2024-09-07 04:23:24 +02:00
|
|
|
end
|
2024-08-24 01:58:03 +02:00
|
|
|
|
2024-09-07 04:45:45 +02:00
|
|
|
renderer.show_nodes(root, state)
|
2024-09-08 06:24:34 +02:00
|
|
|
|
|
|
|
local new_state = "disconnected"
|
2024-09-17 17:26:23 +02:00
|
|
|
if CODEMP.client ~= nil then new_state = "connected" end
|
|
|
|
if CODEMP.workspace ~= nil then new_state = "joined" end
|
2024-09-08 06:24:34 +02:00
|
|
|
|
|
|
|
if last_state ~= new_state then expand(state.tree) end
|
|
|
|
last_state = new_state
|
2024-08-24 01:58:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|