Skip to content

Commit

Permalink
Fix user commands
Browse files Browse the repository at this point in the history
Fix user commands
  • Loading branch information
vyfor authored Apr 14, 2024
2 parents 5b49d82 + 3458ca1 commit aa41020
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 28 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
- Lightweight and dependency-free
- Cross-platform support (Windows, Linux, macOS)
- Blazingly fast startup due to non-blocking, asynchronous nature
- Highly [configurable](https://github.com/vyfor/cord.nvim#-configuration) in Lua
- Highly [configurable](#-configuration) in Lua
- Offers a rich icon set for various components
- Automatically detects working directory and repository based on Git
- Identifies problems across active buffers
- Supports configurable idle status detection
- Provides [user commands](#%EF%B8%8F-user-commands) for managing the presence
- Is written in native code and uses Lua FFI for integration

## 🔌 Requirements
Expand Down Expand Up @@ -104,7 +105,7 @@ require('cord').setup({
})
```

### ⌨️ User commands (WIP)
### ⌨️ User commands
- `:CordConnect` - Initialize presence client internally and connect to Discord
- `:CordReconnect` - Reconnect to Discord
- `:CordDisconnect` - Disconnect from Discord
Expand Down
30 changes: 11 additions & 19 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ local last_updated = os.clock()
local last_presence

local function connect(config)
return discord.init(
discord.init(
config.editor.client,
config.editor.image,
config.editor.tooltip,
Expand Down Expand Up @@ -127,6 +127,7 @@ local function update_presence(config)
end

local function start_timer(config)
timer:stop()
if vim.g.cord_started == nil then
vim.g.cord_started = true
if not utils.validate_severity(config) then return end
Expand All @@ -136,7 +137,6 @@ local function start_timer(config)
discord.update_time()
end
end
timer:stop()
timer:start(0, config.timer.interval, vim.schedule_wrap(function() update_presence(config) end))
end

Expand All @@ -147,13 +147,9 @@ function cord.setup(userConfig)

local work = vim.loop.new_async(vim.schedule_wrap(function()
discord = utils.init_discord(ffi)
local err = connect(config)
if err ~= nil then
vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err))
else
if config.timer.enable then
start_timer(config)
end
connect(config)
if config.timer.enable then
start_timer(config)
end

vim.api.nvim_create_autocmd('ExitPre', { callback = function() discord.disconnect() end })
Expand All @@ -172,21 +168,15 @@ end

function cord.setup_usercmds(config)
vim.api.nvim_create_user_command('CordConnect', function()
local err = connect(config)
if err ~= nil then
vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err))
end
connect(config)
start_timer(config)
end, {})

vim.api.nvim_create_user_command('CordReconnect', function()
timer:stop()
discord.disconnect()
local err = connect(config)
if err ~= nil then
enabled = false
vim.api.nvim_err_writeln('[cord.nvim] Caught unexpected error: ' .. ffi.string(err))
end
last_presence = nil
connect(config)
start_timer(config)
enabled = true
end, {})
Expand All @@ -195,20 +185,21 @@ function cord.setup_usercmds(config)
timer:stop()
discord.disconnect()
enabled = false
last_presence = nil
end, {})

vim.api.nvim_create_user_command('CordTogglePresence', function()
if enabled then
timer:stop()
discord.clear_presence()
enabled = false
last_presence = nil
else
start_timer(config)
enabled = true
end
end, {})

-- user commands for showing and hiding the presence
vim.api.nvim_create_user_command('CordShowPresence', function()
start_timer(config)
enabled = true
Expand All @@ -218,6 +209,7 @@ function cord.setup_usercmds(config)
timer:stop()
discord.clear_presence()
enabled = false
last_presence = nil
end, {})
end

Expand Down
1 change: 1 addition & 0 deletions src/ipc/platform/unix_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::os::unix::net::UnixStream;

use crate::ipc::client::{Connection, RichClient};
use crate::ipc::utils;
use crate::rpc::packet::Packet;

impl Connection for RichClient {
fn connect(client_id: u64) -> Result<Self, Box<dyn std::error::Error>> {
Expand Down
10 changes: 3 additions & 7 deletions src/json/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ impl Packet {
json_str.push_str(",\"args\":{");

write!(&mut json_str, "\"pid\":{}", self.pid)?;

if let Some(activity) = &self.activity {
json_str.push_str(",\"activity\":{");
json_str.push_str(",\"activity\":");
activity.push_json(&mut json_str)?;
json_str.push('}');
}

json_str.push_str("}");
json_str.push_str("}}");

Ok(json_str)
}
}

impl Activity {
pub fn push_json(&self, json_str: &mut String) -> Result<(), Error> {
json_str.push_str("\"type\":0");
json_str.push_str("{\"type\":0");

if let Some(timestamp) = &self.timestamp {
write!(json_str, ",\"timestamps\":{{\"start\":{}}}", timestamp)?;
Expand Down Expand Up @@ -93,5 +91,3 @@ impl Activity {
Ok(())
}
}


1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub extern "C" fn disconnect() {
}
if let Some(mut client) = RICH_CLIENT.take() {
client.close().expect("Failed to close connection");
INITIALIZED = false;
}
}
}
Expand Down

0 comments on commit aa41020

Please sign in to comment.