-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand.lua
More file actions
310 lines (283 loc) Β· 8.13 KB
/
command.lua
File metadata and controls
310 lines (283 loc) Β· 8.13 KB
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
local M = {}
M.install = function()
require('cord.core.async').run(function() require('cord.server.update').install():await() end)
end
M.fetch = function()
require('cord.core.async').run(function() require('cord.server.update').fetch():await() end)
end
M.build = function()
require('cord.core.async').run(function() require('cord.server.update').build():await() end)
end
M.update = function()
local mode = require('cord.plugin.config').advanced.server.update
if mode == 'fetch' then
M.fetch()
elseif mode == 'install' then
M.install()
elseif mode == 'build' then
M.build()
elseif mode ~= 'none' then
require('cord.plugin.log').log_raw(
vim.log.levels.ERROR,
'Unknown update mode: ' .. '\'' .. mode .. '\''
)
end
end
M.show_presence = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:resume() end
end
M.hide_presence = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:hide() end
end
M.suppress_presence = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:suppress() end
end
M.toggle_presence = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:toggle() end
end
M.toggle_presence_suppress = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:toggle_suppress() end
end
M.idle = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:idle() end
end
M.force_idle = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:force_idle() end
end
M.unidle = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:unidle() end
end
M.toggle_idle = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:toggle_idle() end
end
M.toggle_idle_force = function()
local cord = require 'cord.server'
if cord.manager then cord.manager:toggle_idle(true) end
end
M.restart = function()
vim.schedule(function()
local cord = require 'cord.server'
if cord.is_updating then
require('cord.plugin.log').log_raw(
vim.log.levels.WARN,
'Operation cancelled: Server is updating'
)
return
end
require('cord.plugin.log').debug 'Restarting...'
local function initialize()
require('cord.core.async').run(function() cord:initialize() end)
end
if cord.manager then cord.manager:cleanup() end
if not cord.tx then return initialize() end
if not cord.client then return initialize() end
if cord.client:is_closing() then return initialize() end
if cord.client.on_close then cord.client.on_close() end
cord.client.on_close = function()
cord.client.on_close = nil
initialize()
end
cord.tx:shutdown()
end)
end
M.shutdown = function()
local cord = require 'cord.server'
if not cord.client or cord.client:is_closing() then
return require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Server is not running')
end
cord.is_updating = false
if cord.manager then cord.manager:cleanup() end
cord.tx:shutdown()
require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Stopped server')
end
M.status = function()
local cord = require 'cord.server'
if cord.status == 'ready' then
require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Status: Connected to Discord')
elseif cord.status == 'connecting' then
require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Status: Connecting to Cord server')
elseif cord.status == 'connected' then
require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Status: Connecting to Discord')
else
require('cord.plugin.log').log_raw(vim.log.levels.INFO, 'Status: Disconnected')
end
end
M.check = function()
require('cord.core.async').run(
function() require('cord.server.update').check_version():await() end
)
end
M.version = function()
require('cord.core.async').run(function() require('cord.server.update').version():await() end)
end
M.health = function() vim.cmd 'checkhealth cord' end
M.features = {
idle = { path = { 'idle', 'enabled' }, on_disable = function() M.unidle() end },
}
local function handle_feature(feature, enable)
local feat = M.features[feature]
if not feat then
require('cord.plugin.log').log_raw(
vim.log.levels.ERROR,
'Unknown option: \'' .. feature .. '\''
)
return
end
local config = require 'cord.plugin.config'
local target = config
for i = 1, #feat.path - 1 do
target = target[feat.path[i]]
end
local last = feat.path[#feat.path]
if enable == true then
target[last] = true
if feat.on_disable then feat.on_disable() end
elseif enable == false then
target[last] = false
if feat.on_disable then feat.on_disable() end
else
target[last] = not target[last]
if target[last] then
if feat.on_enable then feat.on_enable() end
else
if feat.on_disable then feat.on_disable() end
end
end
end
M.commands = {
enable = {
default = function() M.show_presence() end,
action = function(feature) handle_feature(feature, true) end,
},
disable = {
default = function() M.hide_presence() end,
action = function(feature) handle_feature(feature, false) end,
},
toggle = {
default = function() M.toggle_presence() end,
action = function(feature) handle_feature(feature) end,
},
presence = {
default = M.toggle_presence,
subcommands = {
show = M.show_presence,
hide = M.hide_presence,
suppress = M.suppress_presence,
toggle = M.toggle_presence,
toggle_suppress = M.toggle_presence_suppress,
},
},
idle = {
default = M.toggle_idle,
subcommands = {
show = M.idle,
hide = M.unidle,
toggle = M.toggle_idle,
force = M.force_idle,
},
},
update = {
default = M.update,
subcommands = {
check = M.check,
fetch = M.fetch,
install = M.install,
build = M.build,
},
},
status = M.status,
version = M.version,
restart = M.restart,
shutdown = M.shutdown,
health = M.health,
}
M.get_commands = function()
local cmds = {}
for cmd, _ in pairs(M.commands) do
table.insert(cmds, cmd)
end
return cmds
end
M.get_subcommands = function(cmd)
local command = M.commands[cmd]
if type(command) ~= 'table' or not command.subcommands then return {} end
local subcmds = {}
for subcmd, _ in pairs(command.subcommands) do
table.insert(subcmds, subcmd)
end
return subcmds
end
M.get_features = function()
local feats = {}
for feat, _ in pairs(M.features) do
table.insert(feats, feat)
end
return feats
end
M.handle = function(q_args)
if not q_args then
require('cord.plugin.log').log_raw(vim.log.levels.ERROR, 'No command provided')
return
end
if type(q_args) ~= 'string' then
require('cord.plugin.log').log_raw(
vim.log.levels.ERROR,
'Invalid input: expected string, got ' .. tostring(q_args)
)
return
end
local args = vim.split(string.gsub(q_args, '"', ''), '%s+')
local args_len = #args
if args_len == 0 then
require('cord.plugin.log').log_raw(vim.log.levels.ERROR, 'No command provided')
return
end
local cmd = args[1]
local command = M.commands[cmd]
if not command then
require('cord.plugin.log').log_raw(vim.log.levels.ERROR, 'Unknown command: \'' .. cmd .. '\'')
return
end
local execute
if type(command) == 'function' then
execute = command
else
execute = command.default
end
if args_len == 1 then
execute()
return
end
local subcmd = args[2]
if not subcmd then
require('cord.plugin.log').log_raw(vim.log.levels.ERROR, 'Additional arguments required')
return
end
if type(command) == 'table' then
if command.subcommands and command.subcommands[subcmd] then
command.subcommands[subcmd]()
elseif command.action then
command.action(subcmd)
else
require('cord.plugin.log').log_raw(
vim.log.levels.ERROR,
'Unknown option: \'' .. subcmd .. '\' for command \'' .. cmd .. '\''
)
end
else
require('cord.plugin.log').log_raw(
vim.log.levels.ERROR,
'Unknown subcommand: \'' .. subcmd .. '\' for command \'' .. cmd .. '\''
)
end
end
return M