Skip to content

Commit

Permalink
Fix workspace_blacklist
Browse files Browse the repository at this point in the history
Fix `workspace_blacklist`
  • Loading branch information
vyfor authored Jun 13, 2024
2 parents cf0af0f + e4ea4ca commit 1ac0498
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 108 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
require('cord').setup({
usercmds = true, -- Enable user commands
timer = {
enable = true, -- Enable automatically updating presence
interval = 1500, -- Interval between presence updates in milliseconds (min 500)
reset_on_idle = false, -- Reset start timestamp on idle
reset_on_change = false, -- Reset start timestamp on presence change
Expand Down
143 changes: 77 additions & 66 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local utils = require 'cord.utils'
cord.config = {
usercmds = true,
timer = {
enable = true,
interval = 1500,
reset_on_idle = false,
reset_on_change = false,
Expand Down Expand Up @@ -63,10 +62,15 @@ local force_idle = false
local problem_count = -1
local last_updated = os.clock()
local last_presence
local is_blacklisted

local function connect(config)
discord.init(
local function init(config)
local blacklist_len = #config.display.workspace_blacklist
local blacklist_arr = ffi.new(
'const char*[' .. blacklist_len .. ']',
config.display.workspace_blacklist
)

return discord.init(
ffi.new(
'InitArgs',
config.editor.client,
Expand All @@ -81,6 +85,8 @@ local function connect(config)
config.text.lsp_manager,
config.text.vcs,
config.text.workspace,
blacklist_arr,
blacklist_len,
vim.fn.getcwd(),
config.display.swap_fields
),
Expand Down Expand Up @@ -113,9 +119,13 @@ local function update_idle_presence(config)
last_presence['idle'] = true
if config.timer.reset_on_idle then discord.update_time() end
if config.idle.show_status then
discord.update_presence(
ffi.new('PresenceArgs', '', 'Cord.idle', nil, 0, false)
)
if
discord.update_presence(
ffi.new('PresenceArgs', '', 'Cord.idle', nil, 0, false)
) > 1
then
timer:stop()
end
else
discord.clear_presence()
end
Expand Down Expand Up @@ -146,9 +156,7 @@ local function update_idle_presence(config)
return false
end

local function update_presence(config, initial)
if is_blacklisted then return end

local function update_presence(config)
local cursor = vim.api.nvim_win_get_cursor(0)
problem_count = utils.get_problem_count(config) or -1
local current_presence = {
Expand Down Expand Up @@ -182,9 +190,9 @@ local function update_presence(config, initial)
local icon, name =
utils.get_icon(config, current_presence.name, current_presence.type)

local success
local status
if icon then
success = discord.update_presence_with_assets(
status = discord.update_presence_with_assets(
icon.name or name,
type(icon) == 'string' and icon or icon.icon,
icon.tooltip,
Expand All @@ -199,7 +207,7 @@ local function update_presence(config, initial)
)
)
else
success = discord.update_presence(
status = discord.update_presence(
ffi.new(
'PresenceArgs',
current_presence.name,
Expand All @@ -210,52 +218,49 @@ local function update_presence(config, initial)
)
)
end
if success then

if status == 0 then
last_presence = current_presence
if is_blacklisted == nil then
is_blacklisted = utils.array_contains(
config.display.workspace_blacklist,
ffi.string(discord.update_workspace(vim.fn.getcwd()))
)
end
if initial then
timer:stop()
timer:start(
0,
config.timer.interval,
vim.schedule_wrap(function() update_presence(config, false) end)
)
end
else
connection_tries = connection_tries + 1
if connection_tries == 16 then
vim.notify(
'[cord.nvim] Failed to connect to Discord within 15 seconds, shutting down connection',
vim.log.levels.WARN
)
connection_tries = 0
timer:stop()
discord.disconnect()
enabled = false
last_presence = nil
end
if status > 1 then timer:stop() end
end
elseif not update_idle_presence(config) then
return
end
end

local function connect(config)
if discord.is_connected() then
timer:stop()
timer:start(
0,
config.timer.interval,
vim.schedule_wrap(function() update_presence(config) end)
)
return
end

connection_tries = connection_tries + 1
if connection_tries == 16 then
vim.notify(
'[cord.nvim] Failed to connect to Discord within 15 seconds, shutting down connection',
vim.log.levels.WARN
)
connection_tries = 0
timer:stop()
discord.disconnect()
enabled = false
last_presence = nil
end
end

local function start_timer(config)
timer:stop()

if not utils.validate_severity(config) then return end
if config.display.show_time then discord.update_time() end

timer:start(
0,
1000,
vim.schedule_wrap(function() update_presence(config, true) end)
)
timer:start(0, 1000, vim.schedule_wrap(function() connect(config) end))
enabled = true
end

function cord.setup(userConfig)
Expand All @@ -272,19 +277,17 @@ function cord.setup(userConfig)
config.timer.interval = math.max(config.timer.interval, 500)

discord = utils.init_discord(ffi)
connect(config)
if config.timer.enable then
cord.setup_autocmds(config)
start_timer(config)
end
cord.setup_autocmds(config)
if config.usercmds then cord.setup_usercmds(config) end

vim.cmd [[
autocmd! ExitPre * lua require('cord').disconnect()
]]

if config.usercmds then cord.setup_usercmds(config) end

vim.g.cord_initialized = true

if init(config) == 2 then return end
start_timer(config)
end
end

Expand All @@ -296,10 +299,14 @@ function cord.setup_autocmds(config)
]]

function cord.on_dir_changed()
is_blacklisted = utils.array_contains(
config.display.workspace_blacklist,
ffi.string(discord.update_workspace(vim.fn.getcwd()))
)
last_presence = nil
if not discord.update_workspace(vim.fn.getcwd()) then
timer:stop()
discord.clear_presence()
enabled = false
else
if not enabled then start_timer(config) end
end
end

function cord.on_focus_gained()
Expand All @@ -325,17 +332,17 @@ function cord.setup_usercmds(config)
]]

function cord.connect()
connect(config)
start_timer(config)
if discord.is_connected() or init(config) > 1 then return end

if not enabled then start_timer(config) end
end

function cord.reconnect()
timer:stop()
discord.disconnect()
last_presence = nil
connect(config)
start_timer(config)
enabled = true
init(config)
if not enabled then start_timer(config) end
end

function cord.toggle_presence()
Expand All @@ -346,13 +353,11 @@ function cord.setup_usercmds(config)
last_presence = nil
else
start_timer(config)
enabled = true
end
end

function cord.show_presence()
start_timer(config)
enabled = true
if not enabled then start_timer(config) end
end

function cord.hide_presence()
Expand Down Expand Up @@ -381,8 +386,14 @@ function cord.setup_usercmds(config)
end

function cord.set_workspace(workspace)
discord.set_workspace(workspace)
last_presence = nil
if not discord.set_workspace(workspace) then
timer:stop()
discord.clear_presence()
enabled = false
else
if not enabled then start_timer(config) end
end
end
end

Expand Down
20 changes: 11 additions & 9 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,44 @@ local function init_discord(ffi)
const char* lsp_manager_text;
const char* vcs_text;
const char* workspace_text;
const char** workspace_blacklist;
const int workspace_blacklist_len;
const char* initial_path;
const bool swap;
} InitArgs;
typedef struct {
const char* filename;
const char* filetype;
const char* cursor_position;
int problem_count;
bool is_read_only;
const int problem_count;
const bool is_read_only;
} PresenceArgs;
typedef struct {
const char* first_label;
const char* first_url;
const char* second_label;
const char* second_url;
} Buttons;
void init(
const bool is_connected();
const uint8_t init(
const InitArgs* args,
const Buttons* buttons
);
const bool update_presence(
const uint8_t update_presence(
const PresenceArgs* args
);
const bool update_presence_with_assets(
const uint8_t update_presence_with_assets(
const char* name,
const char* icon,
const char* tooltip,
int asset_type,
const int asset_type,
const PresenceArgs* args
);
void clear_presence();
void disconnect();
void set_workspace(const char* workspace);
const char* update_workspace(const char* workspace);
void update_time();
const char* get_workspace();
const bool set_workspace(const char* workspace);
const bool update_workspace(const char* workspace);
]]

return ffi.load(new_path)
Expand Down
Loading

0 comments on commit 1ac0498

Please sign in to comment.