-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.lua
389 lines (358 loc) · 12.7 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
local netman_utils = require("netman.tools.utils")
local netman_api = require("netman.api")
local netman_types = require("netman.tools.options").api.READ_TYPE
local netman_type_attributes = require("netman.tools.options").api.ATTRIBUTES
local neo_tree_utils = require("neo-tree.utils")
local neo_tree_renderer = require("neo-tree.ui.renderer")
local netman_ui = require("netman.ui")
local logger = netman_ui.get_logger()
local neo_tree_defaults = require("netman.ui.neo-tree.defaults")
local M = {}
M.internal = {}
M.constants = {
TYPES = {
NETMAN_BOOKMARK = "netman_bookmark",
NETMAN_PROVIDER = "netman_provider",
NETMAN_HOST = "netman_host",
NETMAN_EXPLORE = "directory",
NETMAN_FILE = "file",
NETMAN_STREAM = "stream"
},
ATTRIBUTE_MAP = {
[netman_type_attributes.DESTINATION] = "file",
[netman_type_attributes.LINK] = "directory"
},
ROOT_IDS = {
NETMAN_RECENTS = "netman_recents",
NETMAN_FAVORITES = "netman_favorites",
NETMAN_PROVIDERS = "netman_providers",
}
}
M.name = "remote"
M.display_name = 'ﯱ Remote'
M.default_config = neo_tree_defaults
M.internal.sorter = {}
-- Sorts nodes in ascending order
M.internal.sorter.ascending = function(a, b) return a.name < b.name end
-- Sorts nodes in descending order
M.internal.sorter.descending = function(a, b) return a.name > b.name end
M.internal.current_process_handle = nil
M.internal.node_map = {}
M.internal.navigate_map = {}
M.internal.refresh_map = {}
M.internal._root_nodes = {
{
name = "Recents",
id = M.constants.ROOT_IDS.NETMAN_RECENTS,
type = M.constants.TYPES.NETMAN_BOOKMARK,
children = {},
parent_id = nil,
extra = {
icon = "",
}
},
{
id = M.constants.ROOT_IDS.NETMAN_FAVORITES,
name = "Favorites",
type = M.constants.TYPES.NETMAN_BOOKMARK,
children = {},
parent_id = nil,
extra = {
icon = "",
}
},
{
name = "Providers",
id = M.constants.ROOT_IDS.NETMAN_PROVIDERS,
type = M.constants.TYPES.NETMAN_BOOKMARK,
children = {},
parent_id = nil,
extra = {
icon = "",
skip = false,
},
},
}
M._root = {}
----------------- \/ Basic Helper Functions
local function get_mapped_node(nui_node)
return nui_node and M.internal.node_map[nui_node:get_id()] or nil
end
local function create_node(node_details, parent_id)
-- Probably should validate we have good data to create
-- a nui node
local parent_node = parent_id and M.internal.node_map[parent_id]
local node = {
name = node_details.name,
id = node_details.id,
extra = node_details.extra or {},
type = node_details.type,
parent_id = parent_id,
navigate = node_details.navigate or M.internal.navigate_map[node_details.id] or M.internal.navigate_map[node_details.type],
refresh = node_details.refresh or M.internal.refresh_map[node_details.id] or M.internal.refresh_map[node_details.type],
collapsed = node_details.collapsed, -- We may not need this variable now
}
local nui_node = {
id = node.id,
name = node.name,
type = node.type,
extra = node_details.extra or {}
}
if not node.id then
logger.warn("Node was created with no id!", node_details)
end
if not node.name then
logger.warn("Node was created with no name!", node_details)
end
if not node.type then
logger.warn("Node was created with no type!", node_details)
end
if node_details.children then
-- Iterate through the children and create them too
node.children = {}
local nui_children = {}
for _, raw_child_node in ipairs(node_details.children) do
local child_node = create_node(raw_child_node, node.id)
table.insert(node.children, child_node.id)
table.insert(nui_children, child_node.nui_node)
end
nui_node.children = nui_children
end
if parent_node then
if not parent_node.children then
-- This is ick, but I suppose we should
-- put a guardrail in place for adding kids when
-- the parent isn't ready for them
parent_node.children = {}
end
table.insert(parent_node.children, node.id)
end
nui_node.extra.parent = node.parent_id
node.extra.nui_node = nui_node
M.internal.node_map[node.id] = node
return node
end
local function tree_to_nui(in_tree, do_sort)
-- TODO: I hate that this is recursive...
local tree = {}
for _, leaf_id in ipairs(in_tree) do
local leaf = M.internal.node_map[leaf_id]
if not leaf.extra.skip then
local node = leaf.extra.nui_node
if leaf.children then
node.children = tree_to_nui(leaf.children, do_sort)
end
table.insert(tree, node)
end
end
if do_sort then
-- We should probably use the
-- neo_tree_utils.sort_by_tree_display function instead
table.sort(tree, M.internal.sorter.ascending)
end
return tree
end
local function navigate_root_provider(nui_node)
local node = get_mapped_node(nui_node)
if nui_node:is_expanded() then
nui_node:collapse()
node.collapsed = true
else
nui_node:expand()
node.collapsed = false
if #node.children > 0 then
return nil, true
end
-- TODO: Add some sort of auto refresh?
local raw_providers = netman_ui.get_providers()
local providers = {}
for name, provider_details in pairs(raw_providers) do
local provider_node = {
id = provider_details.path,
name = name,
type = M.constants.TYPES.NETMAN_PROVIDER,
children = {},
extra = {
icon = provider_details.ui.icon,
highlight = provider_details.ui.highlight,
path = provider_details.path,
hosts_func = provider_details.hosts
}
}
create_node(provider_node, node.id)
table.insert(providers, provider_node.id)
end
logger.debug("Adding provider table to root providers node", providers)
return tree_to_nui(providers, true)
end
return nil, true
end
local function navigate_file(nui_node)
-- Do nothing. Don't use this
end
local function navigate_directory(nui_node)
local node = get_mapped_node(nui_node)
if nui_node:is_expanded() then
nui_node:collapse()
node.collapsed = true
else
nui_node:expand()
-- Get content for the node
node.collapsed = false
end
return nil, true
end
----------------- /\ Basic Helper Functions
----------------- \/ Provider Helper Functions
local function navigate_provider(nui_node)
-- Collapse the node if its expanded
local node = get_mapped_node(nui_node)
if nui_node:is_expanded() then
nui_node:collapse()
node.collapsed = true
else
-- Get content for the node
local hosts = {}
local raw_hosts = node.extra.hosts_func()
for host_name, host_state_func in pairs(raw_hosts) do
local raw_host_details = host_state_func()
local host_details = {
id = raw_host_details.id,
name = host_name,
type = M.constants.TYPES.NETMAN_HOST,
children = {},
extra = {
-- Maybe make this pass a callable?
state = raw_host_details.state,
uri = raw_host_details.uri,
entrypoint = raw_host_details.entrypoint,
last_access = raw_host_details.last_loaded,
}
}
create_node(host_details, nui_node:get_id())
table.insert(hosts, host_details.id)
end
nui_node:expand()
node.collapsed = false
return tree_to_nui(hosts, true)
end
return nil, true
end
local function refresh_provider(nui_node)
local node = M.internal.node_map[nui_node.id]
logger.infof("Refreshing Node: %s", node.name)
end
----------------- /\ Provider Helper Functions
----------------- \/ URI Helper Functions
local function open_file(file)
end
local function open_directory(directory, parent_id)
local parent_node = M.internal.node_map[parent_id]
for _, raw_node in ipairs(directory.data) do
local node = {
id = raw_node.URI,
name = raw_node.NAME,
type = M.constants.ATTRIBUTE_MAP[raw_node.FIELD_TYPE],
children = {},
extra = {
uri = raw_node.URI,
metadata = raw_node.METADATA
}
}
create_node(node, parent_id)
end
local children = parent_node.children
return tree_to_nui(children, true)
end
local function open_stream(stream)
end
local function async_process_uri_results(results, parent_id)
-- TODO: Handle failure somehow?
local results_type = results.type
if results_type == netman_types.EXPLORE then
return open_directory(results, parent_id)
elseif results_type == netman_types.FILE then
return open_file(results)
else
return open_stream(results)
end
end
local function navigate_uri(nui_node, state)
-- TODO: We need something to prevent accidentally executing "multiple" reads at once
local node = get_mapped_node(nui_node)
if nui_node:is_expanded() then
-- Collapse the node and return
nui_node:collapse()
node.collapsed = true
return nil, true
end
-- We should temporarily map something globally to cancel the read?
-- Or add something to the top of the tree to "stop" the handle?
M.internal.current_process_handle = netman_api.read(node.extra.uri, {}, function(data, complete)
local render_tree = async_process_uri_results(data, node.id)
if complete then
M.internal.finish_navigate(state, render_tree, nui_node:get_id())
end
end)
-- Returing "true" so we can update to show the "refresh"/"loading" icon
return nil, true
end
local function refresh_uri(nui_node)
end
----------------- /\ URI Helper Functions
function M.internal.generate_tree(state)
return tree_to_nui(M._root)
end
function M.internal.finish_navigate(state, render_tree, render_parent, do_redraw_only)
if do_redraw_only then
neo_tree_renderer.redraw(state)
elseif render_tree and #render_tree > 0 then
-- Purge children?
neo_tree_renderer.show_nodes(render_tree, state, render_parent)
end
end
function M.navigate(state)
local tree = state.tree
local render_tree = nil
local render_parent = nil
local do_redraw_only = false
-- We should see if we can just redraw the tree?
if not tree or not neo_tree_renderer.window_exists(state) then
render_tree = M.internal.generate_tree(state)
else
local target_node = tree and tree:get_node()
if not target_node then
-- There is nothing to do, render the root tree and move on
render_tree = M.internal.generate_tree(state)
elseif target_node then
local mapped_node = get_mapped_node(target_node)
if not mapped_node then
logger.warnf("Unable to find matching mapped node for %s!", target_node:get_id())
return
end
render_tree, do_redraw_only = mapped_node.navigate(target_node, state)
if render_tree then
render_parent = target_node:get_id()
end
end
end
M.internal.finish_navigate(state, render_tree, render_parent, do_redraw_only)
end
function M.setup()
logger.debug("Initializing Neotree Node Type Navigation Map")
M.internal.navigate_map[M.constants.ROOT_IDS.NETMAN_PROVIDERS] = navigate_root_provider
M.internal.navigate_map[M.constants.ROOT_IDS.NETMAN_FAVORITES] = navigate_directory
M.internal.navigate_map[M.constants.ROOT_IDS.NETMAN_RECENTS] = navigate_directory
M.internal.navigate_map[M.constants.TYPES.NETMAN_FILE] = navigate_uri
M.internal.navigate_map[M.constants.TYPES.NETMAN_EXPLORE] = navigate_uri
M.internal.navigate_map[M.constants.TYPES.NETMAN_HOST] = navigate_uri
M.internal.navigate_map[M.constants.TYPES.NETMAN_PROVIDER] = navigate_provider
logger.debug("Initializing Neotree Node Type Refresh Map")
M.internal.refresh_map[M.constants.TYPES.NETMAN_PROVIDER] = refresh_provider
for _, node in pairs(M.internal._root_nodes) do
create_node(node)
table.insert(M._root, node.id)
end
logger.debug("Root", M._root)
end
return M