-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
280 lines (242 loc) · 7.66 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
-- mod-version:3 lite-xl 2.1
local core = require "core"
local config = require "core.config"
local common = require "core.common"
local command = require "core.command"
local RootView = require "core.rootview"
local Object = require "core.object"
local discord = require "plugins.discord-presence.discord"
-- stolen from https://github.com/TorchedSammy/litepresence/ Copyright (c) 2021 TorchedSammy
local function makeTbl(tbl)
local t = {}
for exts, ftype in pairs(tbl) do
for ext in exts:gmatch('[^,]+') do
t[ext] = ftype
end
end
return t
end
-- extensions mapped to language names
local extTbl = makeTbl {
['asm'] = 'assembly',
['c,h'] = 'c',
['cpp,hpp'] = 'cpp',
['cr'] = 'crystal',
['cs'] = 'cs',
['css'] = 'css',
['dart'] = 'dart',
['ejs,tmpl'] = 'ejs',
['ex,exs'] = 'elixir',
['gitignore,gitattributes,gitmodules'] = 'git',
['go'] = 'go',
['hs'] = 'haskell',
['htm,html,mhtml'] = 'html',
['png,jpg,jpeg,jfif,gif,webp'] = 'image',
['java,class,properties'] = 'java',
['js'] = 'javascript',
['json'] = 'json',
['kt'] = 'kotlin',
['lua'] = 'lua',
['md,markdown'] = 'markdown',
['t'] = 'perl',
['php'] = 'php',
['py,pyx'] = 'python',
['jsx,tsx'] = 'react',
['rb'] = 'ruby',
['rs'] = 'rust',
['sh,bat'] = 'shell',
['swift'] = 'swift',
['txt,rst,rest'] = 'text',
['toml'] = 'toml',
['ts'] = 'typescript',
['vue'] = 'vue',
['xml,svg,yml,yaml,cfg,ini'] = 'xml',
}
-- thanks sammyette
-- some rules for placeholders:
-- %f - filename
-- %F - file path (absolute)
-- %d - file dir
-- %D - file dir (absolute)
-- %w - workspace name
-- %W - workspace path
-- %.n where n is a number - nth function after the string
-- %% DOES NOT NEED TO BE ESCAPED.
local default_config = {
application_id = "749282810971291659",
editing_details = "Editing %f",
idle_details = "Idling",
lower_editing_details = "in %w",
lower_idle_details = "Idle",
elapsed_time = true,
idle_timeout = 30,
autoconnect = true,
reconnect = 5
}
local rpc_config = common.merge(default_config, config.discord_rpc)
local function replace_placeholders(data, placeholders)
local text = type(data) == "string" and data or data[1]
return string.gsub(text, "%%()(.)(%d*)", function(i, t, n)
if placeholders[t] then
return placeholders[t]
elseif t == "." then
if type(data) ~= "table" then error("no function provided", 0) end
if not n or not data[tonumber(n) + 1] then
error(string.format("invalid function index at %d", i), 0)
end
return data[tonumber(n) + 1]()
else
return "%" .. t
end
end)
end
local Discord = Object:extend()
function Discord:new()
self.running = false
self.idle = false
self.error = false
self.placeholders = {}
core.add_thread(function()
while true do
coroutine.yield(config.project_scan_rate)
discord.poll()
local time = system.get_time()
if self.running then
if time - self.last_activity >= rpc_config.idle_timeout then
self.idle = true
self:update()
end
else
if not self.error
and type(rpc_config.reconnect) == "number"
and self.disconnect ~= nil
and time - self.disconnect >= rpc_config.reconnect then
self:start()
end
end
end
end)
end
function Discord:update_placeholders()
self.placeholders["w"] = common.basename(core.project_dir)
self.placeholders["W"] = core.project_dir
if core.active_view.doc and core.active_view.doc.filename then
local filename = common.basename(core.active_view.doc.filename)
self.placeholders["f"] = filename
self.placeholders["F"] = core.active_view.doc.abs_filename
local file_dir = string.sub(core.active_view.doc.abs_filename, 1, -#filename - 2)
self.placeholders["d"] = string.sub(file_dir, #core.project_dir + 1, -1) or "."
self.placeholders["D"] = file_dir
else
for _, t in ipairs { "f", "F", "d", "D" } do
self.placeholders[t] = core.active_view:get_name()
end
end
end
function Discord:update()
if not self.running then return end
self:update_placeholders()
local details = replace_placeholders(
self.idle and rpc_config.idle_details or rpc_config.editing_details,
self.placeholders
)
local state = replace_placeholders(
self.idle and rpc_config.lower_idle_details or rpc_config.lower_editing_details,
self.placeholders
)
local new_status = {
state = state,
details = details,
large_image = "litexl",
start_time = self.start_time
}
local filetype = self.placeholders["f"] and self.placeholders["f"]:match('^.+(%..+)$')
if filetype then
local img = extTbl[filetype:sub(2)]
if img and not self.idle then
new_status.large_image = img
new_status.small_image = "litexl"
end
end
discord.update(new_status)
end
function Discord:verify_config()
for _, name in ipairs { "idle_details", "editing_details", "lower_idle_details", "lower_editing_details" } do
local status, err = pcall(replace_placeholders, rpc_config[name], {})
if not status then
self.error = true
core.error("lite-xl-discord: Invalid value for config.discord_rpc.%s: %s", name, err)
end
end
return self.error
end
function Discord:start()
if self.running then return end
if self:verify_config() then return end
self.running = true
self.disconnect = nil
self.last_activity = system.get_time()
self.start_time = rpc_config.elapsed_time and os.time() or nil
discord.on_event("ready", function()
core.log("lite-xl-discord: connected to RPC!")
self:update()
end)
discord.on_event("disconnect", function(n, err)
self.running = false
self.disconnect = system.get_time()
discord.shutdown()
core.error("lite-xl-discord: lost RPC connection: %d %s", n, err)
end)
core.log("lite-xl-discord: Starting RPC")
discord.init(rpc_config.application_id)
end
function Discord:stop()
self.running = false
discord.shutdown()
core.log("lite-xl-discord: RPC stopped.")
end
function Discord:bump()
self.last_activity = system.get_time()
if self.idle then
self.idle = false
self:update()
end
end
local rpc = Discord()
-- function replacements
-- unless one day they finally decided that autoreloading user module is not a good idea
-- this will be required since user expects their config to automagically update
local load_user_directory = core.load_user_directory
function core.load_user_directory()
load_user_directory()
rpc_config = common.merge(default_config, config.discord_rpc)
end
local on_quit_project = core.on_quit_project
function core.on_quit_project(...)
rpc:stop()
on_quit_project(...)
end
local set_active_view = core.set_active_view
function core.set_active_view(view)
set_active_view(view)
core.try(rpc.update, rpc)
end
for _, fn in ipairs { "mouse_pressed", "mouse_released", "text_input" } do
local oldfn = RootView["on_" .. fn]
RootView["on_" .. fn] = function(...)
rpc:bump()
return oldfn(...)
end
end
-- commands
command.add(nil, {
["discord-rpc:stop-RPC"] = function()
rpc:stop()
end,
["discord-rpc:start-RPC"] = function()
rpc:start()
end
})
if rpc_config.autoconnect then
rpc:start()
end