Skip to content

Commit

Permalink
fix: workspace_blacklist misbehaving (note: config.timer.enable is no…
Browse files Browse the repository at this point in the history
… longer a valid option)
  • Loading branch information
vyfor committed Jun 13, 2024
1 parent c2b06b8 commit f2c9d99
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
48 changes: 29 additions & 19 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 @@ -71,7 +70,7 @@ local function init(config)
config.display.workspace_blacklist
)

discord.init(
return discord.init(
ffi.new(
'InitArgs',
config.editor.client,
Expand Down Expand Up @@ -256,10 +255,12 @@ local function connect(config)
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() connect(config) end))
enabled = true
end

function cord.setup(userConfig)
Expand All @@ -276,31 +277,36 @@ function cord.setup(userConfig)
config.timer.interval = math.max(config.timer.interval, 500)

discord = utils.init_discord(ffi)
init(config)
if config.timer.enable then
cord.setup_autocmds()
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

function cord.setup_autocmds()
function cord.setup_autocmds(config)
vim.cmd [[
autocmd! DirChanged * lua require('cord').on_dir_changed()
autocmd! FocusGained * lua require('cord').on_focus_gained()
autocmd! FocusLost * lua require('cord').on_focus_lost()
]]

function cord.on_dir_changed()
if not discord.update_workspace(vim.fn.getcwd()) then timer:stop() end
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 @@ -326,17 +332,17 @@ function cord.setup_usercmds(config)
]]

function cord.connect()
init(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
init(config)
start_timer(config)
enabled = true
if not enabled then start_timer(config) end
end

function cord.toggle_presence()
Expand All @@ -347,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 @@ -382,8 +386,14 @@ function cord.setup_usercmds(config)
end

function cord.set_workspace(workspace)
if not discord.update_workspace(workspace) then timer:stop() end
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
1 change: 1 addition & 0 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ local function init_discord(ffi)
void clear_presence();
void disconnect();
void update_time();
const bool set_workspace(const char* workspace);
const bool update_workspace(const char* workspace);
]]

Expand Down
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ pub unsafe extern "C" fn update_time() {
}

#[no_mangle]
pub unsafe extern "C" fn update_workspace(value: *mut c_char) -> bool {
pub unsafe extern "C" fn set_workspace(value: *mut c_char) -> bool {
if let Some(config) = CONFIG.as_mut() {
config.workspace = ptr_to_string(value);

Expand All @@ -504,3 +504,22 @@ pub unsafe extern "C" fn update_workspace(value: *mut c_char) -> bool {

true
}

#[no_mangle]
pub unsafe extern "C" fn update_workspace(value: *mut c_char) -> bool {
if let Some(config) = CONFIG.as_mut() {
if let Some(workspace) =
find_workspace(&ptr_to_string(value)).file_name()
{
config.workspace = workspace.to_string_lossy().to_string();

if config.workspace_blacklist.contains(&config.workspace) {
return false;
}

return true;
}
}

true
}

0 comments on commit f2c9d99

Please sign in to comment.